Module:User:Jberkel/usex/testcases

local ScribuntoUnit = require('Module:ScribuntoUnit') local suite = ScribuntoUnit:new local m_usex = require('Module:User:Jberkel/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 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(       args.lang or en,        args.sc,              -- The script code        args.usex,            -- The example or quote        args.translation,     -- The English translation for examples on foreign entries.        args.transliteration, -- The Latin transliteration for examples in non-Latin script.        args.noenum or false, -- To use in unnumbered sections (not following a definition).        args.inline or false, -- To include the translation in the same line as the example,                              -- separated from the example by em dash.        args.ref,             -- To include a reference or link to an originating external site        args.quote or false,  -- Used from Template:quote        args.lit              -- A literal translation.    ) 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'} suite:assertStringContains('Original.+&lrm;.+Translated', result, false) end

function suite:test_format_usex_adds_left_to_right_mark_after_example_inline local result = format {lang=fr, usex='Original', translation='Translated', inline=true} suite:assertStringContains('Original.+&lrm;.+Translated', 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, false) end

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

function suite:test_format_usex_literally_is_wrapped local result = format {usex='Example', lit='Literally'} suite:assertStringContains(' Literally ', result, false) 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