Skip to content

Commit

Permalink
Improved error message for one assert function
Browse files Browse the repository at this point in the history
> 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
  • Loading branch information
Oleg Chaplashkin authored and ylobankov committed Oct 25, 2023
1 parent 9c7710e commit 2a26c32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion luatest/assertions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...
end
local differ = false
if stripFileAndLine then
if error_msg:gsub("^.+:%d+: ", "") ~= expectedMsg then
error_msg = error_msg:gsub("^.+:%d+: ", "")
if error_msg ~= expectedMsg then
differ = true
end
else
Expand Down
22 changes: 22 additions & 0 deletions test/assertions_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ g.test_assert_comparisons_error = function()
helper.assert_failure_contains('must supply only number arguments.\n'..
'Arguments supplied: \"one\", 3', t.assert_gt, 'one', 3)
end

local function external_error_fn(msg)
error(msg)
end

local g2 = t.group('g2', {
{fn = external_error_fn},
{fn = error},
{fn = function(msg) error(msg) end}
})

g2.test_assert_error_msg_content_equals = function(cg)
local msg = "error"
t.assert_error_msg_content_equals(msg, cg.params.fn, msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:123: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "/foo/bar.lua:1: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, ".../foo/bar.lua:1: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: foo.bar:1: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar:1: .../foo/bar.lua:1: " .. msg)
t.assert_error_msg_content_equals(msg, cg.params.fn, "foo.bar.bar:1: foo.bar.bar:1: " .. msg)
end

0 comments on commit 2a26c32

Please sign in to comment.