Jump to content

Module:Text/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Hike395 (talk | contribs) at 15:56, 17 July 2022 (more tests). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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:testIsLatin()
	self:equals('blank',Text.isLatinRange(),true)
	self:equals('latin',Text.isLatinRange('abcd'),true)
	self:equals('diacritic',Text.isLatinRange('Ça ira'),true)
	self:equals('greek',Text.isLatinRange('α – Ω'),false)
	self:equals('CJK',Text.isLatinRange('a日本d'),false)
end

function p:testSentenceTerminated()
	self:preprocess_equals_sandbox_many('{{#invoke:Text', 'sentenceTerminated', {
		{"Hello", ""},
		{"(Hello)", ""},
		{"Hello.", "1"},
		{"„Deutsche“", ""},
		{"[[Hello!]]", "1"},
		{"„Deutsche?“", "1"},
		{"\"English?\"", "1"},
	})
end

function p:testUprightNonLatin()
	self:equals('simple',Text.uprightNonlatin('abc'),'abc')
	self:equals('with blank',Text.uprightNonlatin('abc '),'abc ')
	self:equals('diacritic',Text.uprightNonlatin('Mutsṭafah'),'Mutsṭafah')
	self:equals('greek1',Text.uprightNonlatin('μm'),'μm')
	self:equals('greek2',Text.uprightNonlatin('1 α-particle'),'1 α-particle')
	self:equals('greek3',Text.uprightNonlatin('Method 3α'),'Method 3α')
	self:equals('cyrillic',Text.uprightNonlatin('abЩyz'),"ab<span dir='auto' style='font-style:normal'>Щ</span>yz")
	self:equals('capital greek1',Text.uprightNonlatin('ΣΨΩ'),"<span dir='auto' style='font-style:normal'>ΣΨΩ</span>")
	self:equals('capital greek2',Text.uprightNonlatin('abcΣΨΩxyz'),
		"abc<span dir='auto' style='font-style:normal'>ΣΨΩ</span>xyz")
end

return p