Module:Text/testcases

local p = require('Module:UnitTests') local Text = require('Module:Text').Text

-- Tests re-written in Lua from https://de.wikipedia.org/wiki/Wikipedia:Lua/Modul/Text/Test version 198988523

function p:testChar self:equals(,Text.char,) self:equals(' ',Text.char({65,104,97}),'Aha') self:equals(' ',Text.char({"65",104,97}),'Aha') self:equals(' ',Text.char({98,108,97},2),'blabla') self:equals(' ',mw.ustring.match(Text.char({'something'}),"error"),"error") self:equals(' ',Text.char('something',1,true),'') self:equals(' ',mw.ustring.match(Text.char({7,8,9}),"error"),"error") self:preprocess_equals('','Aha') self:preprocess_equals('','blabla') self:preprocess_equals(,) end

function p:testConcatParams self:equals(,Text.concatParams,) self:equals(' ',Text.concatParams({}),'') self:equals(' ',Text.concatParams('A'),'A') self:equals(' ',Text.concatParams({'A'}),'A') self:equals(' ',Text.concatParams({'A','B','C'}),'A|B|C',{nowiki=1}) self:equals(' ',Text.concatParams({'A','B','C'},'-'),'A-B-C') self:equals(' ',Text.concatParams({'1','2','3'},nil,'%.2f'),'1.00|2.00|3.00',{nowiki=1}) self:preprocess_equals('',		'1.00:2.00:3.00') end

function p:testListToText self:equals(,Text.listToText,) self:equals(' ',Text.listToText({}),'') self:equals(' ',Text.listToText('A'),'A') self:equals(' ',Text.listToText({'A'}),'A') self:equals(' ',Text.listToText({'A','B','C'}),'A, B and C') self:equals(' ',Text.listToText({'1','2','3'},'%.2f'),'1.00, 2.00 and 3.00') self:preprocess_equals('',		'1.00, 2.00 and 3.00') end

function p:testContainsCJK local function singleTest(arg, expected) self:equals(' ',Text.containsCJK(arg),expected) end self:equals('',Text.containsCJK,false) singleTest('Draco Dormiens Nunquam Titillandus',false) singleTest('Никогда не щекочи спящего дракона',false) singleTest('सोए शेर को न जगाओ',false) singleTest('永远不要惊醒卧龙',true) singleTest('眠っているドラゴンをくすぐることはありません',true) singleTest('잠자는 용을 간지럽히지 마십시오',true) end

function p:testGetPlain local function singleTest(arg, expected) self:equals(' ',			Text.getPlain(arg),expected) end singleTest('a and b','a and b') singleTest('ab','ab') singleTest(' hello, world ',' hello, world ') singleTest('a b c','a b c') singleTest("a","a") singleTest("b","b") singleTest("a and b","a and b") singleTest("a and b","a and b") singleTest("a and b","a and b") singleTest("aa  b ","a b") singleTest("a b, c ","a b, c") end

function p:testRemoveDelimited self:equals('comment',Text.removeDelimited('ab',''),'ab') self:equals('2 comments',Text.removeDelimited(' hello, world ',''),' hello, world ') self:equals('ref',Text.removeDelimited('in foo. ','<ref',' '),'in foo.') end

function p:testIsLatin local function singleTest(arg,expected) self:equals(' ',Text.isLatinRange(arg),expected) end self:equals('',Text.isLatinRange,true) singleTest('abcd',true) singleTest('Ça ira',true) singleTest('α – Ω',false) singleTest('a日本d',false) end

function p:testIsQuote local function singleTest(arg,expected) self:equals(' ',Text.isQuote(arg),expected) end self:equals('',Text.isQuote,false) singleTest('"',true)	singleTest('日',false)	singleTest('abc"',false) end

function p:testQuote self:equals('',Text.quote,'“”') self:equals(' ',Text.quote('abcd'),'“abcd”') self:equals(' ',Text.quote('"abcd"'),'“"abcd"”') self:equals('fr',Text.quote('abcd','fr'),'«&#160;abcd&#160;»') self:equals('fr2',Text.quote('abcd','fr',2),'‹&#160;abcd&#160;›') end

function p:testQuoteUnquoted self:equals(,Text.quoteUnquoted,) self:equals(' ',Text.quoteUnquoted('abcd'),'“abcd”') self:equals(' ',Text.quoteUnquoted('"abcd"'),'"abcd"') self:equals('fr',Text.quoteUnquoted('abcd','fr'),'«&#160;abcd&#160;»') self:equals('fr2',Text.quoteUnquoted('abcd','fr',2),'‹&#160;abcd&#160;›') end

function p:testRemoveDiacritics local function singleTest(arg,expected) self:equals(' ',Text.removeDiacritics(arg),expected) end self:equals(,Text.removeDiacritics,) singleTest('abcd','abcd') singleTest('âbçdé','abcde') singleTest('a日本d','a日本d') end

function p:testSentenceTerminated local function singleTest(arg,expected) self:equals(' ',			Text.sentenceTerminated(arg),expected) end singleTest('Hello',false) singleTest('(Hello)',false) singleTest('Hello.',true) singleTest('„Deutsche“',false) singleTest('„Deutsche?“',true) singleTest('"English?"',true) singleTest('Hello!',true) end

function p:testUCFirstAll local function singleTest(arg,expected) self:equals(' ',Text.ucfirstAll(arg),expected) end self:equals(,Text.ucfirstAll,) singleTest(25,'25') singleTest('Help test me','Help Test Me') singleTest('an der Schönen','An Der Schönen') singleTest('an der Schönen &lauen','An Der Schönen &Lauen') self:equals('HTML ndash',Text.ucfirstAll('an der Schönen &lauen donau – X y z'),		'An Der Schönen &amp;Lauen Donau '..mw.text.decode('–',true)..' X Y Z') self:equals('HTML nbsp',Text.ucfirstAll('a b'),'A B') self:equals('many HTML',Text.ucfirstAll("&amp;&lt;&gt; &#8201;&#8204;&#8205;&#8206;&#8207;"),		'&amp;&lt;&gt; '..mw.text.decode("&#8201;&#8204;&#8205;&#8206;&#8207;")) end

function p:testUprightNonLatin local function singleTest(arg,expected) self:equals(' ',Text.uprightNonlatin(arg),expected) end singleTest('abc','abc') singleTest('abc ','abc ') singleTest('Mutsṭafah','Mutsṭafah') singleTest('μm','μm') singleTest('1 α-particle','1 α-particle') singleTest('Method 3α','Method 3α') singleTest('ΣΨΩ'," ΣΨΩ ") singleTest('abcΣΨΩxyz',		"abc ΣΨΩ xyz") singleTest('abЩyz',"ab Щ yz") singleTest('Войната 1915 година. Втора Македония',		" Войната 1915 година. Втора Македония ") singleTest('a日本d',"a 日本 d") end

function p:testExport self:preprocess_equals('',"") self:preprocess_equals('',"1") self:preprocess_equals('',"") self:preprocess_equals('',"1") self:preprocess_equals('',"") self:preprocess_equals('',"1") self:preprocess_equals('',"") self:preprocess_equals('',"1") self:preprocess_equals('',"a") self:preprocess_equals('',"a") self:preprocess_equals('',"A") self:preprocess_equals('',"a") end

return p