--Go to the talk page to see the results of the tests
--Consider making sure that other modules that rely heavily on this module (E.g. [[Module:Template parameter value]]) also pass their testcases
local p = require('Module:UnitTests')

local frame = mw.getCurrentFrame()
local function preprocess(text, IsPrepared)
	local content = mw.text.unstrip(frame:preprocess(text))
	if IsPrepared then
		content = mw.text.decode(content)
	end
	return content
end

function p:_internal_test(module, name)
	--All PrepareText runs have keepComments set to true for testing convenience
	--Test conditions:
	--1) The escaped text should match our expectation for escaped text
	--2) The escaped text should always preprocess to the same as the original text (we only nowiki nowiki'd content)
	local PrepareTextTestN = 1
	local function TestPrepareText(...)
		local OriginalString = ""
		local ExpectedEscapedString = ""
		local args = {...}
		for i = 1, #args do
			OriginalString = OriginalString .. args[i]
			ExpectedEscapedString = ExpectedEscapedString .. (i%2 == 1 and args[i] or mw.text.nowiki(args[i]))
		end
		local ModuleEscapedString = module.PrepareText(OriginalString, true)
		self:equals(name.."PrepareText Escape Test "..PrepareTextTestN, ExpectedEscapedString, ModuleEscapedString, {nowiki=1})
		self:equals(name.."PrepareText Preprocess Test "..PrepareTextTestN, preprocess(OriginalString), preprocess(ModuleEscapedString, true), {nowiki=1})
		PrepareTextTestN = PrepareTextTestN + 1
	end
	
	--== PrepareText ==--
	--Standard comment escape
	TestPrepareText(
	--	R------, NW------, R-----
		"B<!--", "\nHey!", "-->A"
	)

	--Even when there's no end, the content inside is escaped
	TestPrepareText(
	--	R----------, NW----------, R------
		"{{Text|A<", "nowiki |}}", ">|B}}"
	)

	--A decently complex case
	TestPrepareText(
	--	R-------------, NW-----------, R--, NW------------, R---, NW------,
		"Hey!{{Text|<", "nowiki | ||", ">", "\nHey! }}\nA", "</", "nowiki",
	--	R-------, NW---------, R------------------------------
		">|<!--", "AAAAA|AAA", "-->Should see|Shouldn't see}}"
	)

	--Another decently complex case
	TestPrepareText(
	--	R---------------------------------------------------------------,
		"{{User:Aidan9382/templates/dummy\n|A|B|C {{{A|B}}} { } } {\n|<",
	--	NW------, R--, NW-, R---, NW------, R------, NW---, R--, NW-----, R---,
		"nowiki", ">", "D", "</", "nowiki", ">\n|<", "pre", ">", "E\n|F", "</",
	--	NW---, R-------------------------------------------------, NW------,
		"pre", ">\n|G|=|a=|A  =  [[{{PAGENAME}}|A=B]]{{Text|1==<", "nowiki",
	--	R--, NW--, R---, NW------, R------------------
		">", "}}", "</", "nowiki", ">}}|A B=Success}}"
	)
end

function p:test_live()
	p:_internal_test(require("Module:Wikitext Parsing"), "Live ")
end

function p:test_sandbox()
	p:_internal_test(require("Module:Wikitext Parsing/sandbox"), "Sandbox ")
end

-- Code for testing ParseTemplates that doesn't exactly work (not a primary feature so not a huge concern)
--[====[
	--Test conditions:
	--1) The parsed information matches the expected information
	local ParseTemplatesTestN = 1
	local function TestParseTemplates(OriginalString, ExpectedInformation)
		local ModuleInformation = module.ParseTemplates(OriginalString)[1]
		ModuleInformation.Children = nil --This isn't gonna be tested properly rn due to reasons
		mw.logObject(ModuleInformation)
		self:equals_deep(name.."ParseTemplates Test "..ParseTemplatesTestN, ExpectedInformation, ModuleInformation, {nowiki=1})
		ParseTemplatesTestN = ParseTemplatesTestN + 1
	end

	--== ParseTemplates ==--
	-- Just the one test since this feature isn't entirely developed
	local TestString = "{{User:Aidan9382/templates/dummy\n|A|B|C {{{A|B}}} { } "
		.. "} {\n|<nowiki>D</nowiki>\n|<pre>E\n|F</pre>\n|G|=|a=|A  =  [[{{PAGE"
		.. "NAME}}|A=B]]{{Text|1==<nowiki>}}</nowiki>}}|A B=Success}}"
	TestParseTemplates(
		"\nA"..TestString.."B\n",
		{
			Text = TestString, Name = "User:Aidan9382.templates/dummy",
			ArgOrder = {"1", "2", "3", "4", "5", "6", "", "a", "A", "A B"},
			Args = {
				["1"]="A", ["2"]="B", ["3"]="C {{{A|B}}} { } } {",
				["4"]="<nowiki>D</nowiki>", ["5"]="<pre>E\n|F</pre>",
				["6"]="G", [""]="", a="",
				A="[[{{PAGENAME}}|A=B]]{{Text|1==<nowiki>}}</nowiki>}}",
				["A B"]="Success"
			}
		}
	)
]====]

return p
"https://si.wikipedia.org/w/index.php?title=Module:Wikitext_Parsing/testcases&oldid=704258" වෙතින් සම්ප්‍රවේශනය කෙරිණි