diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lbaselib.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/lbaselib.js b/tests/lbaselib.js index b55afde..cca3ebe 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -290,4 +290,74 @@ test('error, protected', function (t) { lapi.lua_tostring(L, -1).endsWith("you fucked up"), "Error is on the stack" ) +}); + + +test('pcall', function (t) { + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + return pcall(willFail) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-pcall"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.ok( + lapi.lua_tostring(L, -1).endsWith("you fucked up"), + "Error is on the stack" + ) +}); + + +test('xpcall', function (t) { + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + local msgh = function (err) + return "Something's wrong: " .. err + end + + return xpcall(willFail, msgh) + `, L; + + t.plan(1); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-pcall"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + console.log(lapi.lua_tostring(L, -1)); + + // t.ok( + // lapi.lua_tostring(L, -1).endsWith("you fucked up"), + // "Error is on the stack" + // ) });
\ No newline at end of file |