Module:UnitTests and Module:UnitTests/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
expected_actual should be based off of the expected, not the actual. This was basically seeing if actual == actual (which is always gonna be true). I've also adjusted the variable name to be more accurate
 
m sync to live, keeping current changes
 
Line 45: Line 45:
end
end


function UnitTester:calculate_output(text, expected, actual, options)
function UnitTester:_calculate_preprocess_result(text, expected, actual, options)
-- Set up some variables for throughout for ease
-- Set up some variables for throughout for ease
num_runs = num_runs + 1
num_runs = num_runs + 1
Line 51: Line 51:
-- Fix any stripmarkers if asked to do so to prevent incorrect fails
-- Fix any stripmarkers if asked to do so to prevent incorrect fails
local compared_expected = expected
local compared_actual = actual
local compared_actual = actual
if options.templatestyles then
if options.templatestyles then
local pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-)(%x+)(%-QINU[^\127]*\127)'
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
local _, expected_stripmarker_id = expected:match(pattern) -- when module rendering has templatestyles strip markers, use ID from expected to prevent false test fail
if expected_stripmarker_id then
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_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
end
end
if options.stripmarker then
if options.stripmarker then
local pattern = '(\127[^\127]*UNIQ%-%-%l+%-)(%x+)(%-%-?QINU[^\127]*\127)'
local pattern = '(\127[^\127]*UNIQ%-%-%l+%-)(%x+)(%-%-?QINU[^\127]*\127)'
local _, expected_stripmarker_id = compared_expected:match(pattern)
local _, expected_stripmarker_id = expected:match(pattern)
if expected_stripmarker_id then
if expected_stripmarker_id then
compared_actual = compared_actual:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
compared_actual = compared_actual:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
compared_expected = compared_expected:gsub(pattern, '%1' .. expected_stripmarker_id .. '%3')
end
end
end
end
-- Perform the comparison
-- Perform the comparison
local success = compared_actual == compared_expected
local success = compared_actual == expected
if not success then
if not success then
num_failures = num_failures + 1
num_failures = num_failures + 1
Line 80: Line 77:
-- We need 2 rows available for the expected and actual columns
-- We need 2 rows available for the expected and actual columns
-- Top one is parsed, bottom is unparsed
-- Top one is parsed, bottom is unparsed
local differs_at = self.differs_at and (' \n| rowspan=2|' .. first_difference(compared_expected, compared_actual)) or ''
local differs_at = self.differs_at and (' \n| rowspan=2|' .. first_difference(expected, compared_actual)) or ''
-- Local copies of tick/cross to allow for highlighting
-- Local copies of tick/cross to allow for highlighting
local highlight = (should_highlight and not success and 'style="background:#fc0;" ') or ''
local highlight = (should_highlight and not success and 'style="background:#fc0;" ') or ''
Line 93: Line 90:
else
else
-- Display normally with whichever option was preferred (nowiki/parsed)
-- 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 differs_at = self.differs_at and (' \n| ' .. first_difference(expected, compared_actual)) or ''
local formatting = options.nowiki and mw.text.nowiki or return_varargs
local formatting = options.nowiki and mw.text.nowiki or return_varargs
local highlight = (should_highlight and not success and 'style="background:#fc0;"|') or ''
local highlight = (should_highlight and not success and 'style="background:#fc0;"|') or ''
Line 108: Line 105:
function UnitTester:preprocess_equals(text, expected, options)
function UnitTester:preprocess_equals(text, expected, options)
local actual = frame:preprocess(text)
local actual = frame:preprocess(text)
self:calculate_output(text, expected, actual, options)
self:_calculate_preprocess_result(text, expected, actual, options)
end
end


Line 120: Line 117:
local actual = frame:preprocess(text1)
local actual = frame:preprocess(text1)
local expected = frame:preprocess(text2)
local expected = frame:preprocess(text2)
self:calculate_output(text1, expected, actual, options)
self:_calculate_preprocess_result(text1, expected, actual, options)
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
end


function UnitTester:preprocess_equals_compare(live, sandbox, expected, options)
function UnitTester:preprocess_equals_compare(live, sandbox, expected, options)
--TODO: Standardise and simplyify - also make table headers more compatible
--Currently, if we use any other option to compare *at literally any point*, we can't use this, and vice versa
--This is because the change in the header setup is far too awkward to handle right now
--As for how to fix this... dunno, needs some thinking. We could probably assume
--that a _compare run and another run won't be used in the exact same function at least,
--and sort the headers per function somehow instead of just having either one header or another for every test
local live_text = frame:preprocess(live)
local live_text = frame:preprocess(live)
local sandbox_text = frame:preprocess(sandbox)
local sandbox_text = frame:preprocess(sandbox)
Line 157: Line 166:
"\n|-\n"
"\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)
end
end
end


Line 324: Line 327:
return (num_runs == 0 and "<b>No tests were run.</b>"
return (num_runs == 0 and "<b>No tests were run.</b>"
or num_failures == 0 and "<b style=\"color:#008000\">All " .. num_runs .. " tests passed.</b>"
or num_failures == 0 and "<b style=\"color:#008000\">All " .. num_runs .. " tests passed.</b>"
or "<b style=\"color:#800000\">" .. num_failures .. " of " .. num_runs .. " tests failed.</b>[[Category:Failed Lua testcases using Module:UnitTests]]"
or "<b style=\"color:#800000\">" .. num_failures .. " of " .. num_runs .. " tests failed.</b>"
) .. "\n\n" .. frame:preprocess(result_table:concat())
) .. "\n\n" .. frame:preprocess(result_table:concat())
end
end