Skip to content

Commit 2a26c32

Browse files
Oleg Chaplashkinylobankov
Oleg Chaplashkin
authored andcommitted
Improved error message for one assert function
> What's the problem? If the user makes a mistake in the `assert_error_msg_content_equals`: t.assert_error_msg_content_equals("bad", error, "foo:1: bar") where: "bad" - expected error message error - built-in function (there may be another function here) "foo:1: bar" - argument for the function then the following error will be returned: Error message expected: "bad" Error message received: "foo:1: bar" The user thinks that a comparison is being made between `foo:1: bar` and `bad` strings. He notices this error and at the same time can write the following: t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar") Now he will get the following error: Error message expected: "foo:1: bar" Error message received: "foo:1: bar" It seems that the function doesn't work cause these strings are equal. In fact, a comparison will be performed between `foo:1: bar` (expected) and `bar` (actual, because `foo:1:` will be dropped from the error message by regex). The next use of the function will be correct: t.assert_error_msg_content_equals("bar", error, "foo:1: bar") > What's the solution? It is necessary to save the result of conversion after the `gsub`. So this way the user will see the actual strings that are being compared: t.assert_error_msg_content_equals("bad", error, "foo:1: bar") Error message expected: "bad" Error message received: "bar" Close #316
1 parent 9c7710e commit 2a26c32

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

luatest/assertions.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...
472472
end
473473
local differ = false
474474
if stripFileAndLine then
475-
if error_msg:gsub("^.+:%d+: ", "") ~= expectedMsg then
475+
error_msg = error_msg:gsub("^.+:%d+: ", "")
476+
if error_msg ~= expectedMsg then
476477
differ = true
477478
end
478479
else

test/assertions_test.lua

+22
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,25 @@ g.test_assert_comparisons_error = function()
6262
helper.assert_failure_contains('must supply only number arguments.\n'..
6363
'Arguments supplied: \"one\", 3', t.assert_gt, 'one', 3)
6464
end
65+
66+
local function external_error_fn(msg)
67+
error(msg)
68+
end
69+
70+
local g2 = t.group('g2', {
71+
{fn = external_error_fn},
72+
{fn = error},
73+
{fn = function(msg) error(msg) end}
74+
})
75+
76+
g2.test_assert_error_msg_content_equals = function(cg)
77+
local msg = "error"
78+
t.assert_error_msg_content_equals(msg, cg.params.fn, msg)
79+
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: " .. msg)
80+
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:123: " .. msg)
81+
t.assert_error_msg_content_equals(msg, cg.params.fn, "/foo/bar.lua:1: " .. msg)
82+
t.assert_error_msg_content_equals(msg, cg.params.fn, ".../foo/bar.lua:1: " .. msg)
83+
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: foo.bar:1: " .. msg)
84+
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: .../foo/bar.lua:1: " .. msg)
85+
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar.bar:1: foo.bar.bar:1: " .. msg)
86+
end

0 commit comments

Comments
 (0)