diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/loadlib.js | 68 | ||||
-rw-r--r-- | tests/module-hello.lua | 3 |
2 files changed, 71 insertions, 0 deletions
diff --git a/tests/loadlib.js b/tests/loadlib.js new file mode 100644 index 0000000..933991c --- /dev/null +++ b/tests/loadlib.js @@ -0,0 +1,68 @@ +"use strict"; + +const test = require('tape'); + +const lauxlib = require("../src/lauxlib.js"); +const lua = require('../src/lua.js'); + +test('require an existing module', function (t) { + let luaCode = ` + return require('os') + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.ok( + lua.lua_istable(L, -1), + "Correct element(s) on the stack" + ); + +}); + + +test('require a file', function (t) { + let luaCode = ` + return require('tests/module-hello')() + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lua.lua_tojsstring(L, -1), + "hello from module", + "Correct element(s) on the stack" + ); + +}); diff --git a/tests/module-hello.lua b/tests/module-hello.lua new file mode 100644 index 0000000..4de5890 --- /dev/null +++ b/tests/module-hello.lua @@ -0,0 +1,3 @@ +return function() + return 'hello from module' +end |