Jump to content

Module:Unicode data/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 07:29, 23 June 2018 (some name-or-label testcases). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
-- Example Unit tests for [[Module:Bananas]]. Click talk page to run tests.
local p = require 'Module:UnitTests'
local Unicode_data = require 'Module:Unicode data'

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))
			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 string, not ' .. type(func) .. '.')
	end
end

local function uplus(codepoint)
	return ("U+%04X"):format(codepoint)
end

function p:check_lookup_name(codepoint, name)
	self:equals(uplus(codepoint), Unicode_data.lookup_name(codepoint), name)
end

local U = mw.ustring.char
function p:test_lookup_name()
	local examples = {
		{ 0x0000, "<control-0000>" },
		{ 0x0300, "COMBINING GRAVE ACCENT" },
		{ 0x0378, "<reserved-0378>" },
		{ 0x3555, "CJK UNIFIED IDEOGRAPH-3555" },
		{ 0xAC01, "HANGUL SYLLABLE GAG" },
		{ 0xD5FF, "HANGUL SYLLABLE HEH" },
		{ 0xDC00, "<surrogate-DC00>", },
		{ 0xFDD1, "<noncharacter-FDD1>", },
		{ 0xFFFD, "REPLACEMENT CHARACTER" },
		{ 0xFFFF, "<noncharacter-FFFF>" },
		{ 0x10FFFF, "<noncharacter-10FFFF>" },
	}
	
	self:iterate(examples, "check_lookup_name")
end

return p