Jump to content

Module:WikitextParser/testcases

From Wikipedia, the free encyclopedia
local W = require( 'Module:WikitextParser' )
local testpage = mw.title.new( 'Module:WikitextParser/testpage' ):getContent()
local ScribuntoUnit = require( 'Module:ScribuntoUnit' )
local Suite = ScribuntoUnit:new()

function Suite:testLead()
	self:assertEquals( '', W.getLead( '' ) )
	self:assertEquals( '', W.getLead( '==b==' ) )
	self:assertEquals( 'a', W.getLead( 'a' ) )
	self:assertEquals( 'a', W.getLead( 'a', 'a\n==b==' ) )
	self:assertEquals( 'This test page interacts with [[Module:WikitextParser/testcases]].', W.getLead( testpage ) )
end

function Suite:testSection()
	self:assertEquals( nil, W.getSection( 'a', 'a' ) )
	self:assertEquals( '', W.getSection( '==a==', 'a' ) )
	self:assertEquals( 'b', W.getSection( '==a==\nb', 'a' ) )
	self:assertEquals( 'b', W.getSection( '==a==\nb\n==c==\nd', 'a' ) )
	self:assertEquals( 'b', W.getSection( '==a==\nb\n==a==\nd', 'a' ) )
	self:assertEquals( 'b\n===c===\nd', W.getSection( '==a==\nb\n===c===\nd', 'a' ) )
	self:assertEquals( 'd', W.getSection( '==a==\nb\n===c===\nd', 'c' ) )
	self:assertEquals( 'a\n===Subsection===\nb', W.getSection( testpage, 'Section' ) )
	self:assertEquals( 'b', W.getSection( testpage, 'Subsection' ) )
end

function Suite:testSectionTag()
	self:assertEquals( nil, W.getSectionTag( 'a', 'a' ) )
	self:assertEquals( nil, W.getSectionTag( 'a<section begin="b" /><section end="b" />c', 'a' ) )
	self:assertEquals( '', W.getSectionTag( 'a<section begin="b" /><section end="b" />c', 'b' ) )
	self:assertEquals( 'c', W.getSectionTag( 'a<section begin="b" />c<section end="b" />d', 'b' ) )
	self:assertEquals( '', W.getSectionTag( testpage, 'a' ) )
	self:assertEquals( 'b', W.getSectionTag( testpage, 'b' ) )
	self:assertEquals( '{|\n!c\n|}', W.getSectionTag( testpage, 'c' ) )
	self:assertEquals( nil, W.getSectionTag( testpage, 'd' ) )
end

function Suite:testSections()
	self:assertDeepEquals( {}, W.getSections( '' ) )
	self:assertDeepEquals( {}, W.getSections( 'a' ) )
	self:assertDeepEquals( { ['a']='' }, W.getSections( '==a==' ) )
	self:assertDeepEquals( { ['b']='' }, W.getSections( 'a\n==b==' ) )
	self:assertDeepEquals( { ['a']='', ['b']='' }, W.getSections( '==a==\n==b==' ) )
	self:assertDeepEquals( { ['a']='b' }, W.getSections( '==a==\nb' ) )
	self:assertDeepEquals( { ['b']='c' }, W.getSections( 'a\n==b==\nc' ) )
	self:assertDeepEquals( { ['b']='c', ['d']='e' }, W.getSections( 'a\n==b==\nc\n===d===\ne' ) )
end

function Suite:testLists()
	self:assertDeepEquals( {}, W.getLists( '' ) )
	self:assertDeepEquals( {}, W.getLists( 'a' ) )
	self:assertDeepEquals( { '*a' }, W.getLists( '*a' ) )
	self:assertDeepEquals( { '*a\n*b' }, W.getLists( '*a\n*b' ) )
	self:assertDeepEquals( { '*b\n*c' }, W.getLists( 'a\n*b\n*c\nd' ) )
	self:assertDeepEquals( { '*b\n*c', '#e\n#f' }, W.getLists( 'a\n*b\n*c\nd\n#e\n#f\ng' ) )
	self:assertDeepEquals( {
		'*b\n*c',
		'#e\n#f',
		'*c\n*d',
		'#h\n#i',
		'*c\n*d',
		'#h\n#i',
	}, W.getLists( testpage ) )
end

