Edge Rewrite
// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a451f9b3ecdfdfff

Jump to content

Module:Sandbox/Varmkorv Tva

From Wikipedia, the free encyclopedia
local p;do
-- UnitTester provides unit testing for other Lua scripts. For details see [[Wikipedia:Lua#Unit testing]].
-- For user documentation see above.
local UnitTester = {}

-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = mw.loadData('Module:UnitTests/config')

local frame, tick, cross, should_highlight

local num_failures_sandbox = 0
local num_failures = 0
local num_runs = 0

local function first_difference(s1, s2)
	if not (type(s1) == "string" and type(s2) == "string") then
		return "N/A"
	elseif s1 == s2 then
		return ""
	end
	
	s1, s2 = tostring(s1), tostring(s2)
	if s1 == s2 then return '' end
	local max = math.min(#s1, #s2)
	for i = 1, max do
		if s1:sub(i, i) ~= s2:sub(i, i) then return i end
	end
	return max + 1
end

local function add_diff_highlighting(str)
	if mw.ustring.find(str, "%s") then
		return '<span style="background-color: var(--wikt-palette-red-4,pink);">' ..
			string.gsub(str, " ", "&nbsp;") .. '</span>'
	else
		return '<span style="color: var(--wikt-palette-red-9, red);">' ..
			str .. '</span>'
	end
end

local is_combining = require("Module:Unicode data").is_combining


local function find_noncombining(str, i, incr)
	while true do
		local ch = mw.ustring.sub(str, i, i)
		if ch == "" or not is_combining(mw.ustring.byte(ch)) then
			return i
		end
		i = i + incr
	end
end

-- Highlight character where a difference was found. Start highlight at first
-- non-combining character before the position. End it after the first non-
-- combining characters after the position. Can specify a custom highlighing
-- function.
local function highlight_difference(actual, expected, differs_at)
	if type(differs_at) ~= "number" or not (actual and expected) then
		return actual
	end
	differs_at = find_noncombining(expected, differs_at, -1)
	local i = find_noncombining(actual, differs_at, -1)
	local j = find_noncombining(actual, differs_at + 1, 1)
	j = j - 1
	return mw.ustring.sub(actual, 1, i - 1) ..
		add_diff_highlighting(mw.ustring.sub(actual, i, j)) ..
		mw.ustring.sub(actual, j + 1, -1)
end

local function return_varargs(...)
	return ...
end

-- Return the header for the result table along with the number of columns in the table.
function UnitTester:new_result_table()
	local header_row = mw.html.create("tr")
		:tag("th")
			:attr("class", "unit-tests-img-corner")
			:done()
	
	local columns = {}
	table.insert(columns, cfg.testString)
	if self.live_sandbox then
		table.insert(columns, cfg.liveString)
		table.insert(columns, cfg.sandboxString)
		table.insert(columns, cfg.expectedString)
	else
		table.insert(columns, cfg.expectedString)
		table.insert(columns, cfg.actualString)
	end
	
	if self.differs_at then
		table.insert(columns, cfg.differsString)
	end
	
	for _, cell in ipairs(columns) do
		header_row:tag("th")
			:wikitext(cell)
	end
	
	self.columns = #columns + 1
	
	return mw.html.create("table")
		:attr("class", "wikitable unit-tests-result")
		:node(header_row)
end

function UnitTester:calculate_output(text, expected, actual, options)
	-- Set up some variables for throughout for ease
	num_runs = num_runs + 1
	options = options or {}

	-- Fix any stripmarkers if asked to do so to prevent incorrect fails
	local compared_expected = expected
	local compared_actual = actual
	if options.templatestyles then
		local pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-)(%x+)(%-QINU[^\127]*\127)'
		local _, expected_stripmarker_id = compared_expected:match(pattern)				-- when module rendering has templatestyles strip markers, use ID from expected to prevent false test fail
		if expected_stripmarker_id then
			compared_actual = compared_actual:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')	-- replace actual id with expected id; ignore second capture in pattern
			compared_expected = compared_expected:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')		-- account for other strip markers
		end
	end
	if options.stripmarker then
		local pattern = '(\127[^\127]*UNIQ%-%-%l+%-)(%x+)(%-%-?QINU[^\127]*\127)'
		local _, expected_stripmarker_id = compared_expected:match(pattern)
		if expected_stripmarker_id then
			compared_actual = compared_actual:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
			compared_expected = compared_expected:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
		end
	end

	-- Perform the comparison
	local success = compared_actual == compared_expected
	if options.noexpectation and compared_expected == '' then
		success = true
	end
	if not success then
		num_failures = num_failures + 1
	end

	-- Sort the wikitext for displaying the results
	if options.combined then
		-- We need 2 rows available for the expected and actual columns
		-- Top one is parsed, bottom is unparsed
		local differs_at, differs_at_nowiki = nil, nil
		if self.differs_at then
			differs_at = first_difference(compared_expected, compared_actual)
			if type(differs_at) == "number" then
				differs_at_nowiki = first_difference(mw.text.nowiki(expected), mw.text.nowiki(actual))
			end
		end
		local cells_row1 = {
			(expected),
			(highlight_difference(actual, expected, differs_at))
		}
		local cells_row2 = {
			(mw.text.nowiki(expected)),
			(highlight_difference(mw.text.nowiki(actual), mw.text.nowiki(actual), differs_at_nowiki))
		}
		local row = mw.html.create("tr")
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
		
		-- Local copies of tick/cross to allow for highlighting
		local highlight = row:tag("td")
			:attr("rowspan", 2)
			:wikitext(success and tick or cross)
		
		if should_highlight and not success then
			highlight:css("background", "#fc0")
		end
		row:tag("td")
			:attr("rowspan", 2)
			:wikitext(mw.text.nowiki(text))

		for _, cell in ipairs(cells_row1) do
			row:tag("td")
				:wikitext(cell)
		end
		
		if differs_at then
			row:tag("td")
				:attr("rowspan", 2)
				:wikitext(differs_at)
		end

		local row2 = row:tag('tr')
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
		
		for _, cell in ipairs(cells_row2) do
			row2:tag("td")
				:wikitext(cell)
		end
			
		self.result_table:node(row)
	else
		-- Display normally with whichever option was preferred (nowiki/parsed)
		local differs_at = nil
		local formatting = options.nowiki and mw.text.nowiki or return_varargs
		local formated_expected, formated_actual = formatting(expected), formatting(actual)
		if self.differs_at then
			differs_at = first_difference(compared_expected, compared_actual)
			if type(differs_at) == "number" then
				formated_actual = highlight_difference(formated_actual, formated_expected, first_difference(formated_actual, formated_expected))
			end
		end
		local cells = {
			success and tick or cross,
			text,
			formated_expected,
			formated_actual,
			differs_at
		}
		local row = mw.html.create("tr")
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
	
		for i, cell in ipairs(cells) do
			local col = row:tag("td")
				:wikitext(cell)
			if i == 1 and should_highlight and not success then
				col:css("background", "#fc0")
			end
		end
		
		self.result_table:node(row)
	end
end

function UnitTester:preprocess_equals(text, expected, options)
	local actual = frame:preprocess(text)
	self:calculate_output(text, expected, actual, options)
end

function UnitTester:preprocess_equals_many(prefix, suffix, cases, options)
	for _, case in ipairs(cases) do
		self:preprocess_equals(prefix .. case[1] .. suffix, case[2], options)
	end
end

function UnitTester:preprocess_equals_preprocess(text1, text2, options)
	local actual = frame:preprocess(text1)
	local expected = frame:preprocess(text2)
	self:calculate_output(text1, expected, actual, options)
end

function UnitTester:preprocess_equals_compare(live, sandbox, expected, options)
	options = options or {}
	local live_text = frame:preprocess(live)
	local sandbox_text = frame:preprocess(sandbox)
	local highlight_live = false
	local highlight_sandbox = false
	num_runs = num_runs + 1
	local compared_live = live_text
	local compared_sandbox = sandbox_text
	local compared_expected = expected
	if options.templatestyles then
		local pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-)(%x+)(%-QINU[^\127]*\127)'
		local _, expected_stripmarker_id = compared_expected:match(pattern)				-- when module rendering has templatestyles strip markers, use ID from expected to prevent false test fail
		if expected_stripmarker_id then
			compared_live = compared_live:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')	-- replace actual id with expected id; ignore second capture in pattern
			compared_sandbox = compared_sandbox:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')	-- replace actual id with expected id; ignore second capture in pattern
			compared_expected = compared_expected:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')		-- account for other strip markers
		end
	end
	if options.stripmarker then
		local pattern = '(\127[^\127]*UNIQ%-%-%l+%-)(%x+)(%-%-?QINU[^\127]*\127)'
		local _, expected_stripmarker_id = compared_expected:match(pattern)
		if expected_stripmarker_id then
			compared_live = compared_live:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
			compared_sandbox = compared_sandbox:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
			compared_expected = compared_expected:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
		end
	end
	local success = compared_live == compared_expected and compared_sandbox == compared_expected
	if not success then
		if compared_live ~= compared_expected then
			num_failures = num_failures + 1
			highlight_live = true
		end

		if compared_sandbox ~= compared_expected then
			num_failures_sandbox = num_failures_sandbox + 1
			highlight_sandbox = true
		end
	end

	-- Sort the wikitext for displaying the results
	if options.combined then
		-- We need 2 rows available for the expected, live, and sandbox columns
		-- Top one is parsed, bottom is unparsed
		local differs_at = ''
		local diff_live, diff_sandbox = nil,nil
		local diff_live_nowiki, diff_sandbox_nowiki = nil, nil
		if self.differs_at then
			diff_live = first_difference(compared_expected, compared_live)
			diff_sandbox = first_difference(compared_expected, compared_sandbox)
			differs_at = (diff_live or diff_sandbox)
			if type(diff_live) == "number" then
				diff_live_nowiki = first_difference(mw.text.nowiki(expected), mw.text.nowiki(live_text))
			end
			if type(diff_sandbox) == "number" then
				diff_sandbox_nowiki = first_difference(mw.text.nowiki(expected), mw.text.nowiki(sandbox_text))
			end
		end

		local cells_row1 = {
			(highlight_difference(live_text, expected, diff_live)),
			(highlight_difference(sandbox_text, expected, diff_sandbox)),
			(expected),
		}
		local cells_row2 = {
			(highlight_difference(mw.text.nowiki(live_text), mw.text.nowiki(expected), diff_live_nowiki)),
			(highlight_difference(mw.text.nowiki(sandbox_text), mw.text.nowiki(expected), diff_sandbox_nowiki)),
			(mw.text.nowiki(expected)),
		}
		local row = mw.html.create("tr")
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
	
		row:tag("td")
			:attr("rowspan", 2)
			:wikitext((not highlight_live and tick or cross) .. (not highlight_sandbox and tick or cross))
		row:tag("td")
			:attr("rowspan", 2)
			:wikitext(mw.text.nowiki(live))

		for i, cell in ipairs(cells_row1) do
			local col = row:tag("td")
				:wikitext(cell)
			if i == 1 and highlight_live and should_highlight then
				col:css("background", "#fc0")
			elseif i == 2 and highlight_sandbox and should_highlight then
				col:css("background", "#fc0")
			end
		end
		
		if differs_at ~= '' then
			row:tag("td")
				:attr("rowspan", 2)
				:wikitext(differs_at)
		end

		local row2 = row:tag('tr')
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
		
		for i, cell in ipairs(cells_row2) do
			local col = row2:tag("td")
				:wikitext(cell)
			if i == 1 and highlight_live and should_highlight then
				col:css("background", "#fc0")
			elseif i == 2 and highlight_sandbox and should_highlight then
				col:css("background", "#fc0")
			end
		end
		self.result_table:node(row)
	else
		-- Display normally with whichever option was preferred (nowiki/parsed)
		local differs_at = ''
		local formatting = options.nowiki and mw.text.nowiki or return_varargs
		local formated_expected, formated_live, formated_sandbox = formatting(expected), formatting(live_text), formatting(sandbox_text)
		if self.differs_at then
			local diff = first_difference(compared_expected, live_text)
			local diff2 = first_difference(compared_expected, sandbox_text)
			differs_at = ' \n| ' .. (diff or diff2)
			if type(diff) == "number" then
				formated_live = highlight_difference(formated_live, formated_expected, first_difference(formated_live, formated_expected))
			end
			if type(diff2) == "number" then
				formated_sandbox = highlight_difference(formated_sandbox, formated_expected, first_difference(formated_sandbox, formated_expected))
			end
		end
		local cells = {
			(not highlight_live and tick or cross) .. (not highlight_sandbox and tick or cross), 
			mw.text.nowiki(live),
			formated_live,
			formated_sandbox,
			formated_expected,
			differs_at ~= '' and differs_at or nil
		}
		local row = mw.html.create("tr")
			:attr("class", success and "unit-test-pass" or "unit-test-fail")
		
		for i, cell in ipairs(cells) do
			local col = row:tag("td")
				:wikitext(cell)
			if i == 3 and highlight_live and should_highlight then
				col:css("background", "#fc0")
			elseif i == 4 and highlight_sandbox and should_highlight then
				col:css("background", "#fc0")
			end
		end
		
		self.result_table:node(row)
	end
end

function UnitTester:preprocess_equals_preprocess_many(prefix1, suffix1, prefix2, suffix2, cases, options)
	for _, case in ipairs(cases) do
		self:preprocess_equals_preprocess(prefix1 .. case[1] .. suffix1, prefix2 .. (case[2] and case[2] or case[1]) .. suffix2, options)
	end
end

function UnitTester:preprocess_equals_sandbox_many(module, function_name, cases, options)
	for _, case in ipairs(cases) do
		local live = module .. '|' .. function_name .. '|' .. case[1] .. '}}'
		local sandbox = module .. '/sandbox|' .. function_name .. '|' .. case[1] .. '}}'
		self:preprocess_equals_compare(live, sandbox, case[2], options)
	end
end

function UnitTester:equals(name, actual, expected, options)
	num_runs = num_runs + 1
	local success = actual == expected
	local formatting = (options and options.nowiki and mw.text.nowiki) or return_varargs
	local differs_at = self.differs_at and (first_difference(expected, actual))
	local display = options and options.display or return_varargs
	local cells = {
			success and tick or cross,
			name,
			formatting(tostring(display(expected))),
			formatting(tostring(display(actual))),
			differs_at
	}
	local row = mw.html.create("tr")
		:attr("class", success and "unit-test-pass" or "unit-test-fail")
	if not success then
		num_failures = num_failures + 1
	end
		
	for i, cell in ipairs(cells) do
		local col = row:tag("td")
			:wikitext(cell)
		if i == 1 and should_highlight and not success then
			col:css("background", "#fc0")
		end
	end
	self.result_table:node(row)
end

local deep_compare; do
	--//pulled from [[wikt:Module:table/deepEquals]]
	local function is_eq(a, b, seen, include_mt)
		-- If `a` and `b` have been compared before, return the memoized result. This will usually be true, since failures normally fail the whole check outright, but match failures are tolerated during the laborious check without this happening, since it compares key/value pairs until it finds a match, so it could be false.
		local memo_a = seen[a]
		if memo_a then
			local result = memo_a[b]
			if result ~= nil then
				return result
			end
			-- To avoid recursive references causing infinite loops, assume the tables currently being compared are equivalent by memoizing the comparison as true; this will be corrected to false if there's a match failure.
			memo_a[b] = true
		else
			memo_a = {[b] = true}
			seen[a] = memo_a
		end
		-- Don't bother checking `memo_b` for `a`, since if `a` and `b` had been compared before then `b` would be in `memo_a`, but it isn't.
		local memo_b = seen[b]
		if memo_b then
			memo_b[a] = true
		else
			memo_b = {[a] = true}
			seen[b] = memo_b
		end
		-- If `include_mt` is set, check the metatables are equivalent.
		if include_mt then
			local mt_a, mt_b = getmetatable(a), getmetatable(b)
			if not (mt_a == mt_b or type(mt_a) == "table" and type(mt_b) == "table" and is_eq(mt_a, mt_b, seen, true)) then
				memo_a[b], memo_b[a] = false, false
				return false
			end
		end
		-- Copy all key/values pairs in `b` to `remaining_b`, and count the size: this uses `pairs`, which will also be used to iterate over `a`, ensuring that `a` and `b` are iterated over using the same iterator. This is necessary to ensure that `deepEquals(a, b)` and `deepEquals(b, a)` always give the same result. Simply iterating over `a` while accessing keys in `b` for comparison would ignore any `__pairs` metamethod that `b` has, which could cause asymmetrical outputs if `__pairs` returns more or less than the complete set of key/value pairs accessible via `__index`, so using `pairs` for both `a` and `b` prevents this.
		-- TODO: handle exotic `__pairs` methods which return the same key multiple times with different values.
		local remaining_b, size_b = {}, 0
		for k_b, v_b in pairs(b) do
			remaining_b[k_b], size_b = v_b, size_b + 1
		end
		-- Fast check: iterate over the keys in `a`, checking if an equivalent value exists at the same key in `remaining_b`. As matches are found, key/value pairs are removed from `remaining_b`. If any keys in `a` or `remaining_b` are tables, the fast check will only work if the exact same object exists as a key in the other table. Any others from `a` that don't match anything in `remaining_b` are added to `remaining_a`, while those in `remaining_b` that weren't found will still remain once the loop ends. `remaining_a` and `remaining_b` are then compared at the end with the laborious check.
		local size_a, remaining_a = 0
		for k, v_a in pairs(a) do
			local v_b = remaining_b[k]
			-- If `k` isn't in `remaining_b`, `a` and `b` can't be equivalent unless it's a table.
			if v_b == nil then
				if type(k) ~= "table" then
					memo_a[b], memo_b[a] = false, false
					return false
				-- Otherwise, add the `k`/`v_a` pair to `remaining_a` for the laborious check.
				elseif not remaining_a then
					remaining_a = {}
				end
				remaining_a[k], size_a = v_a, size_a + 1
			-- Otherwise, if `k` exists in `a` and `remaining_b`, `v_a` and `v_b` must be equivalent for there to be a match.
			elseif v_a == v_b or type(v_a) == "table" and type(v_b) == "table" and is_eq(v_a, v_b, seen, include_mt) then
				remaining_b[k], size_b = nil, size_b - 1
			else
				memo_a[b], memo_b[a] = false, false
				return false
			end
		end
		-- Must be the same number of remaining keys in each table.
		if size_a ~= size_b then
			memo_a[b], memo_b[a] = false, false
			return false
		-- If the size is 0, there's nothing left to check.
		elseif size_a == 0 then
			return true
		end
		-- Laborious check: since it's not possible to use table lookups to check if two keys are equivalent when they're tables, check each key/value pair in `remaining_a` against every key/value pair in `remaining_b` until a match is found, removing the matching key/value pair from `remaining_b` each time, to ensure one-to-one equivalence.
		for k_a, v_a in next, remaining_a do
			local success
			for k_b, v_b in next, remaining_b do
				-- Keys/value pairs must be equivalent in order to match.
				if ( -- More efficient to compare the values first, as they might not be tables.
					(v_a == v_b or type(v_a) == "table" and type(v_b) == "table" and is_eq(v_a, v_b, seen, include_mt)) and
					(k_a == k_b or type(k_a) == "table" and type(k_b) == "table" and is_eq(k_a, k_b, seen, include_mt))
				) then
					-- Remove matched key from `remaining_b`, and break the inner loop.
					success, remaining_b[k_b] = true, nil
					break
				end
			end
			-- Fail if `remaining_b` runs out of keys, as the `k_a`/`v_a` pair still hasn't matched.
			if not success then
				memo_a[b], memo_b[a] = false, false
				return false
			end
		end
		-- If every key/value pair in `remaining_a` matched with one in `remaining_b`, `a` and `b` must be equivalent. Note that `remaining_b` will now be empty, since the laborious check only starts if `remaining_a` and `remaining_b` are the same size.
		return true
	end
	deep_compare = function(a, b, ignore_mt)
		-- Do simple checks before calling is_eq to avoid generating an unnecessary memo.
		-- Simple equality check and type check; if not a ~= b, a and b can only be equivalent if they're both tables.
		return a == b or type(a) == "table" and type(b) == "table" and is_eq(a, b, {}, not ignore_mt)
	end
end

local function val_to_str(obj)
	local function table_key_to_str(k)
		if type(k) == 'string' and mw.ustring.match(k, '^[_%a][_%a%d]*$') then
			return k
		else
			return '[' .. val_to_str(k) .. ']'
		end
	end

	if type(obj) == 'string' then
		obj = mw.ustring.gsub(obj, '\n', '\\n')
		if mw.ustring.match(mw.ustring.gsub(obj, '[^\'"]', ''), '^"+$') then
			return "'" .. obj .. "'"
		end
		return '"' .. mw.ustring.gsub(obj, '"', '\\"' ) .. '"'
	elseif type(obj) == 'table' then
		local result, checked = {}, {}
		for k, v in ipairs(obj) do
			table.insert(result, val_to_str(v))
			checked[k] = true
		end
		for k, v in pairs(obj) do
			if not checked[k] then
				table.insert(result, table_key_to_str(k) .. '=' .. val_to_str(v))
			end
		end
		return '{' .. table.concat(result, ',') .. '}'
	else
		return tostring(obj)
	end
end
local function insert_differences(keys, t1, t2)
	for k, v1 in pairs(t1) do
		local v2 = t2[k]
		if v2 == nil or not deep_compare(v1, v2, true) then
			table.insert(keys, k)
		end
	end
end

local function get_differing_keys(t1, t2)
	local ty1 = type(t1)
	if not (ty1 == type(t2) and ty1 == "table") then
		return nil
	end
	
	local keys = {}
	insert_differences(keys, t1, t2)
	insert_differences(keys, t2, t1)

	return keys
end

local function extract_keys(t, keys)
	if not keys then
		return t
	end
	local new_t = {}
	for _, key in ipairs(keys) do
		new_t[key] = t[key]
	end
	return new_t
end

function UnitTester:equals_deep(name, actual, expected, options)
	num_runs = num_runs + 1
	local actual_str, expected_str
	local success = deep_compare(actual, expected, true)
	if success then
		if options and options.show_table_difference then
			actual_str = ''
			expected_str = ''
		end
	else
		if options and options.show_table_difference then
			local keys = get_differing_keys(actual, expected)
			actual_str = val_to_str(extract_keys(actual, keys))
			expected_str = val_to_str(extract_keys(expected, keys))
		end
	end
	if (not options) or not options.show_table_difference then
		actual_str = val_to_str(actual)
		expected_str = val_to_str(expected)
	end
	local formatting = (options and options.nowiki and mw.text.nowiki) or return_varargs
	local differs_at = self.differs_at and (' \n| ' .. first_difference(expected_str, actual_str)) or ''
	local cells = {
			success and tick or cross,
			name,
			formatting(formatting(expected_str)),
			formatting(formatting(actual_str)),
			differs_at
	}
	local row = mw.html.create("tr")
		:attr("class", success and "unit-test-pass" or "unit-test-fail")
	
	if not success then
		num_failures = num_failures + 1
	end
		
	for _, cell in ipairs(cells) do
		row:tag("td")
			:wikitext(cell)
	end
	self.result_table:node(row)
end

function UnitTester:iterate(examples, func)
	require 'libraryUtil'.checkType('iterate', 1, examples, 'table')
	if type(func) == 'string' then
		func = self[func]
	elseif type(func) ~= 'function' then
		error(("bad argument #2 to 'iterate' (expected function or string, got %s)")
			:format(type(func)), 2)
	end

	for i, example in ipairs(examples) do
		if type(example) == 'table' then
			func(self, unpack(example))
		elseif type(example) == 'string' then
			self:heading(example)
		else
			error(('bad example #%d (expected table, got %s)')
				:format(i, type(example)), 2)
		end
	end
end

function UnitTester:heading(text)
	local prefix, maintext = mw.ustring.match(text, '^#(h[0-9]+):(.*)$')
	if not prefix then
		maintext = text
	end
	
	local header = mw.html.create("th")
		:attr("colspan", self.columns)
	
	if prefix == "h1" then
		header:css("text-align", "center")
			:css("font-size", "150%")
	else
		header:css("text-align", "left")
	end
	
	header:wikitext(maintext)
	
	self.result_table:tag("tr")
		:node(header)
		:done()
end

local function err_handler(err) 
	return {
		error = err, 
		trace = debug.traceback()
	} 
end

function UnitTester:runTest(name, test)
	self.result_table = self:new_result_table()
	self.result_table:tag("h2")
			:wikitext(string.format('%s\n', name))
	self.result_table:tag("caption")
			:wikitext(name)
	local success, details = xpcall(function() test(self) end, err_handler)
	if not success then
		num_failures = num_failures + 1
		num_runs = num_runs + 1
		
		self.result_table:tag("tr")
			:tag("td")
				:wikitext(cross)
				:done()
			:tag("td")
				:wikitext(name)
				:done()
			:tag("td")
				:attr("rowspan", 2)
				:attr("colspan", self.columns - 2)
				:css("text-align", "left")
				:tag("strong")
					:attr("class", "error")
					:wikitext("Lua error during testing -- " .. mw.text.nowiki(details.error))
					:done()
				:wikitext(frame:extensionTag("pre", details.trace or "(no traceback)"))
	end
	return tostring(self.result_table)
end

function UnitTester:run(frame_arg)
	frame = frame_arg or mw.getCurrentFrame()
	self.frame = frame
	self.differs_at = frame.args['differs_at']
	self.live_sandbox = frame.args['live_sandbox']
	tick = frame:preprocess(cfg.successIndicator)
	cross = frame:preprocess(cfg.failureIndicator)

	if frame.args.highlight then
		should_highlight = true
	end

	self.columns = 4
	if self._tests._last then
		table.insert(self._tests, self._tests._last)
		self._tests._last = nil
	end
	local outputs = {}
	-- Add results to the results table.
	for _, testDetails in ipairs(self._tests) do
		local result_table = self:runTest(testDetails.name, testDetails.test)
		table.insert(outputs, result_table)
	end
	local refresh_link = tostring(mw.uri.fullUrl(mw.title.getCurrentTitle().fullText, 'action=purge&forcelinkupdate=1'))
	local failCat = cfg.failureCategory
	if self.noCat then
		failCat = ''
	end
	if self.live_sandbox then
		local live, sandbox
		if num_runs == 0 then
			live = cfg.noTestsRunSummary
		elseif num_failures ~= 0 then
			live = frame:preprocess(mw.message.newRawMessage(cfg.liveFailureSummary, num_runs, num_failures):plain())
		else
			live = frame:preprocess(mw.message.newRawMessage(cfg.liveSuccessSummary, num_runs):plain())
		end
		if num_runs == 0 then
			sandbox = ''
		elseif num_failures_sandbox ~= 0 then
			sandbox = frame:preprocess(
					mw.message.newRawMessage(cfg.sandboxFailureSummary, num_runs, num_failures_sandbox):plain()
				)
		else
			sandbox = frame:preprocess(
					mw.message.newRawMessage(cfg.sandboxSuccessSummary, num_runs):plain()
				)
		end
		local cat = (num_failures ~= 0 or num_failures_sandbox ~= 0) and 
			(failCat .. " <span class='plainlinks unit-tests-refresh'>[" .. refresh_link .. " (refresh)]</span>") or ''
		return live .. ' ' .. sandbox .. cat .. '\n\n' .. table.concat(outputs)
	else
		local msg;
		if num_runs == 0 then
			msg = cfg.noTestsRunSummary
		elseif num_failures ~= 0 then
			msg = frame:preprocess(mw.message.newRawMessage(cfg.failureSummary, num_runs, num_failures):plain())  
				.. failCat
				.. " <span class='plainlinks unit-tests-refresh'>[" .. refresh_link .. " (refresh)]</span>"
		else
			msg = frame:preprocess(mw.message.newRawMessage(cfg.successSummary, num_runs):plain())
		end
		return msg .. '\n\n' .. table.concat(outputs)
		
	end
end

-- Set up metatable
UnitTester.__meta = {}
UnitTester.__meta.__index = UnitTester
function UnitTester.__meta:__newindex(key, value)
	if type(key) == 'string' and key:find('^test') and type(value) == 'function' then
		-- Store test functions in the order they were defined
		if key == self.runLast or key == rawget(_G, "runLast") then
			self._tests._last = {name = key, test = value}
		else
			table.insert(self._tests, {name = key, test = value})
		end
	else
		rawset(self, key, value)
	end
end

function UnitTester.new()
	local o = {}
	o.runLast = '' -- puts a test at the end of the page
	o._tests = {}
	setmetatable(o, UnitTester.__meta)
	function o.run_tests(frame_arg) return o:run(frame_arg) end
	return o
end
p = UnitTester.new()
end

--local p = UnitTester.new()
p.noCat = true

function p:test_01_icon()
	self:preprocess_equals_sandbox_many('{{#invoke:Adjacent stations', 'rail icon', {
		-- Icon for a system with an icon. Use <system icon>.
		{'system=testcases/Test module', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},
		{'testcases/Test module', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},

		-- Icon for a system without an icon. Show no icon.
		{'system=San Diego Trolley', ''}, --TODO: use test module instead.
		{'San Diego Trolley', ''}, --TODO: use test module instead.

		-- Icon for a line with an icon. Use <line icon>.
		{'system=testcases/Test module|line=line with title', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line with title', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type without icon, belonging to a line with an icon. Use <line icon>.
		{'system=testcases/Test module|line=line with linked title and type|type=type with title', '[[File:Taoyuan Metro Line Airport.svg|18px|link=Taoyuan Airport MRT]]'},
		{'testcases/Test module|line with linked title and type|type with title', '[[File:Taoyuan Metro Line Airport.svg|18px|link=Taoyuan Airport MRT]]'},

		-- Icon for a type with an icon, belonging to a line with an icon. Use <type icon>.
		{'system=testcases/Test module|line=line with linked title and type|type=icon type', '[[File:Logo Blue Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line with linked title and type|icon type', '[[File:Logo Blue Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type with an icon, belonging to a line without an icon. Use <type icon>.
		{'system=testcases/Test module|line=line without icon with type|type=type with icon', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line without icon with type|type with icon', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type without an icon, belonging to a line without an icon. Use <system icon>.
		{'system=testcases/Test module|line=line without icon with type|type=type without icon', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},
		{'testcases/Test module|line without icon with type|type without icon', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},

		-- _default icon for lines. --TODO: add expected.
		{'Antwerp tram|1', '[[File:DeLijn icon 1antw.svg|16px|alt=]]'}, --TODO: use test module instead.
	
		-- _default icon for lines using alias. --TODO: add expected.
		{'Antwerp tram|line 1', '[[File:DeLijn icon 1antw.svg|16px|alt=]]'}, --TODO: use test module instead.

		-- Override _default icon for lines with icon specified. --TODO: add expected.
		{'São Paulo Metropolitan Trains|13-AE', '[[File:Cptm 13.svg|16px|alt=]]'}, --TODO: use test module instead.

		-- Sole line default. --TODO: add expected.
		{'Taiwan High Speed Rail', '[[File:Taiwan High Speed Rail Logo(Log Only).svg|18px|link=Taiwan High Speed Rail|alt=Taiwan High Speed Rail]]'}, --TODO: use test module instead.

		-- Override icon for line when "icon format" is set. --TODO: add expected.
		{'Brampton Transit|Brampton Transit', ''}, --TODO: use test module instead.
	
    }, {combined=true})
end

function p:test_02_icon()
	self:preprocess_equals_sandbox_many('{{#invoke:Adjacent stations', 'rail icon', {
		-- Icon for a system with an icon. Use <system icon>.
		{'system=testcases/Test module', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},
		{'testcases/Test module', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},

		-- Icon for a system without an icon. Show no icon.
		{'system=San Diego Trolley', ''}, --TODO: use test module instead.
		{'San Diego Trolley', ''}, --TODO: use test module instead.

		-- Icon for a line with an icon. Use <line icon>.
		{'system=testcases/Test module|line=line with title', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line with title', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type without icon, belonging to a line with an icon. Use <line icon>.
		{'system=testcases/Test module|line=line with linked title and type|type=type with title', '[[File:Taoyuan Metro Line Airport.svg|18px|link=Taoyuan Airport MRT]]'},
		{'testcases/Test module|line with linked title and type|type with title', '[[File:Taoyuan Metro Line Airport.svg|18px|link=Taoyuan Airport MRT]]'},

		-- Icon for a type with an icon, belonging to a line with an icon. Use <type icon>.
		{'system=testcases/Test module|line=line with linked title and type|type=icon type', '[[File:Logo Blue Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line with linked title and type|icon type', '[[File:Logo Blue Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type with an icon, belonging to a line without an icon. Use <type icon>.
		{'system=testcases/Test module|line=line without icon with type|type=type with icon', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},
		{'testcases/Test module|line without icon with type|type with icon', '[[File:Logo Green Line (San Diego Trolley).svg|16px]]'},

		-- Icon for a type without an icon, belonging to a line without an icon. Use <system icon>.
		{'system=testcases/Test module|line=line without icon with type|type=type without icon', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},
		{'testcases/Test module|line without icon with type|type without icon', '[[File:HK MTR logo (PD).svg|14px|link=MTR|alt=MTR]]'},

		-- _default icon for lines. --TODO: add expected.
		{'Antwerp tram|1', '[[File:DeLijn icon 1antw.svg|16px|alt=]]'}, --TODO: use test module instead.
	
		-- _default icon for lines using alias. --TODO: add expected.
		{'Antwerp tram|line 1', '[[File:DeLijn icon 1antw.svg|16px|alt=]]'}, --TODO: use test module instead.

		-- Override _default icon for lines with icon specified. --TODO: add expected.
		{'São Paulo Metropolitan Trains|13-AE', '[[File:Cptm 13.svg|16px|alt=]]'}, --TODO: use test module instead.

		-- Sole line default. --TODO: add expected.
		{'Taiwan High Speed Rail', '[[File:Taiwan High Speed Rail Logo(Log Only).svg|18px|link=Taiwan High Speed Rail|alt=Taiwan High Speed Rail]]'}, --TODO: use test module instead.

		-- Override icon for line when "icon format" is set. --TODO: add expected.
		{'Brampton Transit|Brampton Transit', ''}, --TODO: use test module instead.
	
    }, {combined=true})
end

function p:test_05_errors_maybe()
	self:preprocess_equals_sandbox_many('{{#invoke:Adjacent stations', 'rail icon', {

		--[[
		
		Tests for a type of a line without a title.
		
		]]--
	--TODO: update type line names
		-- Icon for a type with "icon format" = "link".
		{'system=testcases/Test module|line=line without title with type|type=link type', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},
		{'testcases/Test module|line without title with type|link type', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},

		-- Icon for a type with "icon format" = "route".
		{'system=testcases/Test module|line=line without title with type|type=route type', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route title</span>]]</span>'},
		{'testcases/Test module|line without title with type|route type', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route title</span>]]</span>'},

		-- Icon for a type with "icon format" = "croute".
		{'system=testcases/Test module|line=line without title with type|type=croute type', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute title</span>]]</span>'},
		{'testcases/Test module|line without title with type|croute type', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute title</span>]]</span>'},

		-- Icon for a type with "icon format" = "xroute".
		{'system=testcases/Test module|line=line without title with type|type=xroute type', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute title</span>]]</span>'},
		{'testcases/Test module|line without title with type|xroute type', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute title</span>]]</span>'},

		-- Icon for a type with "icon format" = "legend".		
		{'system=testcases/Test module|line=line without title with type|type=legend type', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – legend title</div>'},
		{'testcases/Test module|line without title with type|legend type', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – legend title</div>'},

		-- Icon for a type with "icon format" = "link" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=link type with linked title and short name', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},
		{'testcases/Test module|line without title with type|link type with linked title and short name', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},

		-- Icon for a type with "icon format" = "route" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=route type with linked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route type linked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|route type with linked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route type linked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "croute" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=croute type with linked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute type linked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|croute type with linked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute type linked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "xroute" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=xroute type with linked title and short name', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute type linked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|xroute type with linked title and short name', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute type linked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "legend" and a short name.		
		{'system=testcases/Test module|line=line without title with type|type=legend type with linked title and short name', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – [[501 Züm Queen]]</div>'},
		{'testcases/Test module|line without title with type|legend type with linked title and short name', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – [[501 Züm Queen]]</div>'},

		-- Icon for a type with "icon format" = "link" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=link type with unlinked title and short name', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},
		{'testcases/Test module|line without title with type|link type with unlinked title and short name', '[[Taoyuan Airport MRT|<span style="color:inherit;background-color:#ff0000;border:1px solid #000">    </span>]]'},

		-- Icon for a type with "icon format" = "route" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=route type with unlinked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route type unlinked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|route type with unlinked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">route type unlinked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "croute" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=croute type with unlinked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute type unlinked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|croute type with unlinked title and short name', '<span style="color:inherit;background-color:#ff0000;border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#FFFFFF;font-weight:bold;font-size:inherit;white-space:nowrap">croute type unlinked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "xroute" and a short name.
		{'system=testcases/Test module|line=line without title with type|type=xroute type with unlinked title and short name', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute type unlinked short name</span>]]</span>'},
		{'testcases/Test module|line without title with type|xroute type with unlinked title and short name', '<span style="border:.075em solid #ff0000;border-radius:.5em;padding:0 .3em">[[Taoyuan Airport MRT|<span style="color:#ff0000;font-weight:bold;font-size:inherit;white-space:nowrap">xroute type unlinked short name</span>]]</span>'},

		-- Icon for a type with "icon format" = "legend" and a short name.		
		{'system=testcases/Test module|line=line without title with type|type=legend type with unlinked title and short name', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – 501 Züm Queen</div>'},
		{'testcases/Test module|line without title with type|legend type with unlinked title and short name', '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;color:inherit;background-color:#ff0000"> </span> [[Taoyuan Airport MRT]] – 501 Züm Queen</div>'},

    }, {combined=true})
end

function p:test_hello()
	self:preprocess_equals('{{#invoke:Example | hello}}', 'Hello World!', {combined = true})
end

function p:test_error()
	self:preprocess_equals('{{#invoke:Example | hello}}', 'Hello World!', {combined = true})
	error("this is a Blatant error")
end
local frame0 = mw.getCurrentFrame()
local frame0_getTitle = frame0:getTitle()
local getCurrentFrame_getTitle = mw.getCurrentFrame():getTitle()
local arg = ...

function p._test(frame)
	return
		"# " .. frame:getTitle() .. "\n" ..
		"# " .. frame0:getTitle() .. "\n" ..
		"# " .. frame0_getTitle .. "\n" ..
		"# " .. getCurrentFrame_getTitle .. "\n" ..
		"# " .. mw.dumpObject(arg) .. "\n" ..
		""
end

function p._test0(frame)
	return
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		"# nil\n" ..
	""
end

function p.preprocess(frame)
	--return frame.args[1]
	return frame:preprocess(frame.args[1])
end

function p.p1(frame)
	return frame:preprocess('{{#invoke:UnitTests/testcases | _test}}')
end

function p.p2(frame)
	return frame:preprocess('{{#invoke:UnitTests/testcases/frame | _test}}')
end

function p:test()
	local testcases_expected_result = "" ..
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		"# Module:UnitTests/testcases\n" ..
		""
	self:preprocess_equals('{{#invoke:UnitTests/testcases | _test}}', "\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"", {combined = true})
	self:preprocess_equals('{{#invoke:UnitTests/testcases | _test}}', "\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"", {nowiki=true})
	self:preprocess_equals('{{#invoke:UnitTests/testcases | _test}}', "\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"", {combined=true})
	local testcases_frame_expected_result = "" ..
		"# Module:UnitTests/testcases/frame\n" ..
		"# Module:UnitTests/testcases/frame\n" ..
		"# Module:UnitTests/testcases/frame\n" ..
		"# Module:UnitTests/testcases/frame\n" ..
		""
	self:preprocess_equals('{{#invoke:UnitTests/testcases/frame | _test}}', "\n" ..
		testcases_frame_expected_result ..
		"# nil\n" ..
	"")
	self:equals("<nowiki>mw.getCurrentFrame():preprocess( '{{#invoke:UnitTests/testcases/frame | _test}}' )</nowiki>",
		mw.getCurrentFrame():preprocess( '{{#invoke:UnitTests/testcases/frame | _test}}' ),
		"\n" ..
		testcases_frame_expected_result ..
		"# nil\n" ..
	"")
	self:equals("<nowiki>frame0:preprocess( '{{#invoke:UnitTests/testcases/frame | _test}}' )</nowiki>",
		frame0:preprocess( '{{#invoke:UnitTests/testcases/frame | _test}}' ),
		"\n" ..
		testcases_frame_expected_result ..
		"# nil\n" ..
	"")
	--[[self:equals("<nowiki>frame0:expandTemplate{ title = '#invoke:UnitTests/testcases/frame', args = { '_test' } }</nowiki>",
		frame0:expandTemplate{ title = '#invoke:UnitTests/testcases/frame', args = { '_test' } },
		"\n" ..
		testcases_frame_expected_result ..
		"# nil\n" ..
	"")]]
	self:equals("require('Module:UnitTests/testcases/frame')._test(mw.getCurrentFrame())",
		"\n"..require('Module:UnitTests/testcases/frame')._test(mw.getCurrentFrame()),
		"\n" ..
		testcases_expected_result ..
		'# "Module:UnitTests/testcases/frame"\n' ..
	"")
	self:equals("p._test(frame0)",
		"\n"..p._test(frame0),
		"\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"")
	self:equals("p._test(mw.getCurrentFrame())",
		"\n"..p._test(mw.getCurrentFrame()),
		"\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"")
	--[[self:preprocess_equals('{{#invoke:UnitTests/testcases | _test0}}', "\n" ..
		testcases_expected_result ..
		"# nil\n" ..
	"")
	self:preprocess_equals('{{#invoke:UnitTests/testcases | _test0}}', "\n" .. p._test0())
	]]
	self:equals("Blatant fail", "<span>String 1</span>", "<span>String 2</span>")
end

return p