aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-05-08 14:11:27 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-05-09 13:50:03 +0200
commit0c5fb6fb28fec2284bdcc3f4d25467a47dcfd58d (patch)
treed7768c9ed4c35998ce7133081b58d83a61c19b13
parentdbb718d265c689195d4bb55be0188e72478b9fb9 (diff)
downloadfengari-0c5fb6fb28fec2284bdcc3f4d25467a47dcfd58d.tar.gz
fengari-0c5fb6fb28fec2284bdcc3f4d25467a47dcfd58d.tar.bz2
fengari-0c5fb6fb28fec2284bdcc3f4d25467a47dcfd58d.zip
[test-suite] events.js
-rw-r--r--tests/test-suite/events.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/test-suite/events.js b/tests/test-suite/events.js
new file mode 100644
index 0000000..64e8056
--- /dev/null
+++ b/tests/test-suite/events.js
@@ -0,0 +1,83 @@
+"use strict";
+
+const test = require('tape');
+
+const lauxlib = require("../../src/lauxlib.js");
+const lua = require('../../src/lua.js');
+
+
+test("[test-suite] events: testing metatable", function (t) {
+ let luaCode = `
+ X = 20; B = 30
+
+ _ENV = setmetatable({}, {__index=_G})
+
+ collectgarbage()
+
+ X = X+10
+ assert(X == 30 and _G.X == 20)
+ B = false
+ assert(B == false)
+ B = nil
+ assert(B == 30)
+
+ assert(getmetatable{} == nil)
+ assert(getmetatable(4) == nil)
+ assert(getmetatable(nil) == nil)
+ a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu",
+ __tostring=function(x) return x.name end})
+ assert(getmetatable(a) == "xuxu")
+ assert(tostring(a) == "NAME")
+ -- cannot change a protected metatable
+ assert(pcall(setmetatable, a, {}) == false)
+ a.name = "gororoba"
+ assert(tostring(a) == "gororoba")
+
+ local a, t = {10,20,30; x="10", y="20"}, {}
+ assert(setmetatable(a,t) == a)
+ assert(getmetatable(a) == t)
+ assert(setmetatable(a,nil) == a)
+ assert(getmetatable(a) == nil)
+ assert(setmetatable(a,t) == a)
+
+
+ function f (t, i, e)
+ assert(not e)
+ local p = rawget(t, "parent")
+ return (p and p[i]+3), "dummy return"
+ end
+
+ t.__index = f
+
+ a.parent = {z=25, x=12, [4] = 24}
+ assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10")
+
+ collectgarbage()
+
+ a = setmetatable({}, t)
+ function f(t, i, v) rawset(t, i, v-3) end
+ setmetatable(t, t) -- causes a bug in 5.1 !
+ t.__newindex = f
+ a[1] = 30; a.x = "101"; a[5] = 200
+ -- assert(a[1] == 27 and a.x == 98 and a[5] == 197)
+ `, 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");
+
+}); \ No newline at end of file