Module:Convert/tester: Difference between revisions

Content deleted Content added
continue testing other modules if a module does not exist
allow test=xxx to be injected into the template under test so previewing make_test need not show preview errors; include 'countries' for c:Module:Countries
Line 80:
end
 
local function run_template(frame, template, forcenameargs, collapse_multiline)
-- Template "{{ example | 2 = def | abc | name = ghi jkl }}"
-- gives argsxargs { " abc ", "def", name = "ghi jkl" }.
if template:sub(1, 2) == '{{' and template:sub(-2, -1) == '}}' then
template = template:sub(3, -3) .. '|' -- append sentinel to get last field
Line 88:
return '(invalid template)'
end
local argsxargs = {}
local index = 1
local templatename
Line 101:
end
end
argsxargs[k] = v
end
template = template:gsub('(%[%[[^%[%]]-)|(.-%]%])', '%1\0%2') -- replace pipe in piped link with a zero byte
Line 107:
field = field:gsub('%z', '|') -- restore pipe in piped link
if templatename == nil then
templatename = forcenameargs.template or strip(field)
if templatename == '' then
return '(invalid template)'
Line 122:
end
else
while argsxargs[index] ~= nil do
-- Skip any explicit numbered parameters like "|5=five".
index = index + 1
Line 129:
end
end
end
if args.test and not xargs.test then
-- For convert, allow test=preview or test=nopreview to be injected into
-- the convert under test, if it does not already use that parameter.
-- That allows, for example, a preview of make_tests to show nopreview results.
xargs.test = args.test
end
local function expand(t)
return frame:expandTemplate(t)
end
local ok, result = pcall(expand, { title = templatename, args = argsxargs })
if not ok then
result = 'Error: ' .. result
Line 143 ⟶ 149:
end
 
local function _make_tests(frame, all_tests, forcenameargs)
local maxlen = 38
for _, item in ipairs(all_tests) do
Line 159 ⟶ 165:
local template = item[1]
if template then
local actual = run_template(frame, template, forcenameargs, true)
local pad = string.rep(' ', maxlen - item.templen) .. ' '
result:add(template .. pad .. actual)
Line 173 ⟶ 179:
end
 
local function _run_tests(frame, all_tests, forcenameargs)
local function safe_cell(text, multiline)
-- For testing {{convert}}, want wikitext like '[[kilogram|kg]]' to be unchanged
Line 199 ⟶ 205:
local template, expected = item[1], item[2] or ''
if template then
local actual = run_template(frame, template, forcenameargs, true)
local sbox, actual, isfail = status_box(stats, expected, actual)
result:add('|-')
Line 367 ⟶ 373:
 
local function main(frame, p, worker)
local args = frame.args
local ok, result = pcall(get_tests, frame, p.tests)
if ok then
ok, result = pcall(worker, frame, result, argsframe.templateargs)
if ok then
return result
Line 381 ⟶ 386:
-- For convenience, a key defined here can be used to refer to the
-- corresponding list of modules.
countries = { -- Commons
'Countries',
'Countries/Africa',
'Countries/Americas',
'Countries/Asia',
'Countries/Caribbean',
'Countries/Central America',
'Countries/Europe',
'Countries/North America',
'Countries/North America (subcontinent)',
'Countries/Oceania',
'Countries/South America',
'Countries/United Kingdom',
},
convert = {
'Convert',