summaryrefslogtreecommitdiff
path: root/example/simple-example.lua
diff options
context:
space:
mode:
Diffstat (limited to 'example/simple-example.lua')
-rw-r--r--example/simple-example.lua102
1 files changed, 76 insertions, 26 deletions
diff --git a/example/simple-example.lua b/example/simple-example.lua
index 1d82944..6f35400 100644
--- a/example/simple-example.lua
+++ b/example/simple-example.lua
@@ -41,39 +41,55 @@ local mt = {
setmetatable(tab, mt)
-local function lprint(...)
+local tests, passed = 0, 0
+
+local function lprint(expect, result, ...)
+ tests = tests + 1
+ luxio.write(1, ("[%d]\r"):format(tests))
local foo = {n=select("#",...),...}
- if foo[1] then
- print "Function ran OK:"
- else
- print "Error encountered:"
+ if expect and not result then
+ print "Want OK, got FAIL"
+ elseif (not expect) and result then
+ print "Want FAIL, got OK"
end
- for i = 2, foo.n do
- if type(foo[i]) == "table" then
- print("{")
- for k, v in pairs(foo[i]) do
- print("",k,v)
+ if expect ~= result then
+ for i = 1, foo.n do
+ if type(foo[i]) == "table" then
+ print("{")
+ for k, v in pairs(foo[i]) do
+ print("",k,v)
+ end
+ print("}")
+ else
+ print(foo[i])
end
- print("}")
- else
- print(foo[i])
end
+ print()
+ else
+ passed = passed + 1
end
- print()
+end
+
+local function lprint_ok(...)
+ return lprint(true, ...)
+end
+
+local function lprint_fail(...)
+ return lprint(false, ...)
end
-- Finally, run the subcode
-lprint(supple.host.run(subcode, "@test-code", tab))
+lprint_ok(supple.host.run(subcode, "@test-code", tab))
assert(tab.tot == 24)
-- Now run a supple command which we expect to error out.
-lprint(supple.host.run("unknown()", "@test-code"))
+lprint_fail(supple.host.run("unknown()", "@test-code"))
-- And now, one where we pass an error from host to sandbox and back
-lprint(supple.host.run("local f = ... f()", "@test-code", function() unknown() end))
+lprint_fail(supple.host.run("local f = ... f()", "@test-code", function() unknown() end))
-- And now, one where we pass an error from sandbox to host to sandbox and back
-lprint(supple.host.run("local f = ... f(function() unknown() end)", "@test-code", function(ff) ff() end))
+lprint_fail(supple.host.run("local f = ... f(function() unknown() end)", "@test-code", function(ff) ff() end))
-- Next , a reasonable traceback via named functions on each end...
@@ -99,7 +115,7 @@ function loopback(f)
f(loopback)
end
-lprint(supple.host.run(errsrc, "@error-code", loopback))
+lprint_fail(supple.host.run(errsrc, "@error-code", loopback))
-- Now we try the sandboxing limits
@@ -112,9 +128,9 @@ local long_run = [[
]]
supple.host.set_limits { count = 100 }
-lprint(supple.host.run(long_run, "@long-code"))
+lprint_fail(supple.host.run(long_run, "@long-code"))
supple.host.set_limits { memory = 1000 }
-lprint(supple.host.run(long_run, "@big-code"))
+lprint_fail(supple.host.run(long_run, "@big-code"))
-- next we demonstrate that ipairs works on proxied tables
@@ -128,7 +144,7 @@ local summing = [[
return tot
]]
-lprint(supple.host.run(summing, "@summing", { 10, 14, 3 }))
+lprint_ok(supple.host.run(summing, "@summing", { 10, 14, 3 }))
-- next we demonstrate that next works on proxied tables
@@ -137,8 +153,8 @@ local isempty = [[
return next(t) == nil
]]
-lprint(supple.host.run(isempty, "@isempty", {}))
-lprint(supple.host.run(isempty, "@isempty", {"bar"}))
+lprint_ok(supple.host.run(isempty, "@isempty", {}))
+lprint_ok(supple.host.run(isempty, "@isempty", {"bar"}))
-- And now that pairs works on proxied tables
@@ -152,7 +168,7 @@ local keys = [[
return ret
]]
-lprint(supple.host.run(keys, "@keys", { foo="bar", baz="meta" }))
+lprint_ok(supple.host.run(keys, "@keys", { foo="bar", baz="meta" }))
-- Next demonstrate that using supple.host.loadstring works to allow
-- globals set in the parent to be seen/changed
@@ -168,4 +184,38 @@ local getname = [[
]]
-- Expect to see 'PRINT'
-lprint(supple.host.run(getname, "@getname", loader))
+lprint_ok(supple.host.run(getname, "@getname", loader))
+
+-- Attempt to do a test which shows the same issue as seen with gitano-web
+
+local updates = {
+ ["refs/heads/master"] = {
+ oldsha = "0123456789abcdef0123456789abcdef01234567",
+ newsha = "76543210fedcba9876543210fedcba9876543210",
+ }
+}
+
+local update_code = [[
+ local repo, updates = ...
+ local hadmaster = false
+ local foundrefs = {}
+ if updates["refs/heads/master"] then
+ hadmaster = true
+ end
+ for ref, data in pairs(updates) do
+ foundrefs[ref] = { data.oldsha, data.newsha }
+ end
+ return hadmaster, foundrefs
+]]
+
+for i = 1, 1000 do
+ lprint_ok(supple.host.run(update_code, "@update-hook", "REPO", updates))
+ if passed ~= tests then
+ os.exit(1)
+ end
+end
+
+print(("[%d/%d OK]"):format(passed, tests))
+
+-- Dump the final trace, just for edification
+--print(supple.track.stop())