diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lbaselib.js | 1 | ||||
-rw-r--r-- | tests/ldblib.js | 211 | ||||
-rw-r--r-- | tests/lexparse.js | 4 | ||||
-rw-r--r-- | tests/loslib.js | 38 | ||||
-rw-r--r-- | tests/ltablib.js | 2 | ||||
m--------- | tests/lua-tests | 0 | ||||
-rw-r--r-- | tests/lua.js | 29 | ||||
-rw-r--r-- | tests/lvm.js | 4 |
8 files changed, 283 insertions, 6 deletions
diff --git a/tests/lbaselib.js b/tests/lbaselib.js index dd91449..5f39991 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -15,7 +15,6 @@ const lua = require('../src/lua.js'); const linit = require('../src/linit.js'); const CT = lua.constant_types; - test('print', function (t) { let luaCode = ` print("hello", "world", 123) diff --git a/tests/ldblib.js b/tests/ldblib.js index a770c76..6587228 100644 --- a/tests/ldblib.js +++ b/tests/ldblib.js @@ -7,6 +7,106 @@ const lauxlib = require("../src/lauxlib.js"); const lua = require('../src/lua.js'); const linit = require('../src/linit.js'); +test('debug.sethook', function (t) { + let luaCode = ` + local result = "" + + debug.sethook(function (event) + result = result .. event .. " " + end, "crl", 1) + + local l = function() end + + l() + l() + l() + + return result + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tojsstring(L, -1), + "return count line count line count line count return count line count line count return count line count line count return count line return ", + "Correct element(s) on the stack" + ); + +}); + + +test('debug.gethook', function (t) { + let luaCode = ` + local result = "" + + debug.sethook(function (event) + result = result .. event .. " " + end, "crl", 1) + + local l = function() end + + l() + l() + l() + + return debug.gethook() + `, L; + + t.plan(5); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.deepEqual( + lapi.lua_typename(L, lapi.lua_type(L, -3)), + lua.to_luastring("function"), + "Correct element(s) on the stack" + ); + + t.deepEqual( + lapi.lua_tojsstring(L, -2), + "crl", + "Correct element(s) on the stack" + ); + + t.deepEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); + +}); + + test('debug.getlocal', function (t) { let luaCode = ` local alocal = "alocal" @@ -54,6 +154,72 @@ test('debug.getlocal', function (t) { }); +test('debug.setlocal', function (t) { + let luaCode = ` + local alocal = "alocal" + local another = "another" + + local l = function() + local infunction = "infunction" + local anotherin = "anotherin" + + debug.setlocal(2, 1, 1) + debug.setlocal(2, 2, 2) + debug.setlocal(1, 1, 3) + debug.setlocal(1, 2, 4) + + return infunction, anotherin + end + + local a, b = l() + + return alocal, another, a, b + `, L; + + t.plan(6); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tointeger(L, -4), + 1, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -3), + 2, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -2), + 3, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 4, + "Correct element(s) on the stack" + ); + +}); + test('debug.upvalueid', function (t) { let luaCode = ` local upvalue = "upvalue" @@ -91,6 +257,51 @@ test('debug.upvalueid', function (t) { }); +test('debug.upvaluejoin', function (t) { + let luaCode = ` + local upvalue1 = "upvalue1" + local upvalue2 = "upvalue2" + + local l1 = function() + return upvalue1 + end + + local l2 = function() + return upvalue2 + end + + debug.upvaluejoin(l1, 1, l2, 1) + + return l1() + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tojsstring(L, -1), + "upvalue2", + "Correct element(s) on the stack" + ); + +}); + + test('debug.traceback (with a global)', function (t) { let luaCode = ` local trace diff --git a/tests/lexparse.js b/tests/lexparse.js index 92901b1..96c8fca 100644 --- a/tests/lexparse.js +++ b/tests/lexparse.js @@ -742,7 +742,7 @@ test('SETTABLE, GETTABLE', function (t) { }, "Lua program ran without error"); t.strictEqual( - lapi.lua_topointer(L, -1).get(0).jsstring(), + lapi.lua_topointer(L, -1).get(1).jsstring(), "hello", "Program output is correct" ); @@ -835,7 +835,7 @@ test('SETTABUP, GETTABUP', function (t) { }, "Lua program ran without error"); t.strictEqual( - lapi.lua_topointer(L, -1).get(0).jsstring(), + lapi.lua_topointer(L, -1).get(1).jsstring(), "hello", "Program output is correct" ); diff --git a/tests/loslib.js b/tests/loslib.js new file mode 100644 index 0000000..1bb217f --- /dev/null +++ b/tests/loslib.js @@ -0,0 +1,38 @@ +"use strict"; + +const test = require('tape'); + +const lapi = require("../src/lapi.js"); +const lauxlib = require("../src/lauxlib.js"); +const lua = require('../src/lua.js'); +const linit = require('../src/linit.js'); + +test('os.time', function (t) { + let luaCode = ` + return os.time() + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.ok( + lapi.lua_isinteger(L, -1), + "Correct element(s) on the stack" + ); + +}); diff --git a/tests/ltablib.js b/tests/ltablib.js index 81ffcbd..60f9abc 100644 --- a/tests/ltablib.js +++ b/tests/ltablib.js @@ -21,7 +21,7 @@ const inttable2array = function(t) { t.forEach(function (v, k) { if (typeof k === 'number') - a[k] = v; + a[k - 1] = v; }); return a.map(e => e.value); diff --git a/tests/lua-tests b/tests/lua-tests -Subproject 01eded4b34159a77f61b56c5567c74f169d3b1e +Subproject 9e0d0bfb5de8cb0534a13c19f23377db62b53d5 diff --git a/tests/lua.js b/tests/lua.js index 43e82bc..30a9c98 100644 --- a/tests/lua.js +++ b/tests/lua.js @@ -7,6 +7,35 @@ const lauxlib = require("../src/lauxlib.js"); const lua = require('../src/lua.js'); const linit = require('../src/linit.js'); + +test('constructs.lua', function (t) { + let luaCode = ` + _soft = true + require = function(lib) return _G[lib] end -- NYI + return dofile("tests/lua-tests/constructs.lua") + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + test('strings.lua', function (t) { let luaCode = ` return dofile("tests/lua-tests/strings.lua") diff --git a/tests/lvm.js b/tests/lvm.js index bda4c4f..0f91d81 100644 --- a/tests/lvm.js +++ b/tests/lvm.js @@ -496,7 +496,7 @@ test('SETTABLE, GETTABLE', function (t) { console.log(L.stack[L.top - 1]); t.deepEqual( - L.stack[L.top - 1].value.get(0).jsstring(), + L.stack[L.top - 1].value.get(1).jsstring(), "hello", "Program output is correct" ); @@ -559,7 +559,7 @@ test('SETTABUP, GETTABUP', function (t) { }, "Program executed without errors"); t.deepEqual( - L.stack[L.top - 1].value.get(0).jsstring(), + L.stack[L.top - 1].value.get(1).jsstring(), "hello", // "hello" "Program output is correct" ); |