local p = require('Module:UnitTests')
local Text = require('Module:Text').Text()
local TextSandbox = require('Module:Text/sandbox').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('<code>{65,104,97}</code>',Text.char({65,104,97}),'Aha')
self:equals('<code>{"65",104,97}</code>',Text.char({"65",104,97}),'Aha')
self:equals('<code>{98,108,97},2</code>',Text.char({98,108,97},2),'blabla')
self:equals('<code>{"something"}</code>',mw.ustring.match(Text.char({'something'}),"error"),"error")
self:equals('<code>"something",1,true</code>',Text.char('something',1,true),'')
self:equals('<code>{7,8,9}</code>',mw.ustring.match(Text.char({7,8,9}),"error"),"error")
self:preprocess_equals('{{#invoke:Text|char|65 104 97}}','Aha')
self:preprocess_equals('{{#invoke:Text|char|98 108 97|*=2}}','blabla')
self:preprocess_equals('{{#invoke:Text|char|something|errors=0}}','')
end
function p:testConcatParams()
self:equals('',Text.concatParams(),'')
self:equals('<code>{}</code>',Text.concatParams({}),'')
self:equals('<code>A</code>',Text.concatParams('A'),'A')
self:equals('<code>{"A"}</code>',Text.concatParams({'A'}),'A')
self:equals('<code>{"A","B","C"}</code>',Text.concatParams({'A','B','C'}),'A|B|C',{nowiki=1})
self:equals('<code>{"A","B","C"},"-"</code>',Text.concatParams({'A','B','C'},'-'),'A-B-C')
self:equals('<code>{"1","2","3"},nil,"%.2f"</code>',Text.concatParams({'1','2','3'},nil,'%.2f'),'1.00|2.00|3.00',{nowiki=1})
self:preprocess_equals('{{#invoke:Text|concatParams|1|2|3|separator=:|format=%.2f}}',
'1.00:2.00:3.00')
end
function p:testListToText()
self:equals('',Text.listToText(),'')
self:equals('<code>{}</code>',Text.listToText({}),'')
self:equals('<code>A</code>',Text.listToText('A'),'A')
self:equals('<code>{"A"}</code>',Text.listToText({'A'}),'A')
self:equals('<code>{"A","B","C"}</code>',Text.listToText({'A','B','C'}),'A, B and C')
self:equals('<code>{"1","2","3"},"%.2f"</code>',Text.listToText({'1','2','3'},'%.2f'),'1.00, 2.00 and 3.00')
self:preprocess_equals('{{#invoke:Text|listToText|1|2|3|format=%.2f}}',
'1.00, 2.00 and 3.00')
end
function p:testContainsCJK()
local function singleTest(arg, expected)
self:equals('<code>'..arg..'</code>',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('<code><nowiki>'..arg..'</nowiki></code>',
Text.getPlain(arg),expected)
end
singleTest('a and b','a and b')
singleTest('a<!--comment-->b','ab')
singleTest(' <!--comment-->hello, world<!--comment 2--> ',' hello, world ')
singleTest('a <nowiki>b</nowiki> 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("a<!--I am an unclosed comment","a")
singleTest("'''unclosed bold","unclosed bold")
singleTest("<!-- hello -- -->'''a''' <!--world-->''<nowiki>b</nowiki>''","a b")
singleTest("<b>a</b> '''b'''<!-- hello -- world -->, <div style='font-size:100%;'>c</div>","a b, c")
end
function p:testRemoveDelimited()
self:equals('comment',Text.removeDelimited('a<!--comment-->b','<!--','-->'),'ab')
self:equals('2 comments',Text.removeDelimited(' <!--comment-->hello, world<!--comment 2--> ','<!--','-->'),' hello, world ')
self:equals('ref',Text.removeDelimited('in foo.<ref name=foo>{{cite web|title=Title|url=https://www.example.com}}</ref>','<ref','</ref>'),'in foo.')
end
function p:testIsLatin()
local function singleTest(arg,expected)
self:equals('<code>'..arg..'</code>',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('<code>'..arg..'</code>',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('<code>abcd</code>',Text.quote('abcd'),'“abcd”')
self:equals('<code>"abcd"</code>',Text.quote('"abcd"'),'“"abcd"”')
self:equals('fr',Text.quote('abcd','fr'),'« abcd »')
self:equals('fr2',Text.quote('abcd','fr',2),'‹ abcd ›')
end
function p:testQuoteUnquoted()
self:equals('',Text.quoteUnquoted(),'')
self:equals('<code>abcd</code>',Text.quoteUnquoted('abcd'),'“abcd”')
self:equals('<code>"abcd"</code>',Text.quoteUnquoted('"abcd"'),'"abcd"')
self:equals('fr',Text.quoteUnquoted('abcd','fr'),'« abcd »')
self:equals('fr2',Text.quoteUnquoted('abcd','fr',2),'‹ abcd ›')
end
function p:testRemoveDiacritics()
local function singleTest(arg,expected)
self:equals('<code>'..arg..'</code>',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('<code><nowiki>'..arg..'</nowiki></code>',
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('<code>'..tostring(arg)..'</code>',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 &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("&<>  ‌‍‎‏"),
'&<> '..mw.text.decode(" ‌‍‎‏"))
end
function p:testUprightNonLatin()
local function singleTest(arg,expected)
self:equals('<code>'..arg..'</code>',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('ΣΨΩ',"<span dir='auto' style='font-style:normal'>ΣΨΩ</span>")
singleTest('abcΣΨΩxyz',
"abc<span dir='auto' style='font-style:normal'>ΣΨΩ</span>xyz")
singleTest('abЩyz',"ab<span dir='auto' style='font-style:normal'>Щ</span>yz")
singleTest('Войната 1915 година. Втора Македония',
"<span dir='auto' style='font-style:normal'>Войната 1915 година. Втора Македония</span>")
singleTest('a日本d',"a<span dir='auto' style='font-style:normal'>日本</span>d")
end
function p:testExport()
self:preprocess_equals('{{#invoke:Text|containsCJK|a}}',"")
self:preprocess_equals('{{#invoke:Text|containsCJK|日}}',"1")
self:preprocess_equals('{{#invoke:Text|isLatinRange|日}}',"")
self:preprocess_equals('{{#invoke:Text|isLatinRange|a}}',"1")
self:preprocess_equals('{{#invoke:Text|isQuote|日}}',"")
self:preprocess_equals('{{#invoke:Text|isQuote|„}}',"1")
self:preprocess_equals('{{#invoke:Text|sentenceTerminated|日}}',"")
self:preprocess_equals('{{#invoke:Text|sentenceTerminated|a.}}',"1")
self:preprocess_equals('{{#invoke:Text|getPlain|a}}',"a")
self:preprocess_equals('{{#invoke:Text|removeDiacritics|a}}',"a")
self:preprocess_equals('{{#invoke:Text|ucfirstAll|a}}',"A")
self:preprocess_equals('{{#invoke:Text|uprightNonlatin|a}}',"a")
end
function p:testSplitSandbox()
self:equals_deep('"?a?b?c?d?e?f?", "c"', TextSandbox.split("?a?b?c?d?e?f?", "c"), mw.text.split("?a?b?c?d?e?f?", "c"))
self:equals_deep('"?a?b?c?d?e?f?", "?"', TextSandbox.split("?a?b?c?d?e?f?", "?"), mw.text.split("?a?b?c?d?e?f?", "?"))
self:equals_deep('"?a?b?c?d?e?f?", ""', TextSandbox.split("?a?b?c?d?e?f?", ""), mw.text.split("?a?b?c?d?e?f?", ""))
self:equals_deep('"?a?b?c?d?e?f?", "z"', TextSandbox.split("?a?b?c?d?e?f?", "z"), mw.text.split("?a?b?c?d?e?f?", "z"))
self:equals_deep('"?a?b?c?d?e?f?", "[bdf]"', TextSandbox.split("?a?b?c?d?e?f?", "[bdf]"), mw.text.split("?a?b?c?d?e?f?", "[bdf]"))
self:equals_deep('"?a?b?c?d?e?f?", "[bdf]", true', TextSandbox.split("?a?b?c?d?e?f?", "[bdf]", true), mw.text.split("?a?b?c?d?e?f?", "[bdf]", true))
self:preprocess_equals_many('{{#invoke:Text/sandbox|split|?a?b?c?|','}}', {
{'c', mw.text.split("?a?b?c?", "c")[1]},
{'?', mw.text.split("?a?b?c?", "?")[1]},
{'?|false|2', mw.text.split("?a?b?c?", "?")[2]},
{'?|false|-2', mw.text.split("?a?b?c?", "?")[#mw.text.split("?a?b?c?", "?") - 2 + 1]},
{'|false|2', mw.text.split("?a?b?c?", "")[2]},
{'z', mw.text.split("?a?b?c?", "z")[1]},
{'[bc]', mw.text.split("?a?b?c?", "[bc]")[1]},
{'[bc]|true', mw.text.split("?a?b?c?", "[bc]", true)[1]},
})
end
return p