summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJackson Moore <jacksonmoore@tuta.io>2026-09-04 17:46:24 +0200
committerJackson Moore <jacksonmoore@tuta.io>2026-09-04 17:46:24 +0200
commitae503e9498553d50cc145d26f4b3457276fc14ba (patch)
treeee8793df7baa4503dcfa13ee342a4e875e65d0d9 /tests
parent3ef0616d516d8c8059380566a29852474d95e801 (diff)
Share test assertion helpers
Diffstat (limited to 'tests')
-rw-r--r--tests/assertions.lua40
-rw-r--r--tests/host_adapter_contract.lua16
-rw-r--r--tests/plugin_smoke.lua16
-rw-r--r--tests/run.lua44
4 files changed, 53 insertions, 63 deletions
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
diff --git a/tests/host_adapter_contract.lua b/tests/host_adapter_contract.lua
index ff369d5..5d22a85 100644
--- a/tests/host_adapter_contract.lua
+++ b/tests/host_adapter_contract.lua
@@ -11,19 +11,9 @@ local composition_root = require("clever_f.composition_root")
local domain = require("clever_f.domain")
local host_adapter = require("clever_f.host_adapter")
-local function same(expected, actual, message)
- if expected ~= actual then
- error((message or "values differ")
- .. ": expected " .. tostring(expected)
- .. ", got " .. tostring(actual), 2)
- end
-end
-
-local function truthy(value, message)
- if not value then
- error(message or "value must be true", 2)
- end
-end
+local assertions = dofile(root .. "/tests/assertions.lua")
+local same = assertions.same
+local truthy = assertions.truthy
local exercised = {}
local host = host_adapter.new()
diff --git a/tests/plugin_smoke.lua b/tests/plugin_smoke.lua
index 3028723..c422490 100644
--- a/tests/plugin_smoke.lua
+++ b/tests/plugin_smoke.lua
@@ -2,19 +2,9 @@ local script = debug.getinfo(1, "S").source:sub(2)
local root = script:match("^(.*)/tests/plugin_smoke%.lua$") or "."
vim.opt.runtimepath:prepend(root)
-local function same(expected, actual, message)
- if expected ~= actual then
- error((message or "values differ")
- .. ": expected " .. tostring(expected)
- .. ", got " .. tostring(actual), 2)
- end
-end
-
-local function truthy(value, message)
- if not value then
- error(message or "value must be true", 2)
- end
-end
+local assertions = dofile(root .. "/tests/assertions.lua")
+local same = assertions.same
+local truthy = assertions.truthy
local function feed(keys)
vim.api.nvim_feedkeys(vim.keycode(keys), "xt", false)
diff --git a/tests/run.lua b/tests/run.lua
index 03aa725..131086f 100644
--- a/tests/run.lua
+++ b/tests/run.lua
@@ -28,6 +28,13 @@ local text_topology = require("clever_f.text_topology")
local target_plan = require("clever_f.target_plan")
local MemoryHost = require("clever_f.testing.memory_host")
+local assertions = dofile(root .. "/tests/assertions.lua")
+local same = assertions.same
+local truthy = assertions.truthy
+local falsy = assertions.falsy
+local fails = assertions.fails
+local list_same = assertions.list_same
+
local tests = {}
local passed = 0
@@ -35,43 +42,6 @@ local function test(name, body)
tests[#tests + 1] = { name = name, body = body }
end
-local function same(expected, actual, message)
- if expected ~= actual then
- error((message or "values differ")
- .. ": expected " .. tostring(expected)
- .. ", got " .. tostring(actual), 2)
- end
-end
-
-local function truthy(value, message)
- if not value then
- error(message or "value must be true", 2)
- end
-end
-
-local function falsy(value, message)
- if value then
- error(message or "value must be false", 2)
- end
-end
-
-local function 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
-
-local function list_same(expected, actual)
- same(#expected, #actual, "list lengths differ")
- for index = 1, #expected do
- same(expected[index], actual[index], "list item " .. tostring(index) .. " differs")
- end
-end
-
test("Position validates one-based byte coordinates", function()
local position = domain.Position.new(1, 1)
same(1, position.line)