Jump to content

Module:Wd/sandbox/testcases/common

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Janhrach (talk | contribs) at 10:33, 8 June 2024 (Clarified variables and changed generate_unit_tests_module_vs_module input format again). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module contains functions used in testcases.
local p = {}

-- generates unit test object to be returned by a specific testcase module
-- first argument: name of the module to test
-- second argument: a table with the structure described in testcaseTable; used for testcases and reference values
function p.generate_unit_tests_module_vs_table(module_to_test, inputs_and_outputs)
    local unit_tests_module = require("Module:UnitTests")
    
    function unit_tests_module:test()
        for _, input_and_output in ipairs(inputs_and_outputs) do
        	input_data = input_and_output[1]
        	expected_output = input_and_output[2]
        	
            unit_tests_module:preprocess_equals(
                "{{#invoke:" .. module_to_test .. "|" .. input_data .. "}}",
                expected_output
            )
        end
    end
    
    return unit_tests_module
end

-- for testing a module against a module
-- first argument: module to test
-- second argument: module to be used for correct outputs
-- third argument: a table with the structure described in testcaseTable; reference values are ignored
function p.generate_unit_tests_module_vs_module(module_to_test, reference_module, inputs_and_outputs)
    local unit_tests_module = require("Module:UnitTests")
    
    function unit_tests_module:test()
        for _, input_and_output in ipairs(inputs_and_outputs) do
        	input_data = input_and_output[1]
            unit_tests_module:preprocess_equals_preprocess(
                "{{#invoke:" .. module_to_test .. "|" .. input_data .. "}}",
                "{{#invoke:" .. reference_module .. "|" .. input_data .. "}}"
            )
        end
    end
    
    return unit_tests_module
end

return p