Skip to content

Commit 32b673f

Browse files
Add assert_one_of
1 parent eef05dd commit 32b673f

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- Raise an error in the `Server:wait_for_condition()` function when
5151
the server process is terminated. This is useful to not wait for timeout, for example,
5252
when a server fails to start due to bad configuration.
53+
- Add `assert_one_of`.
5354

5455
## 0.5.7
5556

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ List of luatest functions
170170
+--------------------------------------------------------------------+-----------------------------------------------+
171171
| ``assert_covers (actual, expected[, message])`` | Checks that actual map includes expected one. |
172172
+--------------------------------------------------------------------+-----------------------------------------------+
173+
| ``assert_one_of (actual, expected[, message])`` | Checks that value is of on alternatives. |
174+
+--------------------------------------------------------------------+-----------------------------------------------+
173175
| ``assert_lt (left, right[, message])`` | Compare numbers. |
174176
+--------------------------------------------------------------------+-----------------------------------------------+
175177
| ``assert_le (left, right[, message])`` | |

luatest/assertions.lua

+19
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,25 @@ function M.assert_ge(left, right, message)
276276
end
277277
end
278278

279+
--- Check that value is one of alternates.
280+
--
281+
-- @param actual
282+
-- @param expected
283+
-- @string[opt] message
284+
function M.assert_one_of(actual, expected, message)
285+
local res = false
286+
for _, v in ipairs(expected) do
287+
if comparator.equals(actual, v) then
288+
res = true
289+
break
290+
end
291+
end
292+
if not res then
293+
local str_actual, str_expected = prettystr_pairs(actual, expected)
294+
fail_fmt(2, message, 'Assertion failed: %s must be one of %s', str_actual, str_expected)
295+
end
296+
end
297+
279298
--- Check that two values are not equal.
280299
-- Tables are compared by value.
281300
--

test/luaunit/assertions_test.lua

+10
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,16 @@ function g.test_assertTableAdditions()
969969
t.assert_not_equals({1,x=2,3,y=4}, {1,x=2,3})
970970
end
971971

972+
function g.test_assert_one_of()
973+
t.assert_one_of(1, {1,2,3})
974+
t.assert_one_of({1}, {{1},{2},{3}})
975+
t.assert_one_of("two", {"one", "two"})
976+
977+
assert_failure_contains("oh no", t.assert_one_of, 1, {2,3}, "oh no")
978+
assert_failure_contains("Assertion failed: 1 must be one of {2, 3}", t.assert_one_of, 1, {2,3})
979+
assert_failure_contains("Assertion failed: {1} must be one of {{2}, {3}}", t.assert_one_of, {1}, {{2},{3}})
980+
end
981+
972982
g.test_assert_comparisons = function()
973983
-- assert_lt
974984
t.assert_lt(1.9, 2)

0 commit comments

Comments
 (0)