From c0fce44238798274f568d7db8e7b597a752e1811 Mon Sep 17 00:00:00 2001 From: Oleg Chaplashkin Date: Tue, 24 Oct 2023 14:46:04 +0400 Subject: [PATCH] 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") 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:1 bar` and `bad`. 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 equals. In fact, a comparison will be performed between `bar` and `foo:1: bar`. 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 --- luatest/assertions.lua | 3 ++- test/assertions_test.lua | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/luatest/assertions.lua b/luatest/assertions.lua index 1e183936..6902133e 100644 --- a/luatest/assertions.lua +++ b/luatest/assertions.lua @@ -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 diff --git a/test/assertions_test.lua b/test/assertions_test.lua index 1a6ab6ee..40815a13 100644 --- a/test/assertions_test.lua +++ b/test/assertions_test.lua @@ -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}}) + + +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