Module:ja-kanji-readings/testcases

From Wiktionary, the free dictionary
Jump to navigation Jump to search

All tests passed. (refresh)

TextExpectedActual
test_catboiler_cats:

local tests = require('Module:UnitTests')

local m_readings = require("Module:ja-kanji-readings")
local get_catboiler_cats = m_readings.get_catboiler_cats
local parse_category_name = m_readings.parse_pagename

local ugsub = mw.ustring.gsub
local kana_regex = "[-ぁ-ー]+"

local function category_name_to_cats(pagename)
	return get_catboiler_cats(parse_category_name(pagename))
end

local function map(t, func)
	local new_t = {}
	
	for i, item in ipairs(t) do
		new_t[i] = func(item)
	end
	
	return new_t
end

local function unabbreviate(category)
	return '[[Category:Japanese kanji ' .. category .. ']]'
end

local function format_categories(categories, abbreviated)
	local func
	table.sort(categories)
	if categories[1] then
		return table.concat(
			abbreviated and map(categories, unabbreviate) or categories,
			'\n')
	else
		return nil
	end
end

local tag = mw.text.tag
local function tag_Japanese(text)
	return tag('span', { class = 'Jpan', lang = 'ja' }, text)
end

local options = { display =
	function(text)
		return text ~= '(nil)' and '<pre>' .. mw.text.nowiki(text) .. '</pre>\n' or text
	end
}
function tests:check_catboiler_cats(pagename, expected)
	self:equals(
		'Japanese kanji ' .. ugsub(pagename, kana_regex, tag_Japanese),
		format_categories(category_name_to_cats('Japanese kanji ' .. pagename)),
		format_categories(expected, true),
		options)
end

function tests:test_catboiler_cats()
	local examples = {
		{ "with ancient kun reading 𛀁-", { "by ancient kun reading|𛀁", "with ancient reading 𛀁-|kun" } },
		{ "read as あん", { "by reading|あん" }, },
		{ "with on reading あん", { "by on reading|あん", "read as あん|on" }, },
		{ "with goon reading あん", { "by goon reading|あん", "with on reading あん|goon", "read as あん|goon" }, },
		{ "with kan'on reading あん", { "by kan'on reading|あん", "with on reading あん|kan'on", "read as あん|kan'on" }, },
		{ "with tōon reading あん", { "by tōon reading|あん", "with on reading あん|tōon", "read as あん|tōon" }, },
		{ "with kan'yōon reading あん", { "by kan'yōon reading|あん", "with on reading あん|kan'yōon", "read as あん|kan'yōon" }, },
		{ "with historical on reading か", { "by historical on reading|か", "with historical reading か|on" }, },
		{ "with historical goon reading か", { "by historical goon reading|か", "with historical on reading か|goon" }, },
		{ "with historical kan'on reading か", { "by historical kan'on reading|か", "with historical on reading か|kan'on" }, },
		{ "with historical tōon reading か", { "by historical tōon reading|か", "with historical on reading か|tōon" }, },
		{ "with historical kan'yōon reading か", { "by historical kan'yōon reading|か", "with historical on reading か|kan'yōon" }, },
		{ "with historical kun reading か", { "with kun readings missing okurigana designation|か", "by historical kun reading|か", "with historical reading か|kun" }, },
		{ "with kun reading い-く", { "by kun reading|いく", "read as い-く|kun" }, },
		{ "with nanori reading ゆき", { "by nanori reading|ゆき", "read as ゆき|nanori" }, },
		{ "with on reading きょう", { "by on reading|きょう", "read as きょう|on" }, },
		{ "with historical on reading きやう", { "by historical on reading|きやう", "with historical reading きやう|on" }, },
		{ "with ancient on reading くゐやう", { "by ancient on reading|くゐやう", "with ancient reading くゐやう|on" }, },
		{ "with kun reading いわ-んや", { "by kun reading|いわんや", "read as いわ-んや|kun" }, },
		{ "with ancient on reading くゐやう", { "by ancient on reading|くゐやう", "with ancient reading くゐやう|on" }, },
		{ "by on reading", { "by reading|on" }, },
		{ "by kan'on reading", { "by on reading|kan'on" }, },
		{ "by ancient on reading", { "by on reading|ancient", "by ancient reading|on" }, },
		{ "by ancient kan'on reading", { "by ancient on reading|kan'on", "by ancient reading|kan'on" }, },
		{ "blah blah", { nil }, },
	}
	
	--self:iterate(examples, "check_catboiler_cats")
end

return tests