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_one_of #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -53,6 +53,7 @@
- Group working directories of servers inside a replica set into one directory.
- Fix collecting coverage if tarantool binary has a suffix.
- Add `--no-clean` option to disable deletion of the var directory.
- Add new assert function `assert_one_of`.

## 0.5.7

2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -170,6 +170,8 @@ List of luatest functions
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_covers (actual, expected[, message])`` | Checks that actual map includes expected one. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_one_of (actual, expected[, message])`` | Checks that value is one of alternatives. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_lt (left, right[, message])`` | Compare numbers. |
+--------------------------------------------------------------------+-----------------------------------------------+
| ``assert_le (left, right[, message])`` | |
19 changes: 19 additions & 0 deletions luatest/assertions.lua
Original file line number Diff line number Diff line change
@@ -276,6 +276,25 @@ function M.assert_ge(left, right, message)
end
end

--- Check that value is one of alternatives.
--
-- @param actual
-- @param expected
-- @string[opt] message
function M.assert_one_of(actual, expected, message)
local res = false
for _, v in ipairs(expected) do
if comparator.equals(actual, v) then
res = true
break
end
end
if not res then
local str_actual, str_expected = prettystr_pairs(actual, expected)
fail_fmt(2, message, 'Assertion failed: %s must be one of %s', str_actual, str_expected)
end
end

--- Check that two values are not equal.
-- Tables are compared by value.
--
10 changes: 10 additions & 0 deletions test/luaunit/assertions_test.lua
Original file line number Diff line number Diff line change
@@ -969,6 +969,16 @@ function g.test_assertTableAdditions()
t.assert_not_equals({1,x=2,3,y=4}, {1,x=2,3})
end

function g.test_assert_one_of()
t.assert_one_of(1, {1,2,3})
t.assert_one_of({1}, {{1},{2},{3}})
t.assert_one_of("two", {"one", "two"})

assert_failure_contains("oh no", t.assert_one_of, 1, {2,3}, "oh no")
assert_failure_contains("Assertion failed: 1 must be one of {2, 3}", t.assert_one_of, 1, {2,3})
assert_failure_contains("Assertion failed: {1} must be one of {{2}, {3}}", t.assert_one_of, {1}, {{2},{3}})
end

g.test_assert_comparisons = function()
-- assert_lt
t.assert_lt(1.9, 2)