Content deleted Content added
try "make_expected=yes" option |
better for make_tests to be function; p.tests can be a table |
||
Line 4:
-- The expected text must be in a single line, but can include "\n" (two characters)
-- to indicate that a newline is expected.
-- Tests are run (or created) by setting p.tests
-- by setting page=PAGE_TITLE (and optionally section=SECTION_TITLE)
-- then executing run_tests (or make_tests).
-- Adapted from [[Module:ConvertTestcase]].
local function collection()
Line 88 ⟶ 77:
end
local function run_template(frame, template, collapse_multiline)
local title, argstr = template:match('^{{%s*(.-)%s*|(.*)}}$')
if title == nil or title == '' or argstr == '' then
Line 106 ⟶ 95:
end
local ok, result = pcall(expand, { title = title, args = args })
if not ok then
end
if collapse_multiline then
result = result:gsub('\n', '\\n')
end
return result
end
local function
local maxlen = 38
for _, item in ipairs(all_tests) do
local template = item[1]
local templen = mw.ustring.len(template)
item.templen = templen
if maxlen < templen and templen <= 70 then
maxlen = templen
end
end
local
for _, item in ipairs(all_tests) do
local actual = run_template(frame, template, true)
local pad = string.rep(' ', maxlen - item.templen) .. ' '
result:add(template .. pad .. actual)
end
-- Pre tags returned by a module are html tags, not like wikitext <pre>...</pre>.
return '<pre>\n' .. mw.text.nowiki(result:join()) .. '\n</pre>\n'
end
local function _run_tests(frame, all_tests)
local function safe_cell(text, multiline)
-- For testing {{convert}}, want wikitext like '[[kilogram|kg]]' to be unchanged
Line 138 ⟶ 144:
return text
end
local stats = { pass = 0, fail = 0, ignored = 0 }
local result = collection()
result:add('{| class="wikitable"')
for _, item in ipairs(all_tests) do
local template, expected = item[1], item[2]
local actual = run_template(frame, template, true)
local sbox, isfail
actual, sbox, isfail = status_box(stats, expected, actual)
result:add('|-')
result:add('| ' ..
result:add('| ' .. safe_cell(expected, true))
result:add('| ' .. safe_cell(actual, true))
if isfail
result:add('|
result:add('| ' ..
end
end
result:add('|}')
Line 223 ⟶ 213:
end
local function get_tests(
local args = frame.args
local page_title, section_title = args.page, args.section
if not empty(page_title) then
if not empty(
error('Invoke must not set "page=' .. page_title .. '" if also setting p.tests.', 0)
end
Line 231 ⟶ 223:
page_title = strip(page_title:sub(3, -3))
end
if not empty(section_title) then
local s = sections(
while true do
local heading = s:next_heading()
if heading then
if heading == section_title then
break
end
else
Line 246 ⟶ 239:
end
end
if type(tests) ~= 'string' then
if type(tests) == 'table' then
return tests
end
error('No tests were specified; see [[Module:Convert/tester/doc]].', 0)
end
local all_tests = collection()
for line in (tests .. '\n'):gmatch('([^\n]+)\n') do
local template, expected = line:match('^({{.-}})%s*(.-)%s*$')
if template then
all_tests:add({ template, expected })
end
end
if all_tests.n == 0 then
error('No templates found; see [[Module:Convert/tester/doc]].', 0)
end
return all_tests
end
local
local ok, result = pcall(get_tests, frame, p.tests)
if ok then
ok, result = pcall(
if ok then
return result
Line 261 ⟶ 267:
end
return '<strong class="error">Error</strong>\n\n' .. result
end
local p = {}
function p.make_tests(frame)
return main(frame, p, _make_tests)
end
function p.run_tests(frame)
return main(frame, p, _run_tests)
end
|