local p = require('Module:LineJoiner')
local m = {}
-- Helper function to simulate frame.args for test cases
local function simulateArgs(args)
local frame = {}
frame.args = args
return frame
end
-- Function to format output for comparison
local function formatOutput(output)
return output:gsub(" ", "_"):gsub("\n", "↵") -- 将每个字符用 [] 包裹
end
-- Define test cases with descriptive names and expected outputs
local testCases = {
["Single Newline Replacement"] = {
args = { "[SEP]", text = "Line 1\nLine 2\nLine 3" },
expected = "Line 1[SEP]Line 2[SEP]Line 3"
},
["Multiple Newlines Not Replaced"] = {
args = { text = "Line 1\n\nLine 2\n\n\nLine 3", "[SEP]" },
expected = "Line 1\n\nLine 2\n\n\nLine 3"
},
["No Newlines"] = {
args = { text = "Line 1 Line 2 Line 3", "[SEP]" },
expected = "Line 1 Line 2 Line 3"
},
["Only Newlines"] = {
args = { text = "\n\n", "[SEP]" },
expected = "\n\n"
},
}
-- Main function to run all tests and format the results
function m.runTests()
local results = {"== Test Results for Module:LineJoiner =="}
for testName, testCase in pairs(testCases) do
-- Run each test case and catch errors
local success, result = pcall(function()
return p.joinLines(simulateArgs(testCase.args))
end)
-- Format both actual and expected outputs for comparison
local formattedResult = formatOutput(result)
local formattedExpected = formatOutput(testCase.expected)
-- Evaluate the results of the test case
if success then
local isPassed = formattedResult == formattedExpected
if isPassed then
table.insert(results, ("=== %s ===\nResult: PASSED\nOutput: %s"):format(testName, formattedResult))
else
table.insert(results, ("=== %s ===\nResult: FAILED\nExpected: %s\nGot: %s"):format(testName, formattedExpected, formattedResult))
end
else
table.insert(results, ("=== %s ===\nError: %s"):format(testName, result))
end
end
return table.concat(results, "\n\n")
end
return m