Module:usex/testcases

local ScribuntoUnit = require('Module:ScribuntoUnit') local suite = ScribuntoUnit:new local m_usex = require('Module:usex') local m_languages = require("Module:languages") local m_scripts = require("Module:scripts") local en, fr, ko = m_languages.getByCode('en'), m_languages.getByCode('fr'), m_languages.getByCode('ko') local rtl_script = m_scripts.getByCode('Hebr') local translation_hint_usex = '(please add an English translation of this usage example)' local translation_hint_quote = '(please add an English translation of this quote)'

local function format(args) return m_usex.format_usex { lang = args.lang or en, sc = args.sc,          				-- The script code usex = args.usex,          			-- The example or quote translation = args.translation,    	-- The English translation for examples on foreign entries. transliteration = args.transliteration,	-- The Latin transliteration for examples in non-Latin script. noenum = args.noenum or false,			-- To use in unnumbered sections (not following a definition). inline = args.inline or false,			-- To include the translation in the same line as the example, -- separated from the example by em dash. ref = args.ref,            			-- To include a reference or link to an originating external site quote = args.quote or false,			-- Used from Template:quote lit = args.lit,            			-- A literal translation. substs = args.substs, 					-- Substitutions to be applied to handle irregular transliterations in certain languages qualifiers = args.qualifier or {}		-- To include qualifier for the example or quote. } end

function suite:test_format_without_usex_displays_hint local result = format {} suite:assertStringContains('(please add the primary text of this usage example)', result, true) end

function suite:test_format_quote_without_quote_displays_hint local result = format {quote=true} suite:assertStringContains('(please add the primary text of this quote)', result, true) end

function suite:test_format_english local result = format {usex='This is an example'} suite:assertStringContains('This is an example', result, true) suite:assertNotStringContains(translation_hint_usex, result, true) end

function suite:test_format_usex_with_embedded_wiki_link local result = format {usex='This is an example'} suite:assertStringContains('This is an example', result, true) end

function suite:test_format_usex_adds_script_and_language_tag local result = format {usex='Example'} suite:assertStringContains('Example', result, false) end

function suite:test_format_usex_explicit_script local result = format {usex='Example', sc=m_scripts.getByCode('Cyrl')} suite:assertStringContains('Example', result, false) end

function suite:test_format_usex_adds_italic_tag_for_latn_script local result = format {usex='Example'} suite:assertStringContains('Example', result, false) end

function suite:test_format_usex_adds_does_not_add_italic_tag_for_quote local result = format {usex='This is a quote', quote=true} suite:assertNotStringContains('', result, true) end

function suite:test_format_usex_without_translation_displays_usex local result = format {lang=fr, usex='Ceci est un exemple'} suite:assertStringContains('Ceci est un exemple', result, true) end

function suite:test_format_usex_without_translation_displays_hint local result = format {lang=fr, usex='Ceci est un exemple'} suite:assertStringContains(translation_hint_usex, result, true) end

function suite:test_format_with_translation local result = format {lang=fr, usex='Ceci est un exemple', translation='This is an example'} suite:assertStringContains('Ceci est un exemple', result, true) suite:assertStringContains('This is an example', result, true) suite:assertNotStringContains(translation_hint_usex, result, true) end

function suite:test_format_usex_with_manual_transliteration local result = format {lang=fr, usex='Ceci est un exemple', translation='This is an example', transliteration='This is the transliterated text'} suite:assertStringContains('This is the transliterated text', result, true) end

function suite:test_format_usex_with_automatic_transliteration local result = format {lang=ko, usex='초식남'} suite:assertStringContains('chosingnam', result, true) end

function suite:test_format_usex_transliterated_text_is_italic local result = format {lang=ko, usex='초식남'} suite:assertStringContains('chosingnam', result, false) end

-- tr=- means omit transliteration altogether function suite:test_format_usex_with_dash_transliteration_parameter_disables_transliteration local result = format {lang=ko, usex='초식남', transliteration='-'} suite:assertNotStringContains('chosingnam', result, true) end

function suite:test_format_usex_with_noenum local result = format {lang=fr, usex='Ceci est un exemple', noenum=true} suite:assertStringContains('^\n: ', result, false) end

function suite:test_format_usex_inline local result = format {lang=fr, usex='Ceci est un exemple', transliteration='This is an example', inline=true} suite:assertStringContains('Ceci est un exemple', result, true) suite:assertStringContains('This is an example', result, true) suite:assertStringContains('―', result, true) suite:assertNotStringContains('', result, true) suite:assertNotStringContains('', result, true) end

function suite:test_format_usex_inline_and_transliteration local result = format {lang=ko, usex='초식남', translation='This is an example', inline=true} suite:assertStringContains('―', result, true) suite:assertStringContains('chosingnam', result, true) end

function suite:test_format_usex_with_ref local result = format {lang=fr, usex='Ceci est un exemple', ref=' '} suite:assertStringContains(' ', result, true) end

function suite:test_format_quote_without_translation_displays_hint_hint local result = format {lang=fr, usex='Ceci est un quote', quote=true} suite:assertStringContains(translation_hint_quote, result, true) end

function suite:test_format_quote_with_translation local result = format {lang=fr, usex='Ceci est un quote', translation='This is a quote', quote=true} suite:assertNotStringContains(translation_hint_quote, result, true) suite:assertStringContains('This is a quote', result, true) end

function suite:test_format_usex_with_literal_translation local result = format {lang=fr, usex='Ceci est un exemple', translation='This is an example', lit='This is the literal translation'} suite:assertStringContains('This is the literal translation', result, true) suite:assertStringContains('(literally, “.+”)', result, false) end

function suite:test_format_usex_adds_left_to_right_mark_after_example local result = format {lang=fr, usex='Original', translation='Translated', sc=rtl_script} suite:assertStringContains('&rlm;.+Original.+&lrm;.+Translated', result, false) end

function suite:test_format_usex_adds_left_to_right_mark_after_example_inline local sc = m_scripts.getByCode('Cyrl') local result = format {lang=fr, usex='Original', translation='Translated', sc=rtl_script, inline=true} suite:assertStringContains('&rlm;.+Original.+&lrm;.+Translated', result, false) end

function suite:test_qualifier local qualifier = {'A qualifier'} local result = format {lang=fr, usex='Usex', qualifier=qualifier} suite:assertStringContains('A qualifier', result, false) end

-- microformat tests

function suite:test_format_usex_is_wrapped_ local result = format {usex='Example'} suite:assertStringContains(' .*Example.* ', result, false) end

function suite:test_format_usex_example_content_is_wrapped local result = format {usex='Example'} suite:assertStringContains('Example', result, false) end

function suite:test_format_usex_translation_is_wrapped local result = format {usex='Example', translation='Translated'} suite:assertStringContains(' Translated ', result, true) end

function suite:test_format_usex_transliteration_is_wrapped local result = format {usex='Example', transliteration='Transliteration'} suite:assertStringContains('Transliteration', result, true) end

function suite:test_format_usex_literally_is_wrapped local result = format {usex='Example', lit='Literally'} suite:assertStringContains(' Literally ', result, true) end

function suite:test_format_quote_quotation_is_wrapped local result = format {usex='Quotation', quote=true} suite:assertStringContains(' .*Quotation.* ', result, false) end

function suite:test_format_quote_quotation_content_is_wrapped_ local result = format {usex='Quotation', quote=true} suite:assertStringContains('Quotation ', result, false) end

return suite