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:50, 8 June 2024 (<li> handling). 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 = {}

-- Some testcases need to be enclosed in <ul>.
function generate_optional_list_tags(testcase)
    if find(testcase, "<li>") then
        return "<ul>", "</ul>"
    else
        return "", ""
    end
end

-- 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]
            
            -- Use <ul> and </ul> tags when needed.
            list_prefix, list_suffix = generate_optional_list_tags(input_data)
            
            unit_tests_module:preprocess_equals(
                list_prefix .. "{{#invoke:" .. module_to_test .. "|" .. input_data .. "}}" .. list_suffix,
                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]
            
            -- Use <ul> and </ul> tags when needed.
            list_prefix, list_suffix = generate_optional_list_tags(input_data)
            
            unit_tests_module:preprocess_equals_preprocess(
                list_prefix .. "{{#invoke:" .. module_to_test .. "|" .. input_data .. "}}" .. list_suffix,
                list_prefix .. "{{#invoke:" .. reference_module .. "|" .. input_data .. "}}" .. list_suffix
            )
        end
    end
    
    return unit_tests_module
end

return p