Skip to content

Commit 105c69d

Browse files
nshyylobankov
authored andcommitted
Add assert_error_covers(expected, fn, ...)
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
1 parent 15dbf75 commit 105c69d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add logging to unified file (gh-324).
66
- Add memory leak detection during server process execution (gh-349).
77
- Improve `luatest.log` function if a `nil` value is passed (gh-360).
8+
- Added `assert_error_covers`.
89

910
## 1.0.1
1011

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ List of luatest functions
190190
+--------------------------------------------------------------------+-----------------------------------------------+
191191
| ``assert_error_msg_matches (pattern, fn, ...)`` | |
192192
+--------------------------------------------------------------------+-----------------------------------------------+
193+
| ``assert_error_covers (expected, fn, ...)`` | Checks that actual error map includes expected|
194+
| | one. |
195+
+--------------------------------------------------------------------+-----------------------------------------------+
193196
| ``assert_eval_to_false (value[, message])`` | Alias for assert_not. |
194197
+--------------------------------------------------------------------+-----------------------------------------------+
195198
| ``assert_eval_to_true (value[, message])`` | Alias for assert. |

luatest/assertions.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,31 @@ function M.assert_error_msg_matches(pattern, fn, ...)
563563
end
564564
end
565565

566+
--- Checks that error raised by function is table that includes expected one.
567+
---
568+
--- If error object supports unpack() method (like Tarantool errors) then
569+
--- error is unpacked to get the table to be compared.
570+
--
571+
-- @tab expected
572+
-- @func fn
573+
-- @param ... arguments for function
574+
function M.assert_error_covers(expected, fn, ...)
575+
local ok, actual = pcall(fn, ...)
576+
if ok then
577+
fail_fmt(2, nil,
578+
'Function successfully returned: %s\nExpected error: %s',
579+
prettystr(actual), prettystr(expected))
580+
end
581+
if actual.unpack ~= nil then
582+
actual = actual:unpack()
583+
end
584+
if type(actual) ~= 'table' or not table_covers(actual, expected) then
585+
actual, expected = prettystr_pairs(actual, expected)
586+
fail_fmt(2, nil, 'Error expected: %s\nError received: %s\n',
587+
expected, actual)
588+
end
589+
end
590+
566591
--- Alias for @{assert}.
567592
--
568593
-- @param value

test/luaunit/assertions_error_test.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,26 @@ function g.test_assert_errorMsgMatches()
118118
assert_failure(t.assert_error_msg_matches, ' This is an error', f_with_error, x)
119119
assert_failure(t.assert_error_msg_matches, "This", f_with_table_error, 33)
120120
end
121+
122+
function g.test_assert_errorCovers()
123+
-- function executes successfully
124+
assert_failure(t.assert_error_covers, {}, f, 1)
125+
-- function expected to raise a table or has unpack()
126+
assert_failure(t.assert_error_covers, {}, f_with_error, 1)
127+
128+
-- good error coverage, error is table
129+
t.assert_error_covers({b = 2},
130+
function(a, b) error({a = a, b = b}) end, 1, 2)
131+
local error_with_unpack = function(a, b)
132+
local e = {}
133+
e.unpack = function()
134+
return {a = a, b = b}
135+
end
136+
error(e)
137+
end
138+
-- good error coverage, error has unpack()
139+
t.assert_error_covers({b = 2}, error_with_unpack, 1, 2)
140+
-- bad error coverage
141+
assert_failure(t.assert_error_covers, {b = 2},
142+
function(a, b) error({a = a, b = b}) end, 1, 3)
143+
end

0 commit comments

Comments
 (0)