Module:User:Chernorizets/bg-orthography/testcases

local tests = require("Module:UnitTests")

local m_ortho = require("Module:User:Chernorizets/bg-orthography") local is_valid = m_ortho.is_valid

local test_cases = { { word = nil, expected_ret = false, expected_rule = "no input" }, { word = "", expected_ret = true, expected_rule = nil },

{		word = nil, opts = { nil_is_valid = true }, opts_hint = "{ nil_is_valid }", expected_ret = true, expected_rule = nil },

-- Valid letter case. { word = "колан", expected_ret = true, expected_rule = nil }, { word = "Станислав", expected_ret = true, expected_rule = nil }, { word = "КОРЕКОМ", expected_ret = true, expected_rule = nil },

-- Invalid letter case { word = "бърЗовар", expected_ret = false, expected_rule = "valid letter case" }, { word = "ОрангутаН", expected_ret = false, expected_rule = "valid letter case" }, { word = "траЛалаЛА", expected_ret = false, expected_rule = "valid letter case" }, -- Valid use of Ьь { word = "БЬОРК", expected_ret = true, expected_rule = nil }, { word = "Бьорк", expected_ret = true, expected_rule = nil }, { word = "миньор", expected_ret = true, expected_rule = nil }, { word = "бельо", expected_ret = true, expected_rule = nil }, -- Invalid use of Ьь { word = "Ьордан", expected_ret = false, expected_rule = "correct use of ьЬ" }, { word = "ьерархия", expected_ret = false, expected_rule = "correct use of ьЬ" }, { word = "конь", expected_ret = false, expected_rule = "correct use of ьЬ" }, { word = "коьот", expected_ret = false, expected_rule = "correct use of ьЬ" }, { word = "ваььар", expected_ret = false, expected_rule = "correct use of ьЬ" }, -- No alphabet mixing. { word = "Facebook", expected_ret = true, expected_rule = nil }, { word = "VIP", expected_ret = true, expected_rule = nil }, { word = "TOEFL", expected_ret = true, expected_rule = nil }, { word = "Туитър", expected_ret = true, expected_rule = nil }, -- Some alphabet mixing { word = "Fейсбук", expected_ret = false, expected_rule = "no alphabet mixing" }, { word = "VIП", expected_ret = false, expected_rule = "no alphabet mixing" }, -- Some of the a's and o's below are Latin vs. Cyrillic (tough to spot) { word = "караванaта", expected_ret = false, expected_rule = "no alphabet mixing" }, { word = "окoкорихме", expected_ret = false, expected_rule = "no alphabet mixing" }, }

local function make_heading(test_case) local word, opts = test_case.word, test_case.opts local word_arg = word and word or "nil" local opts_arg = opts and test_case.opts_hint or "nil" return ("is_valid(%s, %s)"):format(word_arg, opts_arg) end

function tests:check_single_case(test_case) local ret, rule = is_valid(test_case.word, test_case.opts) tests:header(make_heading(test_case)) tests:equals("return value", ret, test_case.expected_ret) tests:equals("failed rule", rule, test_case.expected_rule) end

function tests:test_is_valid for _, test_case in ipairs(test_cases) do		tests:check_single_case(test_case) end end

local malformed_input_cases = { 3.14, -- number true, -- boolean function(a, b) return a + b end, -- function {}, -- table }

function tests:check_malformed_input(input) local status, message = pcall(is_valid, input) local ret = { status = status, message = mw.ustring.gsub(message, "(.-): (.*)", "%2") }	tests:equals_deep("is_valid(" .. tostring(input) .. "): error", ret, {		status = false,		message = "Input must be a string!"	}) end

function tests:test_malformed_input for _, input in ipairs(malformed_input_cases) do		tests:check_malformed_input(input) end end

return tests