Module:User:Oyunqi/ug-decl-noun/testcases

-- Unit tests for Module:User:Oyunqi/ug-decl-noun. local p = require('Module:UnitTests') --local m = require('Module:User:Oyunqi/ug-decl-noun') local m = require('Module:User:Oyunqi/ug-decl-noun1') local testcases = require('Module:User:Oyunqi/ug-decl-noun/testcases/data')

local sub = mw.ustring.gsub local match = mw.ustring.gmatch

function format_input(params) local par = "" if params[1] and params[1] ~= "" then par = par .. "|" .. params[1] end if params[2] and params[2] ~= "" then par = par .. "|" .. params[2] end if params["change"] and params["change"] ~= "" then par = par .. "|change=" .. params["change"] end if params["tongue"] and params["tongue"] ~= "" then par = par .. "|tongue=" .. params["tongue"] end str = " " str = str .. "   "    str = str .. "   "    str = str .. "   "    str = str .. "    "    return str end

function format_output(output) str ="" i = 1 for token in match(output,"[^%s]+") do	  sep = " " -- " " if i % 2 == 0 then sep = " " end str = str .. token.. sep i = i + 1 end str = str .. " "

return str end

function fill_args(params, args) params = sub(params, "|", " ") i=1 --log =params for token in match(params,"[^%s]+") do     t = sub(token, "=", " ") j =1 ts={} for tt in match(t,"[^%s]+") do         ts[j]=tt j=j+1 end

if j ==3 then args[ts[1]] =ts[2] --log=log..ts[1].."-"..ts[2].." " else args[i] = ts[1] --log=log..i.."-"..ts[1].." " i=i+1 end end args["test"] =1 end

function table.show(t, name, indent) local cart    -- a container local autoref -- for self references

-- counts the number of elements in a table  local function tablecount(t)      local n = 0      for _, _ in pairs(t) do n = n+1 end      return n   end -- (RiciLake) returns true if the table is empty local function isemptytable(t) return next(t) == nil end

local function basicSerialize (o) local so = tostring(o) if type(o) == "function" then local info = debug.getinfo(o, "S") -- info.name is nil because o is not a calling level if info.what == "C" then return string.format("%q", so .. ", C function") else -- the information is defined through lines return string.format("%q", so .. ", defined in (" .. info.linedefined .. "-" .. info.lastlinedefined .. ")" .. info.source) end elseif type(o) == "number" or type(o) == "boolean" then return so     else return string.format("%q", so) end end

local function addtocart (value, name, indent, saved, field) indent = indent or "" saved = saved or {} field = field or name

cart = cart .. indent .. field

if type(value) ~= "table" then cart = cart .. " = " .. basicSerialize(value) .. ";\n" else if saved[value] then cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n" autoref = autoref .. name .. " = " .. saved[value] .. ";\n" else saved[value] = name --if tablecount(value) == 0 then if isemptytable(value) then cart = cart .. " = {};\n" else cart = cart .. " = {\n" for k, v in pairs(value) do                 k = basicSerialize(k) local fname = string.format("%s[%s]", name, k)                 field = string.format("[%s]", k)                  -- three spaces between levels addtocart(v, fname, indent .. "  ", saved, field) end cart = cart .. indent .. "};\n" end end end end

name = name or "__unnamed__" if type(t) ~= "table" then return name .. " = " .. basicSerialize(t) end cart, autoref = "", "" addtocart(t, name, indent) return cart .. autoref end

function p:test_case for params, expected in pairs(testcases) do		if type(expected) == "table" then local frame={} frame.args={} fill_args(params, frame.args) local word = m.check(frame) local nouns = m.createCases(word) if expected["case0"] and expected["case0"] ~= {} then local result = table.concat(nouns["case0"], " ") self:equals(format_input(frame.args), format_output(result), format_output(expected["case0"])) end if expected["case0.gen"] and expected["case0.gen"] ~= "" then local result = table.concat(nouns["case0.gen"], " ") self:equals(format_input(frame.args), format_output(result), format_output(expected["case0.gen"])) end if expected["case0.lqu"] and expected["case0.lqu"] ~= "" then local result = table.concat(nouns["case0.lqu"], " ") self:equals(format_input(frame.args), format_output(result), format_output(expected["case0.lqu"])) end if expected["case1s"] and expected["case1s"] ~= {} then local result = table.concat(nouns["case1s"], " ") self:equals(format_input(frame.args), format_output(result), format_output(expected["case1s"])) end --self:equals(format_input(frame.args), table.show(nouns), table.show(expected)) else if expected ~= "" then local frame={} frame.args={} fill_args(params, frame.args) local word = m.check(frame) --result = word local nouns = m.createCases(word) --				result = nouns				local temp = ""				--[				for k, v in pairs(result) do					temp = temp .. tostring(k) .. "=>"					if type(v) == "table" then						for tk, tv in pairs(v) do							temp = temp .. tostring(kt) .. "==>" .. tv						end						-- temp = temp .. table.concat(v, " ")					else						temp = temp  .. v .. " "					end				end				result = temp				-- --				local word = m.check(frame)				local nouns = m.createCases(word) local result = table.concat(nouns["case0"], " ") self:equals(format_input(frame.args) ,format_output(result), format_output(expected)) -- self:equals(format_input(frame.args), table.show(nouns), table.show(expected)) end end end end

return p