From 4bc6a3b39c45865723b5ce07f08e16186b702fbb Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 27 Apr 2017 11:23:28 +0200 Subject: [test-suite] locals.js --- tests/test-suite/locals.js | 248 ++++++++++++++++++++++++++++++++++++++++++++ tests/test-suite/strings.js | 38 ++++--- 2 files changed, 266 insertions(+), 20 deletions(-) create mode 100644 tests/test-suite/locals.js diff --git a/tests/test-suite/locals.js b/tests/test-suite/locals.js new file mode 100644 index 0000000..93dd27c --- /dev/null +++ b/tests/test-suite/locals.js @@ -0,0 +1,248 @@ +"use strict"; + +const test = require('tape'); + +const lauxlib = require("../../src/lauxlib.js"); +const lua = require('../../src/lua.js'); + + +test('[test-suite] locals: bug in 5.1', function (t) { + let luaCode = ` + local function f(x) x = nil; return x end + assert(f(10) == nil) + + local function f() local x; return x end + assert(f(10) == nil) + + local function f(x) x = nil; local y; return x, y end + assert(f(10) == nil and select(2, f(20)) == nil) + `, L; + + t.plan(2); + + 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"); + +}); + + +test('[test-suite] locals: local scope', function (t) { + let luaCode = ` + do + local i = 10 + do local i = 100; assert(i==100) end + do local i = 1000; assert(i==1000) end + assert(i == 10) + if i ~= 10 then + local i = 20 + else + local i = 30 + assert(i == 30) + end + end + + f = nil + + local f + x = 1 + + a = nil + load('local a = {}')() + assert(a == nil) + + function f (a) + local _1, _2, _3, _4, _5 + local _6, _7, _8, _9, _10 + local x = 3 + local b = a + local c,d = a,b + if (d == b) then + local x = 'q' + x = b + assert(x == 2) + else + assert(nil) + end + assert(x == 3) + local f = 10 + end + + local b=10 + local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 + + + assert(x == 1) + + f(2) + assert(type(f) == 'function') + `, L; + + t.plan(2); + + 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"); + +}); + + +const getenv = ` + local function getenv (f) + local a,b = debug.getupvalue(f, 1) + assert(a == '_ENV') + return b + end +`; + + +test('[test-suite] locals: test for global table of loaded chunks', function (t) { + let luaCode = ` + assert(getenv(load"a=3") == _G) + local c = {}; local f = load("a = 3", nil, nil, c) + assert(getenv(f) == c) + assert(c.a == nil) + f() + assert(c.a == 3) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(getenv + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test('[test-suite] locals: old test for limits for special instructions (now just a generic test)', function (t) { + let luaCode = ` + do + local i = 2 + local p = 4 -- p == 2^i + repeat + for j=-3,3 do + assert(load(string.format([[local a=%s; + a=a+%s; + assert(a ==2^%s)]], j, p-j, i), '')) () + assert(load(string.format([[local a=%s; + a=a-%s; + assert(a==-2^%s)]], -j, p-j, i), '')) () + assert(load(string.format([[local a,b=0,%s; + a=b-%s; + assert(a==-2^%s)]], -j, p-j, i), '')) () + end + p = 2 * p; i = i + 1 + until p <= 0 + end + `, L; + + t.plan(2); + + 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"); + +}); + + +test('[test-suite] locals: testing lexical environments', function (t) { + let luaCode = ` + assert(_ENV == _G) + + do + local dummy + local _ENV = (function (...) return ... end)(_G, dummy) -- { + + do local _ENV = {assert=assert}; assert(true) end + mt = {_G = _G} + local foo,x + A = false -- "declare" A + do local _ENV = mt + function foo (x) + A = x + do local _ENV = _G; A = 1000 end + return function (x) return A .. x end + end + end + assert(getenv(foo) == mt) + x = foo('hi'); assert(mt.A == 'hi' and A == 1000) + assert(x('*') == mt.A .. '*') + + do local _ENV = {assert=assert, A=10}; + do local _ENV = {assert=assert, A=20}; + assert(A==20);x=A + end + assert(A==10 and x==20) + end + assert(x==20) + end + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(getenv + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); diff --git a/tests/test-suite/strings.js b/tests/test-suite/strings.js index 471f6d0..48f742c 100644 --- a/tests/test-suite/strings.js +++ b/tests/test-suite/strings.js @@ -2,11 +2,9 @@ const test = require('tape'); -const lapi = require("../../src/lapi.js"); const lauxlib = require("../../src/lauxlib.js"); const lua = require('../../src/lua.js'); - const checkerror = ` local maxi, mini = math.maxinteger, math.mininteger @@ -51,7 +49,7 @@ test('[test-suite] strings: string comparisons', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -93,7 +91,7 @@ test('[test-suite] strings: string.sub', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -131,7 +129,7 @@ test('[test-suite] strings: string.find', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -163,7 +161,7 @@ test('[test-suite] strings: string.len and #', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -220,7 +218,7 @@ test('[test-suite] strings: string.byte/string.char', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -257,7 +255,7 @@ test('[test-suite] strings: repetitions with separator', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -309,7 +307,7 @@ test('[test-suite] strings: tostring', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -356,7 +354,7 @@ test('[test-suite] strings: string.format', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -397,7 +395,7 @@ test('[test-suite] strings: %q', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -424,7 +422,7 @@ test('[test-suite] strings: embedded zeros error', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -467,7 +465,7 @@ test('[test-suite] strings: format x tostring', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -504,7 +502,7 @@ test('[test-suite] strings: longest number that can be formatted', function (t) t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -553,7 +551,7 @@ test('[test-suite] strings: large numbers for format', function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -610,7 +608,7 @@ test("[test-suite] strings: 'format %a %A'", function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -650,7 +648,7 @@ test("[test-suite] strings: errors in format", function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -701,7 +699,7 @@ test("[test-suite] strings: table.concat", function (t) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -758,7 +756,7 @@ if (false) { t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); @@ -790,7 +788,7 @@ test("[test-suite] strings: bug in Lua 5.3.2: 'gmatch' iterator does not work ac t.doesNotThrow(function () { - lapi.lua_call(L, 0, -1); + lua.lua_call(L, 0, -1); }, "Lua program ran without error"); -- cgit v1.2.3-70-g09d2