Skip to content

Commit db7cb39

Browse files
committed
assert() can handle non-string errors.
1 parent fb114d8 commit db7cb39

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

compat53/module.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ if lua_version < "5.3" then
147147
end
148148

149149

150+
-- assert should allow non-string error objects
151+
do
152+
function M.assert(cond, ...)
153+
if cond then
154+
return cond, ...
155+
elseif select('#', ...) > 0 then
156+
error((...), 0)
157+
else
158+
error("assertion failed!", 0)
159+
end
160+
end
161+
end
162+
163+
150164
-- ipairs should respect __index metamethod
151165
do
152166
local function ipairs_iterator(st, var)

tests/test.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,25 @@ end
4545
package.path = "../?.lua;../?/init.lua;"..package.path
4646
package.cpath = "./?-"..V..".so;./?-"..V..".dll;./?.so;./?.dll"
4747
if mode == "module" then
48-
print( "testing Lua API using `compat53.module` ..." )
48+
print("testing Lua API using `compat53.module` ...")
4949
_ENV = require("compat53.module")
5050
if setfenv then setfenv(1, _ENV) end
5151
else
52-
print( "testing Lua API using `compat53` ..." )
52+
print("testing Lua API using `compat53` ...")
5353
require("compat53")
5454
end
5555

56+
57+
___''
58+
do
59+
print("assert", F(pcall(assert, false)))
60+
print("assert", F(pcall(assert, false, nil)))
61+
print("assert", F(pcall(assert, false, "error msg")))
62+
print("assert", F(pcall(assert, nil, {})))
63+
print("assert", F(pcall(assert, 1, 2, 3)))
64+
end
65+
66+
5667
___''
5768
do
5869
local t = setmetatable({}, { __index = { 1, false, "three" } })

0 commit comments

Comments
 (0)