From ce9fd9bdca0caa6a33ce37a01f41593ca42ba95b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 2 Apr 2018 22:44:25 +1000 Subject: Start moving test suite from tape => jest --- test/lbaselib.test.js | 436 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 436 insertions(+) create mode 100644 test/lbaselib.test.js (limited to 'test/lbaselib.test.js') diff --git a/test/lbaselib.test.js b/test/lbaselib.test.js new file mode 100644 index 0000000..259de36 --- /dev/null +++ b/test/lbaselib.test.js @@ -0,0 +1,436 @@ +"use strict"; + +const lua = require('../src/lua.js'); +const lauxlib = require('../src/lauxlib.js'); +const lualib = require('../src/lualib.js'); +const {to_luastring} = require("../src/fengaricore.js"); + + +test('print', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + print("hello", "world", 123) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } +}); + + +test('setmetatable, getmetatable', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local mt = { + __index = function () + print("hello") + return "hello" + end + } + + local t = {} + + setmetatable(t, mt); + + return t[1], getmetatable(t) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tojsstring(L, -2)) + .toBe("hello"); + + expect(lua.lua_istable(L, -1)).toBe(true); +}); + + +test('rawequal', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local mt = { + __eq = function () + return true + end + } + + local t1 = {} + local t2 = {} + + setmetatable(t1, mt); + + return rawequal(t1, t2), t1 == t2 + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_toboolean(L, -2)).toBe(false); + + expect(lua.lua_toboolean(L, -1)).toBe(true); +}); + + +test('rawset, rawget', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local mt = { + __newindex = function (table, key, value) + rawset(table, key, "hello") + end + } + + local t = {} + + setmetatable(t, mt); + + t["yo"] = "bye" + rawset(t, "yoyo", "bye") + + return rawget(t, "yo"), t["yo"], rawget(t, "yoyo"), t["yoyo"] + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tojsstring(L, -4)) + .toBe("hello"); + + expect(lua.lua_tojsstring(L, -3)) + .toBe("hello"); + + expect(lua.lua_tojsstring(L, -2)) + .toBe("bye"); + + expect(lua.lua_tojsstring(L, -1)) + .toBe("bye"); +}); + + +test('type', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + return type(1), type(true), type("hello"), type({}), type(nil) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tojsstring(L, -5)) + .toBe("number"); + + expect(lua.lua_tojsstring(L, -4)) + .toBe("boolean"); + + expect(lua.lua_tojsstring(L, -3)) + .toBe("string"); + + expect(lua.lua_tojsstring(L, -2)) + .toBe("table"); + + expect(lua.lua_tojsstring(L, -1)) + .toBe("nil"); +}); + + +test('error', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + error("you fucked up") + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + expect(() => { + lua.lua_call(L, 0, -1); + }).toThrow(/you fucked up/); + } +}); + + +test('error, protected', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + error("you fucked up") + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_pcall(L, 0, -1, 0); + } + + expect(lua.lua_tojsstring(L, -1)).toMatch(/you fucked up/); +}); + + +test('pcall', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + return pcall(willFail) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tojsstring(L, -1)).toMatch(/you fucked up/); +}); + + +test('xpcall', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local willFail = function () + error("you fucked up") + end + + local msgh = function (err) + return "Something's wrong: " .. err + end + + return xpcall(willFail, msgh) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tojsstring(L, -1)).toMatch(/Something's wrong: .*you fucked up/); +}); + + +test('ipairs', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local t = {1, 2, 3, 4, 5, ['yo'] = 'lo'} + + local sum = 0 + for i, v in ipairs(t) do + sum = sum + v + end + + return sum + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tointeger(L, -1)).toBe(15); +}); + + +test('select', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + return {select('#', 1, 2, 3)}, {select(2, 1, 2, 3)}, {select(-2, 1, 2, 3)} + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect([...lua.lua_topointer(L, -3).strong.entries()].map(e => e[1].value.value)) + .toEqual([3]); + + expect([...lua.lua_topointer(L, -2).strong.entries()].map(e => e[1].value.value).sort()) + .toEqual([2, 3]); + + expect([...lua.lua_topointer(L, -1).strong.entries()].map(e => e[1].value.value).sort()) + .toEqual([2, 3]); +}); + + +test('tonumber', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + return tonumber('foo'), + tonumber('123'), + tonumber('12.3'), + tonumber('az', 36), + tonumber('10', 2) + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_isnil(L, -5)).toBe(true); + expect(lua.lua_tonumber(L, -4)).toBe(123); + expect(lua.lua_tonumber(L, -3)).toBe(12.3); + expect(lua.lua_tonumber(L, -2)).toBe(395); + expect(lua.lua_tonumber(L, -1)).toBe(2); +}); + + +test('assert', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(1 < 0, "this doesn't makes sense") + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_pcall(L, 0, -1, 0); + } + + expect(lua.lua_tojsstring(L, -1)).toMatch(/this doesn't makes sense/); +}); + + +test('rawlen', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + return rawlen({1, 2, 3}), rawlen('hello') + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tonumber(L, -2)).toBe(3); + expect(lua.lua_tonumber(L, -1)).toBe(5); +}); + + +test('next', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local total = 0 + local t = { + 1, + two = 2, + 3, + four = 4 + } + + for k,v in next, t, nil do + total = total + v + end + + return total + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tonumber(L, -1)).toBe(10); +}); + + +test('pairs', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local total = 0 + local t = { + 1, + two = 2, + 3, + four = 4 + } + + for k,v in pairs(t) do + total = total + v + end + + return total + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tonumber(L, -1)).toBe(10); +}); + + +test('pairs with __pairs', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local total = 0 + + local mt = { + __pairs = function(t) + return next, {5, 6, 7, 8}, nil + end + } + + local t = { + 1, + two = 2, + 3, + four = 4 + } + + setmetatable(t, mt) + + for k,v in pairs(t) do + total = total + v + end + + return total + `; + { + lualib.luaL_openlibs(L); + expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK); + lua.lua_call(L, 0, -1); + } + + expect(lua.lua_tonumber(L, -1)).toBe(26); +}); -- cgit v1.2.3-54-g00ecf From e9986b9189e2358f9c1a005412a792b7795643d2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 14 Apr 2018 15:03:54 +1000 Subject: test/test-suite: Remove print calls --- test/lbaselib.test.js | 1 - test/test-suite/api.test.js | 2 +- test/test-suite/attrib.test.js | 2 +- test/test-suite/code.test.js | 2 +- test/test-suite/constructs.test.js | 2 +- test/test-suite/db.test.js | 3 +- test/test-suite/math.test.js | 124 +++++++++++++++++++------------------ test/test-suite/sort.test.js | 6 +- test/test-suite/strings.test.js | 2 +- test/test-suite/tpack.test.js | 16 ++--- test/test-suite/vararg.test.js | 2 +- 11 files changed, 81 insertions(+), 81 deletions(-) (limited to 'test/lbaselib.test.js') diff --git a/test/lbaselib.test.js b/test/lbaselib.test.js index 259de36..dc30a98 100644 --- a/test/lbaselib.test.js +++ b/test/lbaselib.test.js @@ -28,7 +28,6 @@ test('setmetatable, getmetatable', () => { let luaCode = ` local mt = { __index = function () - print("hello") return "hello" end } diff --git a/test/test-suite/api.test.js b/test/test-suite/api.test.js index 152ddcf..5a5e10e 100644 --- a/test/test-suite/api.test.js +++ b/test/test-suite/api.test.js @@ -686,7 +686,7 @@ test("[test-suite] api: test errors in non protected threads", () => { if not _soft then checkerrnopro("pushnum 3; call 0 0", "attempt to call") - print"testing stack overflow in unprotected thread" + -- print"testing stack overflow in unprotected thread" function f () f() end checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") end diff --git a/test/test-suite/attrib.test.js b/test/test-suite/attrib.test.js index 596c82c..bb07e62 100644 --- a/test/test-suite/attrib.test.js +++ b/test/test-suite/attrib.test.js @@ -23,7 +23,7 @@ test("[test-suite] attrib: testing require", () => { assert(type(package.preload) == "table") assert(type(package.config) == "string") - print("package config: "..string.gsub(package.config, "\\n", "|")) + -- print("package config: "..string.gsub(package.config, "\\n", "|")) do -- create a path with 'max' templates, diff --git a/test/test-suite/code.test.js b/test/test-suite/code.test.js index d37030c..377dd63 100644 --- a/test/test-suite/code.test.js +++ b/test/test-suite/code.test.js @@ -45,7 +45,7 @@ const prefix = ` local arg = {...} local c = T.listcode(f) for i=1, #arg do - print(arg[i], c[i]) + -- print(arg[i], c[i]) assert(string.find(c[i], '- '..arg[i]..' *%d')) end assert(c[#arg+2] == nil) diff --git a/test/test-suite/constructs.test.js b/test/test-suite/constructs.test.js index de0648d..e79778a 100644 --- a/test/test-suite/constructs.test.js +++ b/test/test-suite/constructs.test.js @@ -332,7 +332,7 @@ test.skip('[test-suite] constructs: huge loops, upvalue', () => { IX = false assert(p() == v[2] and IX == not not v[2]) i = i + 1 - if i % 60000 == 0 then print('+') end + -- if i % 60000 == 0 then print('+') end end end `; diff --git a/test/test-suite/db.test.js b/test/test-suite/db.test.js index 38330db..89ec66d 100644 --- a/test/test-suite/db.test.js +++ b/test/test-suite/db.test.js @@ -598,8 +598,7 @@ test("[test-suite] db: tests for tail calls", () => { local tail = debug.getinfo(2) assert(tail.func == g1 and tail.istailcall == true) assert(debug.getinfo(3, "S").what == "main") - print"+" - end + end end function g(x) return f(x) end diff --git a/test/test-suite/math.test.js b/test/test-suite/math.test.js index c649225..9cf4e21 100644 --- a/test/test-suite/math.test.js +++ b/test/test-suite/math.test.js @@ -80,8 +80,8 @@ test("[test-suite] math: number of bits in the mantissa of a floating-point numb local x = 2.0^floatbits assert(x > x - 1.0 and x == x + 1.0) - print(string.format("%d-bit integers, %d-bit (mantissa) floats", - intbits, floatbits)) + -- print(string.format("%d-bit integers, %d-bit (mantissa) floats", + -- intbits, floatbits)) end assert(math.type(0) == "integer" and math.type(0.0) == "float" @@ -304,66 +304,69 @@ test("[test-suite] math: order between floats and integers", () => { assert(minint <= -2^(intbits - 1)) assert(-2^(intbits - 1) <= minint) end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); - if floatbits < intbits then - print("testing order (floats cannot represent all integers)") - local fmax = 2^floatbits - local ifmax = fmax | 0 - assert(fmax < ifmax + 1) - assert(fmax - 1 < ifmax) - assert(-(fmax - 1) > -ifmax) - assert(not (fmax <= ifmax - 1)) - assert(-fmax > -(ifmax + 1)) - assert(not (-fmax >= -(ifmax - 1))) - - assert(fmax/2 - 0.5 < ifmax//2) - assert(-(fmax/2 - 0.5) > -ifmax//2) - - assert(maxint < 2^intbits) - assert(minint > -2^intbits) - assert(maxint <= 2^intbits) - assert(minint >= -2^intbits) - else - print("testing order (floats can represent all integers)") - assert(maxint < maxint + 1.0) - assert(maxint < maxint + 0.5) - assert(maxint - 1.0 < maxint) - assert(maxint - 0.5 < maxint) - assert(not (maxint + 0.0 < maxint)) - assert(maxint + 0.0 <= maxint) - assert(not (maxint < maxint + 0.0)) - assert(maxint + 0.0 <= maxint) - assert(maxint <= maxint + 0.0) - assert(not (maxint + 1.0 <= maxint)) - assert(not (maxint + 0.5 <= maxint)) - assert(not (maxint <= maxint - 1.0)) - assert(not (maxint <= maxint - 0.5)) - - assert(minint < minint + 1.0) - assert(minint < minint + 0.5) - assert(minint <= minint + 0.5) - assert(minint - 1.0 < minint) - assert(minint - 1.0 <= minint) - assert(not (minint + 0.0 < minint)) - assert(not (minint + 0.5 < minint)) - assert(not (minint < minint + 0.0)) - assert(minint + 0.0 <= minint) - assert(minint <= minint + 0.0) - assert(not (minint + 1.0 <= minint)) - assert(not (minint + 0.5 <= minint)) - assert(not (minint <= minint - 1.0)) - end - do - local NaN = 0/0 - assert(not (NaN < 0)) - assert(not (NaN > minint)) - assert(not (NaN <= -9)) - assert(not (NaN <= maxint)) - assert(not (NaN < maxint)) - assert(not (minint <= NaN)) - assert(not (minint < NaN)) - end +test("[test-suite] math: testing order (floats can represent all integers)", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(floatbits >= intbits) + + assert(maxint < maxint + 1.0) + assert(maxint < maxint + 0.5) + assert(maxint - 1.0 < maxint) + assert(maxint - 0.5 < maxint) + assert(not (maxint + 0.0 < maxint)) + assert(maxint + 0.0 <= maxint) + assert(not (maxint < maxint + 0.0)) + assert(maxint + 0.0 <= maxint) + assert(maxint <= maxint + 0.0) + assert(not (maxint + 1.0 <= maxint)) + assert(not (maxint + 0.5 <= maxint)) + assert(not (maxint <= maxint - 1.0)) + assert(not (maxint <= maxint - 0.5)) + + assert(minint < minint + 1.0) + assert(minint < minint + 0.5) + assert(minint <= minint + 0.5) + assert(minint - 1.0 < minint) + assert(minint - 1.0 <= minint) + assert(not (minint + 0.0 < minint)) + assert(not (minint + 0.5 < minint)) + assert(not (minint < minint + 0.0)) + assert(minint + 0.0 <= minint) + assert(minint <= minint + 0.0) + assert(not (minint + 1.0 <= minint)) + assert(not (minint + 0.5 <= minint)) + assert(not (minint <= minint - 1.0)) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] math: NaN order", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local NaN = 0/0 + assert(not (NaN < 0)) + assert(not (NaN > minint)) + assert(not (NaN <= -9)) + assert(not (NaN <= maxint)) + assert(not (NaN < maxint)) + assert(not (minint <= NaN)) + assert(not (minint < NaN)) `; lualib.luaL_openlibs(L); if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) @@ -988,7 +991,6 @@ test("[test-suite] math: testing -0 and NaN", () => { let luaCode = ` do - print("testing -0 and NaN") local mz, z = -0.0, 0.0 assert(mz == z) assert(1/mz < 0 and 0 < 1/z) diff --git a/test/test-suite/sort.test.js b/test/test-suite/sort.test.js index 154db03..cc33c0c 100644 --- a/test/test-suite/sort.test.js +++ b/test/test-suite/sort.test.js @@ -22,7 +22,7 @@ const prefix = ` table.sort(a, func) x = (os.clock() - x) * 1000 pre = pre or "" - print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) + -- print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) check(a, func) end @@ -397,8 +397,8 @@ test("[test-suite] sort: Invert-sorting", () => { x = os.clock(); i=0 table.sort(a, function(x,y) i=i+1; return y { end if not pcall(string.format, "%.3a", 0) then - (Message or print)("\\n >>> modifiers for format '%a' not available <<<\\n") + -- (Message or print)("\\n >>> modifiers for format '%a' not available <<<\\n") else assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) diff --git a/test/test-suite/tpack.test.js b/test/test-suite/tpack.test.js index 132956e..3b97d3a 100644 --- a/test/test-suite/tpack.test.js +++ b/test/test-suite/tpack.test.js @@ -38,14 +38,14 @@ test("[test-suite] tpack: maximum size for integers", () => { assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and sizefloat <= sizedouble) - print("platform:") - print(string.format( - "\\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\\n\\z - \\tlua Integer %d, lua Number %d", - sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, - sizeLI, sizenumber)) - print("\\t" .. (little and "little" or "big") .. " endian") - print("\\talignment: " .. align) + -- print("platform:") + -- print(string.format( + -- "\\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\\n\\z + -- \\tlua Integer %d, lua Number %d", + -- sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, + -- sizeLI, sizenumber)) + -- print("\\t" .. (little and "little" or "big") .. " endian") + -- print("\\talignment: " .. align) `; lualib.luaL_openlibs(L); if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) diff --git a/test/test-suite/vararg.test.js b/test/test-suite/vararg.test.js index 78439f1..0538541 100644 --- a/test/test-suite/vararg.test.js +++ b/test/test-suite/vararg.test.js @@ -45,7 +45,7 @@ test("[test-suite] vararg: testing vararg", () => { assert(a[1] == b and a[2] == c and a.n == 2) a = vararg(call(call, {c12, {1,2}})) assert(a.n == 2 and a[1] == 55 and a[2] == 2) - a = call(print, {'+'}) + a = call(function()end, {'+'}) assert(a == nil) local t = {1, 10} -- cgit v1.2.3-54-g00ecf