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:23, 8 June 2024 (use preprocess_equals and preprocess_equals_preprocess instead of the "many"-functions from Module:UnitTests (I used code from the said module, though the code is not large and original enough to justify in-code attribution), tabs to spaces, comment change). 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 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
            unit_tests_module:preprocess_equals(
                "{{#invoke:" .. module_to_test .. "|" .. input_and_output[1] .. "}}",
                input_and_output[2]
            )
        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: inputs for the modules
function p.generate_unit_tests_module_vs_module(module_to_test, reference_module, inputs)
    local unit_tests_module = require("Module:UnitTests")
    
    function unit_tests_module:test()
        for _, input_text in ipairs(inputs) do
            unit_tests_module:preprocess_equals_preprocess(
                "{{#invoke:" .. module_to_test .. "|" .. input_text .. "}}",
                "{{#invoke:" .. reference_module .. "|" .. input_text .. "}}"
            )
        end
    end
    
    return unit_tests_module
end

-- extracts test module inputs from a table with the structure described in testcaseTable into a table suitable for generate_unit_tests_module_vs_module
function p.format_module_inputs_for_module_vs_module(input_table)
    output_table = {}
    
    for i, j in ipairs(input_table) do
        output_table[i] = {j[1]}
    end
    
    return output_table
end

return p