模組:Unicode data/testcases

維基詞典,自由的多語言詞典

21測試s失敗。 (刷新)

文字 預期 實際
test char_to_script:
Script error during testing: Module:Unicode_data/testcases:53: attempt to call field 'char_to_script' (a nil value)
stack traceback:
	[C]: in function 'char_to_script'
	Module:Unicode_data/testcases:53: in function 'func'
	Module:UnitTests:246: in function 'iterate'
	Module:Unicode_data/testcases:50: in function <Module:Unicode_data/testcases:34>
	(tail call): ?
	[C]: in function 'xpcall'
	Module:UnitTests:295: in function <Module:UnitTests:260>
	(tail call): ?
	mw.lua:527: in function <mw.lua:507>
	[C]: ?
	[C]: in function 'expandTemplate'
	mw.lua:333: in function 'expandTemplate'
	Module:Documentation:746: in function 'chunk'
	mw.lua:527: in function <mw.lua:507>
	[C]: ?
文字 預期 實際
test get_block_range:
Failed Basic Latin U+0000–U+007F nil
Passed blah nil nil
文字 預期 實際
test get_entry_title:
Failed U+0023: # Unsupported titles/Number sign 不支援的頁面名稱/數字符號
Failed U+0020:   Unsupported titles/Space 不支援的頁面名稱/空格
文字 預期 實際
test is_assigned:
Passed U+0061: a true true
Passed U+0378 false false
Failed U+30000: 𰀀 false true
文字 預期 實際
test is_combining:
Passed U+0060: ` false false
Passed U+0300: ◌̀ true true
Passed U+0378 false false
Passed U+DC00 false false
文字 預期 實際
test is_printable:
Passed U+0000 false false
Passed U+0020:   true true
Passed U+0061: a true true
文字 預期 實際
test is_valid_pagename:
Passed # false false
Passed   false false
Passed word true true
文字 預期 實際
test is_whitespace:
Passed U+0020:   true true
Passed U+0061: a false false
文字 預期 實際
test lookup_block:
Failed U+0064: d Basic Latin 基本拉丁字母
Failed U+030B: ◌̋ Combining Diacritical Marks 結合附加符號
Failed U+03A3: Σ Greek and Coptic 希臘字母及科普特字母
Failed U+3175: ㅵ Hangul Compatibility Jamo 諺文相容字母
Failed U+AC01: 각 Hangul Syllables 諺文音節
Failed U+10FFFF Supplementary Private Use Area-B 補充私人使用區-B
文字 預期 實際
test lookup_image:
Failed U+203D: ‽
Failed U+30A2: ア
Passed U+0B85: அ
文字 預期 實際
test lookup_name:
Failed U+0000 <control-0000> <控制字符-0000>
Failed U+007F <control-007F> <控制字符-007F>
Passed U+00C1: Á LATIN CAPITAL LETTER A WITH ACUTE LATIN CAPITAL LETTER A WITH ACUTE
Passed U+0300: ◌̀ COMBINING GRAVE ACCENT COMBINING GRAVE ACCENT
Passed U+0378 <reserved-0378> <reserved-0378>
Passed U+1B44: ◌᭄ BALINESE ADEG ADEG BALINESE ADEG ADEG
Passed U+1F71: ά GREEK SMALL LETTER ALPHA WITH OXIA GREEK SMALL LETTER ALPHA WITH OXIA
Failed U+3555: 㕕 CJK UNIFIED IDEOGRAPH-3555 中日韓統一表意文字-3555
Failed U+AC01: 각 HANGUL SYLLABLE GAG 諺文音節 GAG
Failed U+D5FF: 헿 HANGUL SYLLABLE HEH 諺文音節 HEH
Failed U+DC00 <surrogate-DC00> <代用區-DC00>
Failed U+EEEE <private-use-EEEE> <私人使用區-EEEE>
Passed U+FDD1 <noncharacter-FDD1> <noncharacter-FDD1>
Passed U+FFFD: � REPLACEMENT CHARACTER REPLACEMENT CHARACTER
Passed U+FFFF <noncharacter-FFFF> <noncharacter-FFFF>
Passed U+1F4A9: 💩 PILE OF POO PILE OF POO
Passed U+E0000 <reserved-E0000> <reserved-E0000>
Failed U+F0F0F <private-use-F0F0F> <私人使用區-F0F0F>
Passed U+10FFFF <noncharacter-10FFFF> <noncharacter-10FFFF>

local tests = require("Module:UnitTests")

local m_Unicode_data = require("Module:Unicode data")

local U = mw.ustring.char

local function show_whitespace(whitespace)
	return '<span style="background-color: lightgray;">'
		.. whitespace:gsub(" ", "&nbsp;") .. '</span>'
end

local function show(codepoint)
	if m_Unicode_data.is_printable(codepoint) then
		local printed_codepoint = U(codepoint)
		if mw.ustring.toNFC(printed_codepoint) ~= printed_codepoint then
			printed_codepoint = ("&#x%X;"):format(codepoint)
		end
		if m_Unicode_data.is_whitespace(codepoint) then
			printed_codepoint = show_whitespace(printed_codepoint)
		end
		if m_Unicode_data.is_combining(codepoint) then
			printed_codepoint = "◌" .. printed_codepoint
		end
		return ("U+%04X: %s"):format(codepoint, printed_codepoint)
	else
		return ("U+%04X"):format(codepoint)
	end
end

local function tag(char, sc)
	return string.format('<span class="%s">%s</span>', sc, char)
end

function tests:test_char_to_script()
	local examples = {
		{ "A", "Latn" },
		{ "一", "Hani" },
		{ "ώ", "Grek" },
		{ "ὦ", "polytonic" },
		{ "Ж", "Cyrl" },
		{ "Ѹ", "Cyrs" },
		{ "ꙑ", "Cyrs" },
		{ "ა", "Geor" },
		{ "Ⴀ", "Geok" },
		{ "ⴀ", "Geok" },
		{ "!", "None" },
		{ U(0x2F82B), "None" },
	}
	
	self:iterate(
		examples,
		function (self, char, expected)
			local sc = m_Unicode_data.char_to_script(char)
			self:equals(
				tag(char, sc),
				sc,
				expected
			)
		end)
end

local function return_all(...) return ... end

local function test_lookup(what, examples, display)
	local funcname = "lookup_" .. what
	local func = m_Unicode_data[funcname]
	display = display or return_all
	
	tests["test_" .. funcname] = function (self)
		self:iterate(
			examples,
			function (self, codepoint, result)
				self:equals(
					show(codepoint),
					display(func(codepoint)),
					display(result))
			end)
	end
end

test_lookup(
	"name",
	{
		{   0x0000, "<control-0000>" },
		{   0x007F, "<control-007F>" },
		{   0x00C1, "LATIN CAPITAL LETTER A WITH ACUTE" },
		{   0x0300, "COMBINING GRAVE ACCENT" },
		{   0x0378, "<reserved-0378>" },
		{   0x1B44, "BALINESE ADEG ADEG" },
		{   0x1F71, "GREEK SMALL LETTER ALPHA WITH OXIA" },
		{   0x3555, "CJK UNIFIED IDEOGRAPH-3555" },
		{   0xAC01, "HANGUL SYLLABLE GAG" },
		{   0xD5FF, "HANGUL SYLLABLE HEH" },
		{   0xDC00, "<surrogate-DC00>", },
		{   0xEEEE, "<private-use-EEEE>" },
		{   0xFDD1, "<noncharacter-FDD1>", },
		{   0xFFFD, "REPLACEMENT CHARACTER" },
		{   0xFFFF, "<noncharacter-FFFF>" },
		{  0x1F4A9, "PILE OF POO" },
		{  0xE0000, "<reserved-E0000>" },
		{  0xF0F0F, "<private-use-F0F0F>" },
		{ 0x10FFFF, "<noncharacter-10FFFF>" },
	})

test_lookup(
	"block",
	{
		{   0x0064, "Basic Latin"                      },
		{   0x030B, "Combining Diacritical Marks"      },
		{   0x03A3, "Greek and Coptic"                 },
		{   0x3175, "Hangul Compatibility Jamo"        },
		{   0xAC01, "Hangul Syllables"                 },
		{ 0x10FFFF, "Supplementary Private Use Area-B" },
	})

test_lookup(
	"image",
	{
		{ 0x203D, "Interrobang.svg" },
		{ 0x30A2, "Japanese Katakana A.svg" },
		{ 0x0B85, "Tamil-alphabet-அஅ.svg" },
	},
	function (image_title)
		return "[[File:" .. image_title .. "|frameless|14px]]"
	end)

local function test_is(what, examples)
	local funcname = "is_" .. what
	local func = m_Unicode_data[funcname]
	
	tests["test_" .. funcname] = function (self)
		self:iterate(
			examples,
			function (self, codepoint, result)
				self:equals(
					show(codepoint),
					func(codepoint),
					result)
			end)
	end
end

test_is(
	"assigned",
	{
		{  0x0061, true  },
		{  0x0378, false },
		{ 0x30000, false },
	})

test_is(
	"combining",
	{
		{ mw.ustring.codepoint "`", false },
		{ 0x0300, true },
		{ 0x0378, false },
		{ 0xDC00, false },
	})

