Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/Erutuon/testcases
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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() local examples = { { "ru", { language = "ru" } }, { "ru-Latn", { language = "ru", script = "Latn" } }, { "fr-CA", { language = "fr", region = "CA" } }, { "ru-petr1708", { language = "ru", variant = "petr1708" } }, { "zh-Latn-pinyin", { language = "zh", script = "Latn", variant = "pinyin" } }, { "sl-rozaj-biske", { language = "sl", variant = { "rozaj", "biske" } } }, { "sl-rozaj-biske-1994", { language = "sl", variant = { "rozaj", "biske", "1994" } } }, { "en-CA-newfound", { language = "en", region = "CA", variant = "newfound" } }, { "ine-x-proto", { language = "ine", private_use = "proto" } }, -- Could be used to indicate the PIE transcription system used by Pokorny? { "ine-x-proto-pokorny", { language = "ine", private_use = { "proto", "pokorny" } } }, { "x-fake-private-use", { private_use = { "fake", "private", "use" } } }, "errors", { "ine-!!!", { error = "invalid characters", invalid = "!!!" } }, { "pâté", { error = "invalid characters", invalid = "pâté" } }, { "Latn", { error = "no language subtag", invalid = "Latn" } }, { "ru-Latn-Cyrl", { language = "ru", script = "Latn", error = "invalid subtag", invalid = "Cyrl" } }, { "blahblahblah", { error = "no language subtag", invalid = "blahblahblah" } }, { "ru-blahblahblah", { language = "ru", error = "invalid subtag", invalid = "blahblahblah" } }, { "ru-Latn-blahblahblah", { language = "ru", script = "Latn", error = "invalid subtag", invalid = "blahblahblah" } }, { "ru-x-blahblahblah", { 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:TableTools".sortedPairs(p) do if type(k) == "string" then local new_k = k:gsub("^test_(.+)$", "testcases for <code>%1</code>") if new_k ~= k then p[k] = nil p[new_k] = v end end end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Sandbox/Erutuon/testcases/doc
(
edit
)