function Suite:testParagraphs()
	self:assertDeepEquals( {}, W.getParagraphs( '' ) )
	self:assertDeepEquals( {}, W.getParagraphs( '{{a}}' ) )
	self:assertDeepEquals( {}, W.getParagraphs( '{{a\n|b=c}}' ) )
	self:assertDeepEquals( {}, W.getParagraphs( '{{a}}\n{{b}}\n{{c}}' ) )
	self:assertDeepEquals( {}, W.getParagraphs( '{{a}}\n\n{{b}}\n\n{{c}}' ) )
	self:assertDeepEquals( { 'a' }, W.getParagraphs( 'a' ) )
	self:assertDeepEquals( { 'a{{b}}' }, W.getParagraphs( 'a{{b}}' ) )
	self:assertDeepEquals( { '{{a}}b' }, W.getParagraphs( '{{a}}b' ) )
	self:assertDeepEquals( { 'a' }, W.getParagraphs( 'a\n\n{{b}}' ) )
	self:assertDeepEquals( { 'b' }, W.getParagraphs( '{{a}}\n\nb' ) )
	self:assertDeepEquals( { '{{a}}b{{c}}' }, W.getParagraphs( '{{a}}b{{c}}' ) )
	self:assertDeepEquals( { 'a', 'b', 'c' }, W.getParagraphs( 'a\n\nb\n\nc' ) )
	self:assertDeepEquals( { 'a', 'c' }, W.getParagraphs( 'a\n\n{{b}}\n\nc' ) )
	self:assertDeepEquals( {
		'X1',
		'{{X2}} X3 {{X4}}',
	}, W.getParagraphs( W.getSection( testpage, 'Paragraphs' ) ) )
end

function Suite:testTemplates()
	self:assertDeepEquals( {}, W.getTemplates( 'a' ) )
	self:assertDeepEquals( { '{{a}}' }, W.getTemplates( '{{a}}' ) )
	self:assertDeepEquals( { '{{b}}' }, W.getTemplates( 'a{{b}}c' ) )
	self:assertDeepEquals( { '{{a|b=c}}' }, W.getTemplates( '{{a|b=c}}' ) )
	self:assertDeepEquals( { '{{a\n|b=c\n}}' }, W.getTemplates( '{{a\n|b=c\n}}' ) )
	self:assertDeepEquals( { '{{b}}', '{{d}}' }, W.getTemplates( 'a{{b}}c{{d}}e' ) )
	self:assertDeepEquals( { '{{b}}', '{{d}}' }, W.getTemplates( 'a{{b}}c{{d}}e' ) )
	self:assertDeepEquals( { '{{a|b={{c}}}}' }, W.getTemplates( '{{a|b={{c}}}}' ) )
	self:assertDeepEquals( {
		'{{X2}}',
		'{{X3}}',
		'{{X4}}',
		'{{X11\n|b=c\n}}',
		'{{X12\n|b=d\n}}',
		'{{X2}}',
		'{{X4}}',
		'{{X5}}',
		'{{reflist}}',
		'{{reflist|group=i}}',
		'{{Infobox\n|a=1\n|b=2\n|d=4\n|c=3\n}}'
	}, W.getTemplates( testpage ) )
end

function Suite:testTemplate()
	self:assertEquals( '{{a}}', W.getTemplate( '{{a}}', 'a' ) )
	self:assertEquals( '{{a|b=c}}', W.getTemplate( '{{a|b=c}}', 'a' ) )
	self:assertEquals( '{{a|b=c}}', W.getTemplate( '{{a|b=c}}', 'A' ) )
	self:assertEquals( '{{Infobox\n|a=1\n|b=2\n|d=4\n|c=3\n}}', W.getTemplate( testpage, 'Infobox' ) )
end

function Suite:testParameters()
	self:assertDeepEquals( { [1]='b' }, W.getParameters( '{{a|b}}' ) )
	--self:assertDeepEquals( { [1]='b' }, W.getParameters( '{{a|1=b}}' ) )
	self:assertDeepEquals( { [1]='b',['c']='1' }, W.getParameters( '{{a|b|c=1}}' ) )
	self:assertDeepEquals( { [1]='b',['c']='d' }, W.getParameters( '{{a|b|c=d}}' ) )
	self:assertDeepEquals( { ['b']='c' }, W.getParameters( '{{a|b=c}}' ) )
	self:assertDeepEquals( { ['b']='c', ['d']='e' }, W.getParameters( '{{a|b=c|d=e}}' ) )
end

