local mPages = require('Module:Pages')
local p = {}
-- Mocking mw.table for test environment
local mw = {
table = {
jsonEncode = function(t)
local result = "{"
for k, v in pairs(t) do
result = result .. string.format('"%s": "%s", ', tostring(k), tostring(v))
end
result = result:sub(1, -3) -- Remove the trailing comma and space
return result .. "}"
end,
},
}
-- Helper function to simulate frame.args for test cases
local function simulateArgs(args)
local frame = {}
frame.args = args
frame.extensionTag = function(self, tag, content, params)
return string.format("Mocked %s with params: %s", tag, mw.table.jsonEncode(params))
end
return frame
end
-- Define test cases with descriptive names
local testCases = {
["Basic Range"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "2",
debug="true"
}
return mPages.pages(args)
end,
["Range with Sections"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "5",
fromsection = "introduction",
tosection = "conclusion",
debug="true"
}
return mPages.pages(args)
end,
["Step"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "10",
step = "2",
debug="true"
}
return mPages.pages(args)
end,
["Exclude"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "10",
exclude = "2-4,8",
debug="true"
}
return mPages.pages(args)
end,
["Include"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
include = "2-4,9",
debug="true"
}
return mPages.pages(args)
end,
["Only Section"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "5",
onlysection = "english",
debug="true"
}
return mPages.pages(args)
end,
["Combined Parameters"] = function()
local args = simulateArgs{
index = "sandbox.djvu",
from = "1",
to = "10",
step = "2",
exclude = "4,6",
include = "9",
debug="true"
}
return mPages.pages(args)
end,
}
-- Main function to run all tests and format the results
function p.runTests()
local results = {"== Test Results for Module:Pages =="}
for testName, testFunc in pairs(testCases) do
local success, result = pcall(testFunc)
if success then
table.insert(results, ("=== %s ===\n%s"):format(testName, result))
else
table.insert(results, ("=== %s ===\nError: %s"):format(testName, result))
end
end
return table.concat(results, "\n\n")
end
return p