-- UnitTester provides unit testing for other Lua scripts. For details see .-- For user documentation see talk page.local UnitTester =
local frame, tick, cross, should_highlightlocal result_table_header = "
' .. first_difference(compared_expected, compared_actual)) or -- Local copies of tick/cross to allow for highlighting local highlight = (should_highlight and not success and 'style="background:#fc0;" ') or result_table:insert(-- Start output ' | ', highlight, 'rowspan=2 | ', success and tick or cross, -- Tick/Cross (2 rows) ' \n | ', mw.text.nowiki(text), ' \n | ', -- Text used for the test (2 rows) expected, ' \n | ', actual, -- The parsed outputs (in the 1st row) differs_at, ' \n | -\n | ', -- Where any relevant difference was (2 rows) mw.text.nowiki(expected), ' \n | ', mw.text.nowiki(actual), -- The unparsed outputs (in the 2nd row) '\n | -\n' -- End output ) else -- Display normally with whichever option was preferred (nowiki/parsed) local differs_at = self.differs_at and (' \n | ' .. first_difference(compared_expected, compared_actual)) or local formatting = options.nowiki and mw.text.nowiki or return_varargs local highlight = (should_highlight and not success and 'style="background:#fc0;" | ') or result_table:insert(-- Start output ' | ', highlight, success and tick or cross, -- Tick/Cross ' \n | ', mw.text.nowiki(text), ' \n | ', -- Text used for the test formatting(expected), ' \n | ', formatting(actual), -- The formatted outputs differs_at, -- Where any relevant difference was '\n | -\n' -- End output ) endend 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) endend 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) 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 if live_text expected and sandbox_textexpected then result_table:insert(' | ', tick) else result_table:insert(' | ', cross) num_failures = num_failures + 1 if live_text ~= expected then highlight_live = true end if sandbox_text ~= expected then highlight_sandbox = true end 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, live_text) or first_difference(expected, sandbox_text)) or result_table:insert(' \n | ', mw.text.nowiki(live), should_highlight and highlight_live and ' \n | ' or ' \n | ', formatting(live_text), should_highlight and highlight_sandbox and ' \n | ' or ' \n | ', formatting(sandbox_text), ' \n | ', formatting(expected), differs_at, "\n | -\n" )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) endend function UnitTester:preprocess_equals_sandbox_many(module, function_name, cases, options) for _, case in ipairs(cases) do local live = module .. " | " .. function_name .. " | " .. case[1] .. " |
function UnitTester:equals(name, actual, expected, options) num_runs = num_runs + 1 if actual
local function deep_compare(t1, t2, ignore_mt) local ty1 = type(t1) local ty2 = type(t2) if ty1 ~= ty2 then return false end if ty1 ~= 'table' and ty2 ~= 'table' then return t1
local mt = getmetatable(t1) if not ignore_mt and mt and mt.__eq then return t1
for k1, v1 in pairs(t1) do local v2 = t2[k1] if v2
nil or not deep_compare(v1, v2) then return false end end
return trueend
local function val_to_str(obj) local function table_key_to_str(k) if type(k)
if type(obj)
elseif type(obj)
else return tostring(obj) endend
function UnitTester:equals_deep(name, actual, expected, options) num_runs = num_runs + 1 if deep_compare(actual, expected) then result_table:insert('| ', tick) else result_table:insert('| ', cross) num_failures = num_failures + 1 end local formatting = (options and options.nowiki and mw.text.nowiki) or return_varargs local actual_str = val_to_str(actual) local expected_str = val_to_str(expected) local differs_at = self.differs_at and (' \n| ' .. first_difference(expected_str, actual_str)) or result_table:insert(' \n| ', name, ' \n| ', formatting(expected_str), ' \n| ', formatting(actual_str), differs_at, "\n|-\n")end
function UnitTester:iterate(examples, func) require 'libraryUtil'.checkType('iterate', 1, examples, 'table') if type(func)
for i, example in ipairs(examples) do if type(example)
'string' then self:heading(example) else error(('bad example #%d (expected table, got %s)') :format(i, type(example)), 2) end endend
function UnitTester:heading(text) result_table:insert_format(' ! colspan="%u" style="text-align: left" | %s \n |- \n ', self.columns, text)end
function UnitTester:run(frame_arg) frame = frame_arg self.frame = frame self.differs_at = frame.args['differs_at'] tick = frame:preprocess('') cross = frame:preprocess('')
local table_header = result_table_header if frame.args['live_sandbox'] then table_header = result_table_live_sandbox_header end if frame.args.highlight then should_highlight = true end
self.columns = 4 if self.differs_at then table_header = table_header .. ' !! Differs at' self.columns = self.columns + 1 end
-- Sort results into alphabetical order. local self_sorted = for key, _ in pairs(self) do if key:find('^test') then table.insert(self_sorted, key) end end table.sort(self_sorted) -- Add results to the results table. for _, value in ipairs(self_sorted) do result_table:insert_format(table_header .. "\n|-\n", value) self[value](self) result_table:insert("|}\n") end
return (num_runs
0 and "All " .. num_runs .. " tests passed." or "" .. num_failures .. " of " .. num_runs .. " tests failed.") .. "\n\n" .. frame:preprocess(result_table:concat)end
function UnitTester:new local o = setmetatable(o, self) self.__index = self return oend
local p = UnitTester:newfunction p.run_tests(frame) return p:run(frame) endreturn p