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

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 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

local function _validate_entry(value, keys) if not value:match("^[fe]:[a-z]+$") then return value .. ": does not match pattern '^[fe]:[a-z]+'" end if #keys ~= 2 then return value .. ": found " .. #keys .. " mapping(s), expected 2" end local key, pattern = nil, "^[v0-9][0-9][0-9][0-9]$" if keys[1]:match(pattern) then key = keys[2] elseif keys[2]:match(pattern) then key = keys[1] else return value .. ": no fund number key in '" .. table.concat(keys, "', '") .. "'"	end if key ~= value:sub(3) then return value .. ": non-number key '" .. key .. "' does not match value" end return "pass" 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 = '_validate_entry("' .. value .. '")' self:equals(name, _validate_entry(value, keys), "pass") end end return p