Skip to content

Commit

Permalink
Change conversion of box.error to string in error message assertions
Browse files Browse the repository at this point in the history
Currently, error message assertions convert `box.error` to string using
`tostring`. `box.error`'s `__tostring` metamethod is currently
equivalent to the `message` field of `box.error`. In scope of
tarantool/tarantool#9105, we are going to increase the verbosity of
`box.error` `__tostring` metamethod, and it will no longer be
convenient for testing the error message. Let's replace `tostring(error)`
with `error.message` usag to retain the old behaviour.

Needed for tarantool/tarantool#9105
  • Loading branch information
CuriousGeorgiy committed Feb 22, 2024
1 parent f8a1c10 commit 8da205a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions luatest/assertions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ function M.assert_str_matches(value, pattern, start, final, message)
end
end

local function error_to_string(err)
if type(err) == 'cdata' then
-- We assume that this is a `box.error` instance.
return error_msg.message
else
return tostring(err)
end
end

local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...)
local no_error, error_msg = pcall(func, ...)
if no_error then
Expand All @@ -467,8 +476,7 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...
failure(failure_message, nil, 3)
end
if type(expectedMsg) == "string" and type(error_msg) ~= "string" then
-- table are converted to string automatically
error_msg = tostring(error_msg)
error_msg = error_to_string(error_msg)
end
local differ = false
if stripFileAndLine then
Expand Down Expand Up @@ -533,7 +541,7 @@ function M.assert_error_msg_contains(expected_partial, fn, ...)
failure(failure_message, nil, 2)
end
if type(error_msg) ~= "string" then
error_msg = tostring(error_msg)
error_msg = error_to_string(error_msg)
end
if not string.find(error_msg, expected_partial, nil, true) then
error_msg, expected_partial = prettystr_pairs(error_msg, expected_partial)
Expand All @@ -555,7 +563,7 @@ function M.assert_error_msg_matches(pattern, fn, ...)
failure(failure_message, nil, 2)
end
if type(error_msg) ~= "string" then
error_msg = tostring(error_msg)
error_msg = error_to_string(error_msg)
end
if not str_match(error_msg, pattern) then
pattern, error_msg = prettystr_pairs(pattern, error_msg)
Expand Down

0 comments on commit 8da205a

Please sign in to comment.