Jump to content

Module:Sandbox/Erutuon/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 00:13, 4 July 2018 (add heading). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = require "Module:UnitTests"
local parse_IETF = require "Module:Sandbox/Erutuon".parse_IETF

function p:test_parse_IETF()
	local examples = {
		{ "ru", { language = "ru" } },
		{ "ru-Latn", { language = "ru", script = "Latn" } },
		{ "ine-x-proto", { language = "ine", private_use = "x-proto" } },
		"errors",
		{ "ine-!!!", { error = "invalid characters", invalid = "ine-!!!" } },
		{ "Latn", { error = "no language subtag", script = "Latn", invalid = "Latn" } },
		{ "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" } },
		{ "", nil },
		-- { mw.log, nil },
		-- { nil, nil },
	}
	
	self:iterate(
		examples,
		function (self, IETF_code, expected)
			self:equals_deep(IETF_code, parse_IETF(IETF_code), expected)
		end)
end

-- Change function names into more readable headers for the testcases tables.
for k, v in require "Module:table".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