Module:Vanguard funds/data/testcases

-- Unit tests for Module:Vanguard funds/data. Click talk page to run tests. -- Example usage: --  -- Console example: --  mw.log(p.run_tests(mw.getCurrentFrame))

local p = require('Module:UnitTests')

local _data = nil

local function _invert(data_) local inverted = {} for key, value in pairs(data_) do		inverted[value] = inverted[value] or {} table.insert(inverted[value], key) end return inverted end

function p:test__initial_data_load _data = mw.loadData('Module:Vanguard funds/data') self:equals("_data ~= nil", _data ~= nil, true) self:equals("type(_data)", type(_data), "table") end

local _test_case_failures = 0

function p:_expect(value, condition, actual) local pass = actual and true or false if not pass then self:equals(value .. ": " .. condition, pass, true) _test_case_failures = _test_case_failures + 1 end end

function p:_result if _test_case_failures == 0 then return "pass" else return "fail, " .. _test_case_failures .. " failing test case(s)" end end

function p:_validate_entry(value, keys) _test_case_failures = 0

self:_expect(value,			"value should match pattern '^[fe]:[a-z]+$'",			value:match("^[fe]:[a-z]+$")) self:_expect(value,			"keys ({'" .. table.concat(keys, "', '") .. "}') should have two entries",			#keys == 2)

if #keys == 2 then local number, other, pattern = nil, nil, "^[v0-9][0-9][0-9][0-9]$" if keys[1]:match(pattern) then number, other = unpack(keys) else other, number = unpack(keys) end self:_expect(value,				"one key ('" .. number .. "') should match pattern '" .. pattern .. "'",				number:match(pattern)) self:_expect(value,				"one key ('" .. other .. "') should be '" .. value:sub(3) .. "'",				other == value:sub(3)) end

return self:_result end

function p:test_all_entries_valid if _data == nil then self:equals("_data ~= nil", _data ~= nil, true) return end local inverted = _invert(_data) for value, keys in pairs(inverted) do		local name = 'self:_validate_entry('				.. '"' .. value .. '"'				.. ', {"' .. table.concat(keys, '", "') .. '"})' self:equals(name, self:_validate_entry(value, keys), "pass") end end

return p