Skip to content

Commit 290589e

Browse files
committed
Add assert_items_exclude matcher
This patch adds the `assert_items_exclude(actual, expected, message)` matcher. This matcher can be useful, for example, to verify that gc is working correctly.
1 parent 3ad7fae commit 290589e

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Add alias `--no-capture` for the option `-c` (gh-391).
2020
- Fix reporting of an assertion failure in `Server:exec()` in case verbose
2121
error serialization is enabled in Tarantool (gh-376).
22+
- Added `assert_items_exclude`.
2223

2324
## 1.0.1
2425

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ List of luatest functions
239239
+--------------------------------------------------------------------+-----------------------------------------------+
240240
| ``assert_eval_to_true (value[, message])`` | Alias for assert. |
241241
+--------------------------------------------------------------------+-----------------------------------------------+
242+
| ``assert_items_exclude (actual, expected[, message])`` | Checks that one table does not include any |
243+
| | items of another, irrespective of their keys. |
244+
+--------------------------------------------------------------------+-----------------------------------------------+
242245
| ``assert_items_include (actual, expected[, message])`` | Checks that one table includes all items of |
243246
| | another, irrespective of their keys. |
244247
+--------------------------------------------------------------------+-----------------------------------------------+

luatest/assertions.lua

+12
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ function M.assert_items_include(actual, expected, message)
411411
end
412412
end
413413

414+
--- Checks that one table does not include any items of another, irrespective of their keys.
415+
--
416+
-- @param actual
417+
-- @param expected
418+
-- @string[opt] message
419+
function M.assert_items_exclude(actual, expected, message)
420+
if not comparator.are_disjoint(expected, actual) then
421+
expected, actual = prettystr_pairs(expected, actual)
422+
fail_fmt(2, message, 'Expected no item values from: %s\nTo be present in: %s', expected, actual)
423+
end
424+
end
425+
414426
local function table_slice(actual, expected)
415427
if type(expected) ~= 'table' or type(actual) ~= 'table' then
416428
return actual

luatest/comparator.lua

+16
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ function comparator.is_subset(actual, expected)
7070
return #expected_array - found_count
7171
end
7272

73+
-- Checks that any element from the actual is an element from the expected.
74+
function comparator.are_disjoint(actual, expected)
75+
if (type(actual) ~= 'table') or (type(expected) ~= 'table') then
76+
return false
77+
end
78+
79+
for _, a in pairs(actual) do
80+
for _, b in pairs(expected) do
81+
if comparator.equals(a, b) then
82+
return false
83+
end
84+
end
85+
end
86+
return true
87+
end
88+
7389
-- This is a specialized metatable to help with the bookkeeping of recursions
7490
-- in table_equals(). It provides an __index table that implements utility
7591
-- functions for easier management of the table. The "cached" method queries

test/luaunit/assertions_test.lua

+26
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,32 @@ function g.test_assert_items_include()
333333
assert_failure(subject, {1,2,3}, {1,1,2,3})
334334
end
335335

336+
function g.test_assert_items_exclude()
337+
local subject = t.assert_items_exclude
338+
assert_failure(subject, {1,2,3}, {3,1,2})
339+
assert_failure(subject, {one=1,two=2,three=3}, {two=2,one=1,three=3})
340+
assert_failure(subject, {one=1,two=2,three=3}, {a=1,b=2,c=3})
341+
assert_failure(subject, {1,2,three=3}, {3,1,two=2})
342+
343+
assert_failure(subject, {1,2,3,4}, {3,1,2})
344+
assert_failure(subject, {1,1,2,3}, {3,1,2})
345+
346+
assert_failure(subject, nil, {1})
347+
assert_failure(subject, {}, nil)
348+
assert_failure(subject, {1,2,3}, {1,2,3,4})
349+
assert_failure(subject, {1,2,3}, {1,1,2,3})
350+
351+
subject({}, {1})
352+
subject({},{})
353+
subject({nil},{nil})
354+
subject({1},{})
355+
subject({one=1},{})
356+
subject({},{one=1})
357+
subject({1, 2, 3},{4, 5, 6, 7})
358+
subject({one=1, two=2, three=3},{four=4, five=5})
359+
subject({one=1, 2, 3},{four=4, 5})
360+
end
361+
336362
function g.test_assert_nan()
337363
assert_failure(t.assert_nan, "hi there!")
338364
assert_failure(t.assert_nan, nil)

0 commit comments

Comments
 (0)