From 278fd3edb6ead6cd65c2293f823887d19e4fbc8e Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 3 May 2017 13:51:20 +0200 Subject: require --- tests/loadlib.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/module-hello.lua | 3 +++ 2 files changed, 71 insertions(+) create mode 100644 tests/loadlib.js create mode 100644 tests/module-hello.lua (limited to 'tests') 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 -- cgit v1.2.3-54-g00ecf