test_is(
	"printable",
	{
		{ 0x0000, false },
		{ 0x0020, true },
		{ 0x0061, true },
	})

test_is(
	"whitespace",
	{
		{ 0x0020, true },
		{ 0x0061, false },
	})

function tests:test_get_block_range()
	local function display_block_range(low, high)
		if type(low) == "nil" then
			return "nil"
		end
		
		return ("U+%04X&ndash;U+%04X"):format(low, high)
	end
	
	self:iterate(
		{
			{ "Basic Latin", { 0x0000, 0x007F } },
			{ "blah", { nil, nil } },
		},
		function (self, block_name, block_range)
			self:equals(
				block_name,
				display_block_range(m_Unicode_data.get_block_range(block_name)),
				display_block_range(unpack(block_range)))
		end)
end

function tests:test_is_valid_pagename()
	self:iterate(
		{
			{ "#", false },
			{ " ", false },
			{ "word", true },
		},
		function (self, pagename, validity)
			local displayed_pagename
			
			if mw.ustring.len(pagename) == 1
					and m_Unicode_data.is_whitespace(mw.ustring.codepoint(pagename)) then
				displayed_pagename = show_whitespace(pagename)
			else
				displayed_pagename = pagename
			end
			
			self:equals(
				displayed_pagename,
				m_Unicode_data.is_valid_pagename(pagename),
				validity)
		end)
end

function tests:test_get_entry_title()
	self:iterate(
		{
			{ mw.ustring.codepoint "#", "Number sign" },
			{ mw.ustring.codepoint " ", "Space" },
		},
		function (self, codepoint, entry_title)
			self:equals(
				show(codepoint),
				"[[" .. m_Unicode_data.get_entry_title(codepoint) .. "]]",
				"[[Unsupported titles/" .. entry_title .. "]]")
		end)
end

for k, v in require "Module:table".sortedPairs(tests) do
	if type(k) == "string" then
		local new_k = k:gsub("^test_(.+)$", "test <code>%1</code>")
		if new_k ~= k then
			tests[k] = nil
			tests[new_k] = v
		end
	end
end

return tests