Jump to content

Module:Sandbox/Erutuon/testcases

From Wikipedia, the free encyclopedia
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