Module:ApplyLinkAnnotations/testcases

-- Unit tests for [[Module:{{ROOTPAGENAME}}]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local m = require('Module:ApplyLinkAnnotations')
local s = require('Module:ApplyLinkAnnotations/sandbox')

local t_caveat = {}
t_caveat["SEE DOCS: html comments"] = { "<!-- wikitext comments -->", "<!-- wikitext comments -->" }
t_caveat["SEE DOCS: link extensions"] = { "*[[Flower]]y", "*[[Flower]]y" }
t_caveat["SEE DOCS: single letter article titles"] = { "*[[T]]", "*[[T]]" }

local t_match = {}
t_match["deep indent with asterisks"] = { "** [[Wikitext]]", "** {{Annotated link |Wikitext}}" }
t_match["std list item link"] = { "*[[hello]]", "* {{Annotated link |hello}}" }
t_match["pipe list item"] = { "*[[hello|hey]]", "* {{Annotated link |hello|hey}}" }
t_match["escaped spaces"] = { "*[[Afternoon_delight]]", "* {{Annotated link |Afternoon_delight}}" }
t_match["section links"] = { "*[[Clowns#On Halloween]]", "* {{Annotated link |Clowns#On Halloween}}" }
t_match["indented list item"] = { ":*[[related]]", ":* {{Annotated link |related}}" }
t_match["very indented item"] = { "::::*[[loosely related|tangential]]", "::::* {{Annotated link |loosely related|tangential}}" }
t_match["piped, indented item"] = { ":*[[related|renamed]]", ":* {{Annotated link |related|renamed}}" }
t_match["accented"] = { "*[[El Niño]]", "* {{Annotated link |El Niño}}" }
t_match["extra spaces"] = { "*   [[Dark Matter]]", "* {{Annotated link |Dark Matter}}" }

local t_avoid = {}
t_avoid["manual annotation"] = "*[[hello]] - an English greeting"
t_avoid["non-listed link"] = "[[Mexico]]"
t_avoid["indented but non-listed"] = ":[[Cancun]]"
t_avoid["link at end"] = "*Alternative theories include [[Nonsense]]"
t_avoid["ordered list"] = "#[[hello]]"
t_avoid["already annotated"] = "* {{Annotated link |hello}}"
t_avoid["section headings"] = "== Section heading =="
t_avoid["stylized links"] = "*''[[Emphasized link]]''"
t_avoid["non-article namespace"] = "*[[File:link_to_file]]"
t_avoid["pipe trick"] = "*[[hello (greeting)|]]"
t_avoid["reverse pipe trick"] = "*[[|Moscow, Russia]]"


function p:test_lines_to_match()
	-- Various lines that should match
	for name, case in pairs(t_match) do
		self:equals('<b>'..name..'</b><br/><kbd><nowiki>'..case[1]..'</nowiki></kbd>',m._testline(case[1]),case[2],{nowiki=1})
		self:equals('<b>'..name..' (Sandbox)</b><br/><kbd><nowiki>'..case[1]..'</nowiki></kbd>',s._testline(case[1]),case[2],{nowiki=1})
	end
end

function p:test_known_caveats()
	-- Lines with known behaviour that probably isn't what you think/hope it is
	for name, case in pairs(t_caveat) do
		self:equals('<b>'..name..'</b><br/><kbd><nowiki>'..case[1]..'</nowiki></kbd>',m._testline(case[1]),case[2],{nowiki=1})
		self:equals('<b>'..name..' (Sandbox)</b><br/><kbd><nowiki>'..case[1]..'</nowiki></kbd>',s._testline(case[1]),case[2],{nowiki=1})
	end
end

function p:test_lines_to_avoid()
	-- Various lines that should NOT match
	for name, wikitext in pairs(t_avoid) do
		self:equals('<b>'..name..'</b><br/><kbd><nowiki>'..wikitext..'</nowiki></kbd>',m._testline(wikitext),wikitext,{nowiki=1})
		self:equals('<b>'..name..' (Sandbox)</b><br/><kbd><nowiki>'..wikitext..'</nowiki></kbd>',s._testline(wikitext),wikitext,{nowiki=1})
	end
end

function p:test_same_line()
	-- Test passing a link with no line breaks 
	self:equals('same line',m.replaceLinksInUnorderedList('*[[hello]]'),'*[[hello]]',{nowiki=1})
end

function p:test_one_item_list()
	-- Test updating a single un-piped link in a list
	local supply = [==[List
*[[hello]]
]==]
	local expect = [==[List
* {{Annotated link |hello}}
]==]
	self:preprocess_equals('{{#invoke:ApplyLinkAnnotations|bySubstitutionInUnorderedList|'..supply..'}}', expect,{nowiki=1})
end

return p