Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_items_exclude matcher #383

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Add alias `--no-capture` for the option `-c` (gh-391).
- Fix reporting of an assertion failure in `Server:exec()` in case verbose
error serialization is enabled in Tarantool (gh-376).
- Added `assert_items_exclude`.

## 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 @@ -239,6 +239,9 @@ List of luatest functions
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_eval_to_true (value[, message])`` | Alias for assert. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_items_exclude (actual, expected[, message])`` | Checks that one table does not include any |
| | items of another, irrespective of their keys. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_items_include (actual, expected[, message])`` | Checks that one table includes all items of |
| | another, irrespective of their keys. |
+--------------------------------------------------------------------+-----------------------------------------------+
Expand Down
15 changes: 15 additions & 0 deletions luatest/assertions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,21 @@ function M.assert_items_include(actual, expected, message)
end
end

--- Checks that one table does not include any items of another, irrespective of their keys.
--
-- @param actual
-- @param expected
-- @string[opt] message
function M.assert_items_exclude(actual, expected, message)
if type(actual) ~= 'table' or type(expected) ~= 'table' then
failure('Argument 1 and 2 must be tables', nil, 2)
end
if not comparator.are_disjoint(expected, actual) then
expected, actual = prettystr_pairs(expected, actual)
fail_fmt(2, message, 'Expected no item values from: %s\nTo be present in: %s', expected, actual)
end
end

local function table_slice(actual, expected)
if type(expected) ~= 'table' or type(actual) ~= 'table' then
return actual
Expand Down
17 changes: 17 additions & 0 deletions luatest/comparator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ function comparator.is_subset(actual, expected)
return #expected_array - found_count
end

-- Returns false if 'actual' or 'expected' are not tables or their value sets
-- intersect. Returns true otherwise.
function comparator.are_disjoint(actual, expected)
if (type(actual) ~= 'table') or (type(expected) ~= 'table') then
return false
locker marked this conversation as resolved.
Show resolved Hide resolved
end

for _, a in pairs(actual) do
for _, b in pairs(expected) do
if comparator.equals(a, b) then
return false
end
end
end
return true
end

-- This is a specialized metatable to help with the bookkeeping of recursions
-- in table_equals(). It provides an __index table that implements utility
-- functions for easier management of the table. The "cached" method queries
Expand Down
33 changes: 33 additions & 0 deletions test/luaunit/assertions_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,39 @@ function g.test_assert_items_include()
assert_failure(subject, {1,2,3}, {1,1,2,3})
end

function g.test_assert_items_exclude()
local subject = t.assert_items_exclude
assert_failure(subject, {1,2,3}, {3,1,2})
assert_failure(subject, {one=1,two=2,three=3}, {two=2,one=1,three=3})
assert_failure(subject, {one=1,two=2,three=3}, {a=1,b=2,c=3})
assert_failure(subject, {1,2,three=3}, {3,1,two=2})

assert_failure(subject, {1,2,3,4}, {3,1,2})
assert_failure(subject, {1,1,2,3}, {3,1,2})
assert_failure(subject, {1,2,3}, {1,2,3,4})
assert_failure(subject, {1,2,3}, {1,1,2,3})

assert_failure(subject, nil, {1})
assert_failure(subject, {}, nil)
assert_failure(subject, 1, {1})
assert_failure(subject, {}, 1)
assert_failure(subject, 'asd', {one = 'asd', two = 'dsa', three = 1})
assert_failure(subject, {one = 'asd', two = 'dsa', three = 1}, 'dsa')
assert_failure(subject, 1, 1)
assert_failure(subject, nil, 1)
assert_failure(subject, 1, nil)

subject({}, {1})
subject({},{})
subject({nil},{nil})
subject({1},{})
locker marked this conversation as resolved.
Show resolved Hide resolved
subject({one=1},{})
subject({},{one=1})
subject({1, 2, 3},{4, 5, 6, 7})
subject({one=1, two=2, three=3},{four=4, five=5})
subject({one=1, 2, 3},{four=4, 5})
locker marked this conversation as resolved.
Show resolved Hide resolved
end

function g.test_assert_nan()
assert_failure(t.assert_nan, "hi there!")
assert_failure(t.assert_nan, nil)
Expand Down
Loading