From 01b7dd000f7aff7d3c9c3cecf091094331d90f03 Mon Sep 17 00:00:00 2001 From: TurtleP Date: Fri, 24 Mar 2023 23:24:24 -0400 Subject: [PATCH] fix assert:one_of for non-string values --- assert.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/assert.lua b/assert.lua index 8998452..8dd62ad 100644 --- a/assert.lua +++ b/assert.lua @@ -82,6 +82,26 @@ 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 + + local values = {} + for index = 1, #t do + values[index] = tostring(t[index]) + end + + error(("assertion failed: %s not one of %s %s"):format( + tostring(a), + table.concat(values, ", "), + _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)