Module:Text/testcases

From MOASSpedia
Jump to navigation Jump to search

Documentation for this module may be created at Module:Text/testcases/doc

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('nil',Text.char(),'')
	self:equals('3 chars',Text.char({65,104,97}),'Aha')
	self:equals('rep',Text.char({98,108,97},2),'blabla')
	self:equals('lenient',Text.char('something',1,true),'')
end

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

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

function p:testGetPlain()
	self:equals('plain',Text.getPlain('a and b'),'a and b')
	self:equals('comment',Text.getPlain('a<!--comment-->b'),'ab')
	self:equals('2 comments',Text.getPlain(' <!--comment-->hello, world<!--comment 2--> '),' hello, world ')
	self:equals('wikimarkup',Text.getPlain('a <nowiki>b</nowiki> c'),'a b c')
	self:equals('bold',Text.getPlain("'''a'''"),"a")
	self:equals('italic',Text.getPlain("''b''"),"b")
	self:equals('bold and italic',Text.getPlain("'''a''' and ''b''"),"a and b")
	self:equals('nbsp',Text.getPlain("a&nbsp;and&nbsp;b"),"a and b")
	self:equals('weird1',Text.getPlain("'''a'' and '''b''"),"a and b")
	self:equals('weird2',Text.getPlain("a<!--I am an unclosed comment"),"a")
	self:equals('weird3',Text.getPlain("'''unclosed bold"),"unclosed bold")
	self:equals('mix1',Text.getPlain("<!-- hello -- -->'''a'''&nbsp;<!--world-->''<nowiki>b</nowiki>''"),"a b")
	self:equals('mix2',Text.getPlain("<b>a</b>&nbsp;'''b'''<!-- hello -- world -->,&nbsp;<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:testSentenceTerminated()
	self:preprocess_equals_sandbox_many('{{#invoke:Text', 'sentenceTerminated', {
		{"Hello", ""},
		{"(Hello)", ""},
		{"Hello.", "1"},
		{"„Deutsche“", ""},
		{"[[Hello!]]", "1"},
		{"„Deutsche?“", "1"},
		{"\"English?\"", "1"},
	})
end

return p