Content deleted Content added
two variant subtags |
|||
(18 intermediate revisions by the same user not shown) | |||
Line 1:
local p = require "Module:UnitTests"
local parse_IETF = require "Module:Sandbox/Erutuon".parse_IETF
local fun = require "Module:Fun"
local show = {
language = function (subtags, key)
if not subtags[key] then return nil end
return ("%s: <code>%s</code>"):format(key, subtags[key] or '')
end,
variant = function (subtags, key)
if not subtags[key] then return nil end
return ("%s: <code>%s</code>"):format(
key,
type(subtags[key]) == "table" and table.concat(subtags[key], ", ")
or subtags[key] or '')
end,
error = function (subtags, key)
if not subtags.error then
return nil
end
return ("error: %s at <code>%s</code>")
:format(subtags.error, subtags.invalid or "???")
end,
}
show.script = show.language
show.region = show.language
show.private_use = show.variant
local items = { "language", "script", "region", "variant", "private_use",
"error" } -- "invalid" is handled together with "error".
local mt = {}
mt.__index = table
local function show_parsed_subtags(parsed_subtags)
if not parsed_subtags then return 'nil' end
local result = setmetatable({}, mt)
for _, key in ipairs(items) do
result:insert(show[key](parsed_subtags, key))
end
return result:concat("; ")
end
function p:test_parse_IETF()
Line 6 ⟶ 50:
{ "ru", { language = "ru" } },
{ "ru-Latn", { language = "ru", script = "Latn" } },
{ "
{ "ru
{ "
{ "
{ "
-- Could be used to indicate the PIE transcription system used by Pokorny?
{ "
{ "x-fake-private-use", { private_use = { "fake", "private", "use" } } },
▲ { language = "RU", script = "CYRL", region = "RU", variant = "PETR1708" } },
"errors",
{ "ine-!!!", { error = "invalid characters", invalid = "
{ "
{ "
▲ { "
{ "blahblahblah", { error = "no language subtag", invalid = "blahblahblah" } },
{ "ru-blahblahblah", { language = "ru", error = "invalid subtag", invalid = "blahblahblah" } },
{ "ru-Latn-blahblahblah",
Line 28 ⟶ 72:
{ language = "ru", error = "length of private-use subtag out of range", invalid = "x-blahblahblah" } },
{ "ru-x", { language = "ru", error = "empty private-use subtag", invalid = "x" } },
{ "blahblah-Latn", { error = "no language subtag", invalid = "blahblah-Latn" } },
{ "", nil },
-- { mw.log, nil },
-- { nil, nil },
}
local index = fun.indexOf("errors", examples)
table.insert(examples, index, "case-insensitivity")
for i = 1, index - 1 do -- Insert testcases for capitalized versions of the valid tags.
local upper_example = mw.clone(examples[i])
upper_example[1] = upper_example[1]:upper()
table.insert(examples, index + i, upper_example)
end
self:iterate(
examples,
function (self, IETF_code, expected)
self:equals(
('<code style="white-space: nowrap;">%s</code>'):format(IETF_code),
show_parsed_subtags(parse_IETF(IETF_code)),
show_parsed_subtags(expected))
end)
end
-- Change function names into more readable headers for the testcases tables.
for k, v in require "Module:
if type(k) == "string" then
local new_k = k:gsub("^test_(.+)$", "testcases for <code>%1</code>")
|