Skip to content

Commit

Permalink
Add assert_items_exclude matcher
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ImeevMA committed Jul 9, 2024
1 parent 82e8777 commit bca550d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Changelog
- Added `assert_items_exclude`.

## Unreleased

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, 3)
end
if comparator.any(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_covers(actual, expected)
if type(actual) ~= 'table' or type(expected) ~= 'table' then
failure('Argument 1 and 2 must be tables', nil, 3)
Expand Down
25 changes: 25 additions & 0 deletions luatest/comparator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ function comparator.is_subset(actual, expected)
return #expected_array - found_count
end

-- Checks that any element from the actual is an element from the expected.
function comparator.any(actual, expected)
if (type(actual) ~= 'table') or (type(expected) ~= 'table') then
return false
end

local expected_array = {}
local expected_casted = {}
for _, v in pairs(expected) do
table.insert(expected_array, v)
end

for _, a in pairs(actual) do
for i, b in ipairs(expected_array) do
if not expected_casted[i] then
expected_casted[i] = comparator.cast(b)
end
if comparator.equals(a, expected_casted[i]) then
return true
end
end
end
return false
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
21 changes: 21 additions & 0 deletions test/luaunit/assertions_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ 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, nil, {1})
assert_failure(subject, {}, nil)
assert_failure(subject, {1,2,3}, {1,2,3,4})
assert_failure(subject, {1,2,3}, {1,1,2,3})

subject({}, {1})
subject({},{})
subject({nil},{nil})
subject({1},{})
end

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

0 comments on commit bca550d

Please sign in to comment.