function Suite:testTags()
	self:assertDeepEquals( {}, W.getTags( 'a' ) )
	self:assertDeepEquals( { '<div>a' }, W.getTags( '<div>a' ) )
	self:assertDeepEquals( { '<div></div>' }, W.getTags( '<div></div>' ) )
	self:assertDeepEquals( { '<div></div>' }, W.getTags( 'a<div></div>b' ) )
	self:assertDeepEquals( { '<div>a</div>' }, W.getTags( '<div>a</div>' ) )
	self:assertDeepEquals( { '<div>b</div>' }, W.getTags( 'a<div>b</div>c' ) )
	self:assertDeepEquals( { '< div >a</ div >' }, W.getTags( '< div >a</ div >' ) )
	self:assertDeepEquals( { '<div class="a">b</div>' }, W.getTags( '<div class="a">b</div>' ) )
	self:assertDeepEquals( { '<div><div></div></div>', '<div></div>' }, W.getTags( '<div><div></div></div>' ) )
	self:assertDeepEquals( { '<div>b<div>c</div>d</div>', '<div>c</div>' }, W.getTags( 'a<div>b<div>c</div>d</div>e' ) )
	self:assertDeepEquals( { '<div><div><span>a</span></div></div>', '<div><span>a</span></div>', '<span>a</span>' }, W.getTags( '<div><div><span>a</span></div></div>' ) )
	self:assertDeepEquals( {
		'<gallery>\nFile:A.png|g<ref>g</ref>\nFile:B.png\n</gallery>', 
		'<ref>g</ref>', 
		'<br>', 
		'<br />', 
		'<hr>', 
		'<div>a<div>b</div></div>', 
		'<div>b</div>', 
		'<span class="a">b<span id="c">d</span></span>', 
		'<span id="c">d</span>', 
		'<section begin="a" />', 
		'<section end="a" />', 
		'<section begin="b" />', 
		'<section end="b" />', 
		'<section begin="c" />', 
		'<section end="c" />', 
		'<ref>b</ref>', 
		'<ref name="d">e</ref>', 
		'<ref name="d" />', 
		'<ref name="h" group="i">j</ref>', 
		'<ref name="l" />', 
		'<ref name="l">m</ref>', 
		'<noinclude>b</noinclude>', 
	}, W.getTags( testpage ) )
end

function Suite:testGalleries()
	self:assertDeepEquals( {}, W.getGalleries( 'a' ) )
	self:assertDeepEquals( { '<gallery>a</gallery>' }, W.getGalleries( '<gallery>a</gallery>' ) )
	self:assertDeepEquals( { '<gallery>b</gallery>' }, W.getGalleries( 'a<gallery>b</gallery>c' ) )
	self:assertDeepEquals( { '<gallery>\nFile:A.png\nFile:B.png\n</gallery>' }, W.getGalleries( '<gallery>\nFile:A.png\nFile:B.png\n</gallery>' ) )
	self:assertDeepEquals( { '<gallery>\nFile:A.png\nFile:B.png\n</gallery>' }, W.getGalleries( 'a<gallery>\nFile:A.png\nFile:B.png\n</gallery>b' ) )
	self:assertDeepEquals( {
		'<gallery>\nFile:A.png|g<ref>g</ref>\nFile:B.png\n</gallery>', 
	}, W.getGalleries( testpage ) )
end

function Suite:testReferences()
	self:assertDeepEquals( {}, W.getReferences( 'a' ) )
	self:assertDeepEquals( { '<ref>a</ref>' }, W.getReferences( '<ref>a</ref>' ) )
	self:assertDeepEquals( { '<ref>b</ref>' }, W.getReferences( 'a<ref>b</ref>c' ) )
	self:assertDeepEquals( { '<ref>b</ref>', '<ref>d</ref>' }, W.getReferences( 'a<ref>b</ref>c<ref>d</ref>' ) )
	self:assertDeepEquals( { '<ref name="b">c</ref>', '<ref name="b" />' }, W.getReferences( 'a<ref name="b">c</ref>d<ref name="b" />' ) )
	self:assertDeepEquals( {
		'<ref>g</ref>', 
		'<ref>b</ref>', 
		'<ref name="d">e</ref>', 
		'<ref name="d" />', 
		'<ref name="h" group="i">j</ref>', 
		'<ref name="l" />', 
		'<ref name="l">m</ref>', 
	}, W.getReferences( testpage ) )
end

function Suite:testTables()
	self:assertDeepEquals( {}, W.getTables( 'a' ) )
	self:assertDeepEquals( { '{|\n!b\n|}' }, W.getTables( 'a\n{|\n!b\n|}\nc' ) )
	self:assertDeepEquals( { '{|\n!b\n|}', '{|\n|d\n|}', '{|id="e"\n|e\n|}' }, W.getTables( 'a\n{|\n!b\n|}\nc\n{|\n|d\n|}\n{|id="e"\n|e\n|}f' ) )
	self:assertDeepEquals( {
		'{|\n!b\n|}', 
		'{|\n|d\n|}', 
		'{|id="e"\n|e\n|}', 
		'{|\n!c\n|}', 
		'{|\n|X2\n|}', 
		'{|\n|e\n|}', 
		'{|\n|k\n|}', 
		'{|\n|e\n|}', 
		'{|\n|k\n|}', 
	}, W.getTables( testpage ) )
