Skip to content

Commit

Permalink
Add assert_error_covers(expected, fn, ...)
Browse files Browse the repository at this point in the history
It checks that `fn` raises error of type table that includes `expected`
table. Altenatively error can be Tarantool box error. In this case
error in converted to table using `error:unpack()` first.

Required for tarantool/tarantool#9111
  • Loading branch information
nshy authored and ylobankov committed Mar 29, 2024
1 parent 15dbf75 commit 105c69d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add logging to unified file (gh-324).
- Add memory leak detection during server process execution (gh-349).
- Improve `luatest.log` function if a `nil` value is passed (gh-360).
- Added `assert_error_covers`.

## 1.0.1

Expand Down
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ List of luatest functions
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_error_msg_matches (pattern, fn, ...)`` | |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_error_covers (expected, fn, ...)`` | Checks that actual error map includes expected|
| | one. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_eval_to_false (value[, message])`` | Alias for assert_not. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_eval_to_true (value[, message])`` | Alias for assert. |
Expand Down
25 changes: 25 additions & 0 deletions luatest/assertions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,31 @@ function M.assert_error_msg_matches(pattern, fn, ...)
end
end

--- Checks that error raised by function is table that includes expected one.
---
--- If error object supports unpack() method (like Tarantool errors) then
--- error is unpacked to get the table to be compared.
--
-- @tab expected
-- @func fn
-- @param ... arguments for function
function M.assert_error_covers(expected, fn, ...)
local ok, actual = pcall(fn, ...)
if ok then
fail_fmt(2, nil,
'Function successfully returned: %s\nExpected error: %s',
prettystr(actual), prettystr(expected))
end
if actual.unpack ~= nil then
actual = actual:unpack()
end
if type(actual) ~= 'table' or not table_covers(actual, expected) then
actual, expected = prettystr_pairs(actual, expected)
fail_fmt(2, nil, 'Error expected: %s\nError received: %s\n',
expected, actual)
end
end

--- Alias for @{assert}.
--
-- @param value
Expand Down
23 changes: 23 additions & 0 deletions test/luaunit/assertions_error_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ function g.test_assert_errorMsgMatches()
assert_failure(t.assert_error_msg_matches, ' This is an error', f_with_error, x)
assert_failure(t.assert_error_msg_matches, "This", f_with_table_error, 33)
end

function g.test_assert_errorCovers()
-- function executes successfully
assert_failure(t.assert_error_covers, {}, f, 1)
-- function expected to raise a table or has unpack()
assert_failure(t.assert_error_covers, {}, f_with_error, 1)

-- good error coverage, error is table
t.assert_error_covers({b = 2},
function(a, b) error({a = a, b = b}) end, 1, 2)
local error_with_unpack = function(a, b)
local e = {}
e.unpack = function()
return {a = a, b = b}
end
error(e)
end
-- good error coverage, error has unpack()
t.assert_error_covers({b = 2}, error_with_unpack, 1, 2)
-- bad error coverage
assert_failure(t.assert_error_covers, {b = 2},
function(a, b) error({a = a, b = b}) end, 1, 3)
end

0 comments on commit 105c69d

Please sign in to comment.