Skip to content

Commit 33ee6fd

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 33ee6fd

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-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

+15
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,21 @@ 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 type(actual) ~= 'table' or type(expected) ~= 'table' then
421+
failure('Argument 1 and 2 must be tables', nil, 2)
422+
end
423+
if not comparator.are_disjoint(expected, actual) then
424+
expected, actual = prettystr_pairs(expected, actual)
425+
fail_fmt(2, message, 'Expected no item values from: %s\nTo be present in: %s', expected, actual)
426+
end
427+
end
428+
414429
local function table_slice(actual, expected)
415430
if type(expected) ~= 'table' or type(actual) ~= 'table' then
416431
return actual

luatest/comparator.lua

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

73+
-- Returns false if 'actual' or 'expected' are not tables or their value sets
74+
-- intersect. Returns true otherwise.
75+
function comparator.are_disjoint(actual, expected)
76+
if (type(actual) ~= 'table') or (type(expected) ~= 'table') then
77+
return false
78+
end
79+
80+
for _, a in pairs(actual) do
81+
for _, b in pairs(expected) do
82+
if comparator.equals(a, b) then
83+
return false
84+
end
85+
end
86+
end
87+
return true
88+
end
89+
7390
-- This is a specialized metatable to help with the bookkeeping of recursions
7491
-- in table_equals(). It provides an __index table that implements utility
7592
-- functions for easier management of the table. The "cached" method queries

test/luaunit/assertions_test.lua

+30
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,36 @@ 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+
assert_failure(subject, {1,2,3}, {1,2,3,4})
346+
assert_failure(subject, {1,2,3}, {1,1,2,3})
347+
348+
assert_failure(subject, nil, {1})
349+
assert_failure(subject, {}, nil)
350+
assert_failure(subject, 1, {1})
351+
assert_failure(subject, {}, 1)
352+
assert_failure(subject, 'asd', {one = 'asd', two = 'dsa', three = 1})
353+
assert_failure(subject, {one = 'asd', two = 'dsa', three = 1}, 'dsa')
354+
355+
subject({}, {1})
356+
subject({},{})
357+
subject({nil},{nil})
358+
subject({1},{})
359+
subject({one=1},{})
360+
subject({},{one=1})
361+
subject({1, 2, 3},{4, 5, 6, 7})
362+
subject({one=1, two=2, three=3},{four=4, five=5})
363+
subject({one=1, 2, 3},{four=4, 5})
364+
end
365+
336366
function g.test_assert_nan()
337367
assert_failure(t.assert_nan, "hi there!")
338368
assert_failure(t.assert_nan, nil)

0 commit comments

Comments
 (0)