end

function Suite:testLinks()
	self:assertDeepEquals( {}, W.getLinks( 'a' ) )
	self:assertDeepEquals( { '[[a]]' }, W.getLinks( '[[a]]' ) )
	self:assertDeepEquals( { '[[a|b]]' }, W.getLinks( '[[a|b]]' ) )
	self:assertDeepEquals( { '[[b]]' }, W.getLinks( 'a[[b]]c' ) )
	self:assertDeepEquals( { '[[b]]', '[[d]]' }, W.getLinks( 'a[[b]]c[[d]]e' ) )
	self:assertDeepEquals( {
		'[[Module:WikitextParser/testcases]]', 
		'[[link]]', 
		'[[File:Name.jpg|thumb|X1]]', 
		'[[File:A.png]]', 
		'[[File:C.png]]', 
		'[[b]]', 
		'[[d]]', 
		'[[Module talk:Transcluder/testcases]]', 
		'[[module talk:Transcluder/testcases]]', 
		'[[Module talk:Transcluder/testcases|a]]', 
		'[[module talk:Transcluder/testcases|a]]', 
		'[[File:A.png]]', 
		'[[File:f.png]]', 
		'[[File:A.png]]', 
		'[[File:f.png]]', 
		'[[Category:X1]]', 
		'[[Category:X2]]', 
		'[[Category:X3]]', 
	}, W.getLinks( testpage ) )
end

function Suite:testCategories()
	self:assertDeepEquals( {}, W.getCategories( 'a' ) )
	self:assertDeepEquals( {}, W.getCategories( '[[a]]' ) )
	self:assertDeepEquals( { '[[Category:a]]' }, W.getCategories( '[[Category:a]]' ) )
	self:assertDeepEquals( { '[[ category : a ]]' }, W.getCategories( '[[ category : a ]]' ) )
	self:assertDeepEquals( { '[[Category:b]]' }, W.getCategories( 'a[[Category:b]]c' ) )
	self:assertDeepEquals( { '[[Category:b]]', '[[Category:c]]' }, W.getCategories( 'a[[Category:b]][[Category:c]]d' ) )
	self:assertDeepEquals( {
		'[[Category:X1]]', 
		'[[Category:X2]]', 
		'[[Category:X3]]', 
	}, W.getCategories( testpage ) )
end

function Suite:testFiles()
	self:assertDeepEquals( {}, W.getFiles( 'a' ) )
	self:assertDeepEquals( {}, W.getFiles( '[[a]]' ) )
	self:assertDeepEquals( { '[[File:a]]' }, W.getFiles( '[[File:a]]' ) )
	self:assertDeepEquals( { '[[ file : a ]]' }, W.getFiles( '[[ file : a ]]' ) )
	self:assertDeepEquals( { '[[File:b]]' }, W.getFiles( 'a[[File:b]]c' ) )
	self:assertDeepEquals( { '[[File:b]]', '[[File:c]]' }, W.getFiles( 'a[[File:b]][[File:c]]d' ) )
	self:assertDeepEquals( {
		'[[File:Name.jpg|thumb|X1]]', 
		'[[File:A.png]]', 
		'[[File:C.png]]', 
		'[[File:A.png]]', 
		'[[File:f.png]]', 
		'[[File:A.png]]', 
		'[[File:f.png]]', 
	}, W.getFiles( testpage ) )
end

function Suite:testExternalLinks()
	self:assertDeepEquals( {}, W.getExternalLinks( '' ) )
	self:assertDeepEquals( {}, W.getExternalLinks( 'a' ) )
	self:assertDeepEquals( {}, W.getExternalLinks( '[[a]]' ) )
	self:assertDeepEquals( { '[//www.example.com]' }, W.getExternalLinks( '[//www.example.com]' ) )
	self:assertDeepEquals( { '[http://www.example.com]' }, W.getExternalLinks( '[http://www.example.com]' ) )
	self:assertDeepEquals( { '[https://www.example.com]' }, W.getExternalLinks( '[https://www.example.com]' ) )
	self:assertDeepEquals( { '[//www.example.com example]' }, W.getExternalLinks( '[//www.example.com example]' ) )
	self:assertDeepEquals( { '[http://www.example.com example]' }, W.getExternalLinks( '[http://www.example.com example]' ) )
	self:assertDeepEquals( { '[https://www.example.com example]' }, W.getExternalLinks( '[https://www.example.com example]' ) )
end

return Suite