From ae503e9498553d50cc145d26f4b3457276fc14ba Mon Sep 17 00:00:00 2001 From: Jackson Moore Date: Fri, 4 Sep 2026 17:46:24 +0200 Subject: Share test assertion helpers --- tests/assertions.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/assertions.lua (limited to 'tests/assertions.lua') diff --git a/tests/assertions.lua b/tests/assertions.lua new file mode 100644 index 0000000..fb97be0 --- /dev/null +++ b/tests/assertions.lua @@ -0,0 +1,40 @@ +local M = {} + +function M.same(expected, actual, message) + if expected ~= actual then + error((message or "values differ") + .. ": expected " .. tostring(expected) + .. ", got " .. tostring(actual), 2) + end +end + +function M.truthy(value, message) + if not value then + error(message or "value must be true", 2) + end +end + +function M.falsy(value, message) + if value then + error(message or "value must be false", 2) + end +end + +function M.fails(body, expected_text) + local ok, failure = pcall(body) + if ok then + error("operation must fail", 2) + end + if expected_text and not tostring(failure):find(expected_text, 1, true) then + error("failure does not contain '" .. expected_text .. "': " .. tostring(failure), 2) + end +end + +function M.list_same(expected, actual) + M.same(#expected, #actual, "list lengths differ") + for index = 1, #expected do + M.same(expected[index], actual[index], "list item " .. tostring(index) .. " differs") + end +end + +return M -- cgit v1.2.3