Module:Sandbox/Erutuon/testcases

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.