Skip to content

Commit cab46b4

Browse files
Change conversion of box.error to string in error message assertions
Currently, error message assertions convert `box.error` to string using `tostring`. The string conversion metamethod of `box.error` is currently equivalent to the `message` field of `box.error`. In scope of tarantool/tarantool#9105, we are going to increase the verbosity of the string metamethod, and it will no longer be suitable for testing error messages. Let's replace `tostring(error)` conversions with `error.message` to retain the old behaviour. Needed for tarantool/tarantool#9105
1 parent f8a1c10 commit cab46b4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

luatest/assertions.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,18 @@ function M.assert_str_matches(value, pattern, start, final, message)
458458
end
459459
end
460460

461+
-- Convert an error object to an error message
462+
-- @param err error object
463+
-- @return error message
464+
local function error_to_msg(err)
465+
if type(err) == 'cdata' then
466+
-- We assume that this is a `box.error` instance.
467+
return err.message
468+
else
469+
return tostring(err)
470+
end
471+
end
472+
461473
local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...)
462474
local no_error, error_msg = pcall(func, ...)
463475
if no_error then
@@ -467,8 +479,7 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...
467479
failure(failure_message, nil, 3)
468480
end
469481
if type(expectedMsg) == "string" and type(error_msg) ~= "string" then
470-
-- table are converted to string automatically
471-
error_msg = tostring(error_msg)
482+
error_msg = error_to_msg(error_msg)
472483
end
473484
local differ = false
474485
if stripFileAndLine then
@@ -533,7 +544,7 @@ function M.assert_error_msg_contains(expected_partial, fn, ...)
533544
failure(failure_message, nil, 2)
534545
end
535546
if type(error_msg) ~= "string" then
536-
error_msg = tostring(error_msg)
547+
error_msg = error_to_msg(error_msg)
537548
end
538549
if not string.find(error_msg, expected_partial, nil, true) then
539550
error_msg, expected_partial = prettystr_pairs(error_msg, expected_partial)
@@ -555,7 +566,7 @@ function M.assert_error_msg_matches(pattern, fn, ...)
555566
failure(failure_message, nil, 2)
556567
end
557568
if type(error_msg) ~= "string" then
558-
error_msg = tostring(error_msg)
569+
error_msg = error_to_msg(error_msg)
559570
end
560571
if not str_match(error_msg, pattern) then
561572
pattern, error_msg = prettystr_pairs(pattern, error_msg)

0 commit comments

Comments
 (0)