Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Wikitext Parsing/testcases

From Wikipedia, the free encyclopedia
--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

return p