mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-21 21:54:35 +00:00
Merge pull request #64 from TurtleP/turtlep/assert_one_of
add `assert:one_of`
This commit is contained in:
commit
3421c93848
15
assert.lua
15
assert.lua
@ -82,6 +82,21 @@ function assert:type_or_nil(a, t, msg, stack_level)
|
||||
return a
|
||||
end
|
||||
|
||||
--assert a value is one of those in a table of options
|
||||
function assert:one_of(a, t, msg, stack_level)
|
||||
for _, value in ipairs(t) do
|
||||
if value == a then
|
||||
return a
|
||||
end
|
||||
end
|
||||
|
||||
error(("assertion failed: %s not one of %s %s"):format(
|
||||
tostring(a),
|
||||
table.concat(t, ", "),
|
||||
_extra(msg)
|
||||
), 2 + (stack_level or 0))
|
||||
end
|
||||
|
||||
--replace everything in assert with nop functions that just return their second argument, for near-zero overhead on release
|
||||
function assert:nop()
|
||||
local nop = function(_, a)
|
||||
|
Loading…
Reference in New Issue
Block a user