Skip to content

Commit 4ecddf6

Browse files
committed
Add assert_match function
1 parent 3327dcb commit 4ecddf6

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lua/assertions.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ function assertions.assert_ne(left, right, message)
2525
end
2626
end
2727

28+
function assertions.assert_match(obj, pattern, message)
29+
if not tostring(obj):match(pattern) then
30+
if message ~= nil then
31+
message = string.format("assertion `obj:match(pattern)` failed: %s", tostring(message))
32+
else
33+
message = "assertion `obj:match(pattern)` failed!"
34+
end
35+
local frame_level = opts.level or 2
36+
error(string.format("%s\n pattern: %s\n obj: %s", message, pattern, tostring(obj)), frame_level)
37+
end
38+
end
39+
2840
local function next_level(level, k)
2941
if type(k) == "string" then
3042
return level .. '["' .. k .. '"]'

lua/testing.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ function TestContext.assert_eq(a, b, msg)
3636
assertions.assert_eq(a, b, msg)
3737
end
3838

39+
function TestContext.assert_ne(a, b, msg)
40+
assertions.assert_ne(a, b, msg)
41+
end
42+
43+
function TestContext.assert_match(a, b, msg)
44+
assertions.assert_match(a, b, msg)
45+
end
46+
3947
function TestContext.assert_same(a, b, msg)
4048
assertions.assert_same(a, b, msg)
4149
end
@@ -100,7 +108,7 @@ function Testing:_run_single_test(test)
100108
-- Run the test
101109
local test_ok, test_err = pcall(test.func, ctx)
102110
if not test_ok then
103-
if test_err and test_err:match("^__SKIP__:") then
111+
if type(test_err) == "string" and test_err:match("^__SKIP__:") then
104112
success, err = "skip", test_err:match("^__SKIP__: (.*)")
105113
else
106114
success, err = false, test_err

tests/lua/assertions_tests.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local assertions = require("@assertions")
22

33
local assert_eq = assertions.assert_eq
44
local assert_ne = assertions.assert_ne
5+
local assert_match = assertions.assert_match
56
local assert_same = assertions.assert_same
67

78
testing:test("assert_eq", function()
@@ -52,6 +53,23 @@ testing:test("assert_ne", function()
5253
assert(err:match(" right: nil"))
5354
end)
5455

56+
testing:test("assert_match", function()
57+
assert_match("hello world", "hello")
58+
assert_match("12345", "%d+")
59+
60+
local ok, err = pcall(assert_match, "hello world", "bye")
61+
assert(not ok)
62+
assert(err:match("assertion `obj:match%(pattern%)` failed!"))
63+
assert(err:match(" pattern: bye"))
64+
assert(err:match(" obj: hello world"))
65+
66+
ok, err = pcall(assert_match, "foo", "bar", "custom message")
67+
assert(not ok)
68+
assert(err:match("assertion `obj:match%(pattern%)` failed: custom message"))
69+
assert(err:match(" pattern: bar"))
70+
assert(err:match(" obj: foo"))
71+
end)
72+
5573
testing:test("assert_same", function()
5674
assert_same(1, 1)
5775
assert_same(nil, nil)

0 commit comments

Comments
 (0)