Module:Dv/validation

From ARK Wiki
Jump to navigation Jump to search
Template-info.png Documentation

local Dv = require('Module:Dv/staging')

local Utility = require('Module:Utility')
local ScribuntoUnitCfg = require('Module:ScribuntoUnit/config')


local ValidationManager = {}


function ValidationManager:new()
    local o = {
		-- Copy the data table to get rid of the meta tables
		data = Utility.deepcopy(mw.loadData('Module:Dv/MergedJsonData')),
		-- State
		currentId = nil,
		currentNode = nil,
        results = nil,
	}
    setmetatable(o, {__index = self})
    o.run = function(frame) return self:run(o) end
    return o
end


function ValidationManager:run(self)
    local tests = {}
    for name in pairs(self) do
        if name:find('^test') then
            table.insert(tests, name)
        end
    end

	local html = {
        '{| class="wikitable',
        '! Creature ID',
        '! Check Name',
        '! ',
    }
	for id, node in pairs(self.data) do
		self.currentId = id
		self.currentNode = node
		
        local creatureColSpan = 0
        local html2 = {}
        
        for _, test in ipairs(tests) do
            self.results = {}
            pcall(self[test], self, self.currentId, self.currentNode)

            creatureColSpan = creatureColSpan + #(self.results)
            html2[#html2+1] = string.format('| rowspan=%d | <code>%s</code>\n%s', #self.results, test, table.concat(self.results, '\n|-\n'))
        end

		html[#html+1] = '|-'
        html[#html+1] = string.format('| rowspan=%d | \'\'\'%s\'\'\'', creatureColSpan, id)
        html[#html+1] = table.concat(html2, '\n|-\n')
	end

    return table.concat(html, '\n') .. '\n|}'
end


function ValidationManager:push(indicator, context, color)
    if color ~= nil then
        table.insert(self.results, string.format('| <span style="color:%s">%s %s</span>', color, indicator, context))
    else
        table.insert(self.results, string.format('| %s %s', indicator, context))
    end
end


function ValidationManager:pushAuto(condition, context)
    local indicator = condition and ScribuntoUnitCfg.successIndicator or ScribuntoUnitCfg.failureIndicator
    local color = condition and 'green' or 'red'
    self:push(indicator, context, color)
end


function ValidationManager:assertTrue(condition, context)
    self:pushAuto(condition == true, context)
end


function ValidationManager:assertEquals(a, b, context)
    self:pushAuto(a == b, context)
end


function ValidationManager:assertFalse(condition, context)
    self:pushAuto(condition == false, context)
end


function ValidationManager:skip(condition, reason)
    if condition then
        self:push(ScribuntoUnitCfg.skippedIndicator, reason, 'grey')
    end
    return condition
end


return ValidationManager