Skip to content

Commit fc5c42e

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 fc5c42e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

luatest/assertions.lua

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

461+
local function error_to_string(err)
462+
if type(err) == 'cdata' then
463+
-- We assume that this is a `box.error` instance.
464+
return error_msg.message
465+
else
466+
return tostring(err)
467+
end
468+
end
469+
461470
local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...)
462471
local no_error, error_msg = pcall(func, ...)
463472
if no_error then
@@ -467,8 +476,7 @@ local function _assert_error_msg_equals(stripFileAndLine, expectedMsg, func, ...
467476
failure(failure_message, nil, 3)
468477
end
469478
if type(expectedMsg) == "string" and type(error_msg) ~= "string" then
470-
-- table are converted to string automatically
471-
error_msg = tostring(error_msg)
479+
error_msg = error_to_string(error_msg)
472480
end
473481
local differ = false
474482
if stripFileAndLine then
@@ -533,7 +541,7 @@ function M.assert_error_msg_contains(expected_partial, fn, ...)
533541
failure(failure_message, nil, 2)
534542
end
535543
if type(error_msg) ~= "string" then
536-
error_msg = tostring(error_msg)
544+
error_msg = error_to_string(error_msg)
537545
end
538546
if not string.find(error_msg, expected_partial, nil, true) then
539547
error_msg, expected_partial = prettystr_pairs(error_msg, expected_partial)
@@ -555,7 +563,7 @@ function M.assert_error_msg_matches(pattern, fn, ...)
555563
failure(failure_message, nil, 2)
556564
end
557565
if type(error_msg) ~= "string" then
558-
error_msg = tostring(error_msg)
566+
error_msg = error_to_string(error_msg)
559567
end
560568
if not str_match(error_msg, pattern) then
561569
pattern, error_msg = prettystr_pairs(pattern, error_msg)

0 commit comments

Comments
 (0)