Files
factorio-scenario-ExpCluster/test/lua/framework.lua
T
bbassieandClaude Fable 5 e6552a39be Address review on the stubs and controller tests
- The controller tests run against a real Controller, which is side effect
  free while not started, with real users through its UserManager. The only
  fakes left are two spies which record broadcasts and permission pushes on
  their way through. Since any user update also applies auto assignment, the
  leave event is observed directly rather than through a role change.
- raise_event fills in the name and tick of the event the way factorio does,
  rather than wrapping the payload, and game.players finds players by index
  as well as by name through a metatable.
- names is generic over anything with a name property, so it lives on the
  suite; the save and load helper no longer calls on_load, the test calls the
  handler itself as it does for the other handlers.
- Sent messages and events are compared whole with deep_eq, which pins the
  event shape, the tick, and that the unassign key is absent rather than
  empty.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 16:18:09 +00:00

154 lines
4.9 KiB
Lua

--[[-- Test framework for plugin module tests
A suite is a collection of named tests. Test files declare them with
`Suite.test(name, function(env) ... end)`, naming each test after the function
or method it covers. Every test function receives a fresh environment from the
factory the suite was created with, so tests are independent and can not leak
state into each other.
]]
local Framework = {}
--- Turn a value into a string for failure messages
local function repr(value, depth)
if type(value) ~= "table" then return tostring(value) end
if depth > 3 then return "{...}" end
local parts = {}
for key, entry in pairs(value) do
parts[#parts + 1] = tostring(key) .. " = " .. repr(entry, depth + 1)
end
return "{ " .. table.concat(parts, ", ") .. " }"
end
--- Create a suite of tests, one is made per test file
--- @param make_env fun(): table Creates the environment given to each test
function Framework.new_suite(make_env)
--- @class Suite
local Suite = {
tests = {}, -- { name, fn } in declaration order
results = {}, -- { name, ok, detail? } accumulated across tests
}
local current_test --- @type string?
--- Declare a named test, its function receives a fresh environment
--- @param name string
--- @param fn fun(env: table)
function Suite.test(name, fn)
Suite.tests[#Suite.tests + 1] = { name = name, fn = fn }
end
--- Record a passing check within the current test
--- @param name string
function Suite.pass(name)
Suite.results[#Suite.results + 1] = {
name = current_test and (current_test .. ": " .. name) or name,
ok = true,
}
end
--- Record a failing check within the current test
--- @param name string
--- @param detail string?
function Suite.fail(name, detail)
Suite.results[#Suite.results + 1] = {
name = current_test and (current_test .. ": " .. name) or name,
ok = false,
detail = detail,
}
end
--- Check a condition
--- @param ok any Truthy when the check passed
--- @param name string
--- @param detail string?
function Suite.check(ok, name, detail)
if ok then
Suite.pass(name)
else
Suite.fail(name, detail)
end
end
--- Check two values or arrays are shallowly equal
function Suite.eq(a, b, name)
local equal
if type(a) ~= "table" or type(b) ~= "table" then
equal = a == b
else
equal = #a == #b
for i = 1, #a do
if a[i] ~= b[i] then equal = false break end
end
end
Suite.check(equal, name, ("%s ~= %s"):format(repr(a, 1), repr(b, 1)))
end
--- Recursive table equality, keys checked from both sides
local function deep_equal(a, b)
if type(a) ~= "table" or type(b) ~= "table" then return a == b end
for key, value in pairs(a) do
if not deep_equal(value, b[key]) then return false end
end
for key in pairs(b) do
if a[key] == nil then return false end
end
return true
end
--- Check two values are equal, comparing tables recursively
function Suite.deep_eq(a, b, name)
Suite.check(deep_equal(a, b), name, ("%s ~= %s"):format(repr(a, 1), repr(b, 1)))
end
--- Names of an array of values with a name property, such as roles,
--- players, forces, or events
--- @param values { name: string }[]
--- @return string[]
function Suite.names(values)
local rtn = {}
for index, value in ipairs(values) do
rtn[index] = value.name
end
return rtn
end
--- Sorted copy of an array of strings
function Suite.sorted(list)
local rtn = {}
for index, value in ipairs(list) do rtn[index] = value end
table.sort(rtn)
return rtn
end
--- Run every declared test, each against a fresh environment, and return
--- the results as JSON for the javascript side
function Suite.run()
for _, test in ipairs(Suite.tests) do
current_test = test.name
local ok, err = pcall(test.fn, make_env())
if not ok then
Suite.fail("did not error", tostring(err))
end
end
current_test = nil
local function escape(value)
return (value:gsub('[%c"\\]', function(c)
return string.format("\\u%04x", c:byte())
end))
end
local parts = {}
for index, result in ipairs(Suite.results) do
local detail = result.detail and string.format(',"detail":"%s"', escape(result.detail)) or ""
parts[index] = string.format('{"name":"%s","ok":%s%s}', escape(result.name), tostring(result.ok), detail)
end
return "[" .. table.concat(parts, ",") .. "]"
end
return Suite
end
return Framework