跳转到内容

模組:Language/scripts/testcases

维基百科,自由的百科全书
local p = require('Module:UnitTests')

local scripts = require("Module:Language/scripts")
local countScripts = scripts.countScripts
local getScript = scripts.getScript
local sortedPairs = require("Module:table").sortedPairs

local function map(func, t)
	local array = {}
	if t[1] then
		for i, v in ipairs(t) do
			array[i] = func(v, i, t)
		end
	else
		local i = 0
		for k, v in sortedPairs(t) do
			i = i + 1
			array[i] = func(v, k, t)
		end
	end
	return array
end

local function showScripts(text)
	local scripts = countScripts(text)
	return table.concat(
		map(function(v, k)
				return k .. " (" .. v .. ")"
			end,
			countScripts(text)),
		", ")
end

-- from [[wikt:Module:UnitTests]]
function p:iterate(examples, func)
	if type(examples) ~= 'table' then
		error('First argument of iterate should be a table, not ' .. type(examples) .. '.')
	end
	if type(func) == 'string' then
		func = self[func]
		for i, example in ipairs(examples) do
			if type(example) == 'table' then
				func(self, unpack(example))
			elseif type(example) == 'string' then
				self:heading(example)
			else
				error('iterate does not know what to do with example number ' .. i .. ', whose type is ' .. type(example) .. '.')
			end
		end
	elseif type(func) == 'function' then -- used in [[Module:he-translit/testcases]]
		for i, example in ipairs(examples) do
			if type(example) == 'table' then
				func(self, unpack(example))
			elseif type(example) == 'string' then
				self:heading(example)
			else
				error('iterate does not know what to do with example number ' .. i .. ', whose type is ' .. type(example) .. '.')
			end
		end
	else
		error('Second argument of iterate should be a function or a string, not ' .. type(func) .. '.')
	end
end

function p:checkCountScripts(example, expected)
	self:equals(example, showScripts(example), expected)
end

function p:checkGetScript(example, expected)
	self:equals(example, getScript(example), expected)
end

p["testcases for countScripts"] = function (self)
	local examples = {
		{
			"'''Ста́нция Восто́к'''",
			"Cyrl (13), Zinh (2), Zyyy (7)"
		},
		{ "Σωκράτης", "Grek (8)" },
		{ "中华人民共和国", "Hani (7)" },
		{ "অবনী বাড়ি আছো ''Ôboni Baŗi Achho''", "Beng (12), Latn (14), Zyyy (9)" }, -- from previous revision of [[Abani Bari Achho]]
	}
	self:iterate(examples, "checkCountScripts")
end

p["testcases for getScript"] = function (self)
	local examples = {
		{ "'''Ста́нция Восто́к'''", "Cyrl" },
		{ mw.ustring.toNFD "Ἑλλήσποντος", "Grek" }, -- decompose to get version with combining diacritics
	}
	self:iterate(examples, "checkGetScript")
end

return p