Documentation for this module may be created at Module:Lang/codes/testcases/doc
local p = require("Module:UnitTests")
local get_ietf_parts = require("Module:Lang/sandbox").get_ietf_parts
local prepare
function p:check(code, expected)
self:equals_deep(code, { get_ietf_parts(code) }, prepare(expected))
end
function p:test()
local examples = {
{ "de", { "de" } },
{ "nav", { "nav" } },
{ "1ca", { "1ca" } },
{ "cmn-Latn", { "cmn", "latn" } },
{ "he-hebr", { "he", "hebr" } },
{ "de-ch", { "de", nil, "ch" } },
{ "de-1901", { "de", nil, nil, "1901" } },
{ "ru-petr1708", { "ru", nil, nil, "petr1708" } },
{ "de-petr1708", {"de"} },
{ "ca-valencia", { "ca", nil, nil, "valencia" } },
{ "ru-cyrl-petr1708", {"ru", "cyrl"} },
{ "ru-Cyril", {"ru"} },
--[[
{ "", { "" } },
--]]
}
self:iterate(examples, "check")
end
function p:iterate(examples, func)
if type(examples) ~= 'table' then
error('First argument of iterate should be a table, not ' .. type(examples) .. '.')
end
if type(func) == 'string' then
func = self[func]
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('iterate does not know what to do with example number ' .. i .. ', whose type is ' .. type(example) .. '.')
end
end
elseif type(func) == 'function' then -- used in [[Module:he-translit/testcases]]
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('iterate does not know what to do with example number ' .. i .. ', whose type is ' .. type(example) .. '.')
end
end
else
error('Second argument of iterate should be a function or a string, not ' .. type(func) .. '.')
end
end
function prepare(subtag_table)
if not next(subtag_table) then
return subtag_table
end
for i = 1, 4 do
if subtag_table[i] == nil then
subtag_table[i] = ""
end
end
return subtag_table
end
return p
