aboutsummaryrefslogtreecommitdiff
path: root/test/lua/visrc.lua
diff options
context:
space:
mode:
authorJeremy Bobbin <jer@jer.cx>2024-12-27 11:53:30 +0100
committerRandy Palamar <randy@rnpnr.xyz>2025-01-02 05:46:40 -0700
commit438b31497d91becdec95d06ff62c2d86184fa669 (patch)
treee8281da94c26732a0ecf2f34b637b2cbad504fd7 /test/lua/visrc.lua
parentf840bcaf95dea328b9edc76c68e27d42cf79330b (diff)
downloadvis-438b31497d91becdec95d06ff62c2d86184fa669.tar.gz
vis-438b31497d91becdec95d06ff62c2d86184fa669.tar.xz
no longer depend on lua-busted
Co-authored-by: Matěj Cepl <mcepl@cepl.eu>
Diffstat (limited to 'test/lua/visrc.lua')
-rw-r--r--test/lua/visrc.lua133
1 files changed, 116 insertions, 17 deletions
diff --git a/test/lua/visrc.lua b/test/lua/visrc.lua
index 16aa8b9..a19916f 100644
--- a/test/lua/visrc.lua
+++ b/test/lua/visrc.lua
@@ -1,28 +1,127 @@
package.path = '../../lua/?.lua;'..package.path
+
dofile("../../lua/vis.lua")
--- redirect output to stdout, stderr is used by the vis UI
-io.stderr = io.stdout
+local function str(e)
+ if type(e) == "string" then
+ if e == "" then
+ return "an empty string"
+ else
+ return '"'..e..'"'
+ end
+ else
+ return tostring(e)
+ end
+end
+
+
+local function same(a, b)
+ if type(a) ~= type(b) then
+ return string.format("expected %s - got %s", type(a), type(b))
+ end
+
+ if type(a) ~= 'table' then
+ if a == b then
+ return nil
+ else
+ return string.format("expected %s - got %s", str(a), str(b))
+ end
+ end
+
+ if #a ~= #b then
+ return string.format("expected table of size %d got %d", #a, #b)
+ end
+
+ for k, v in pairs(a) do
+ if b[k] == nil then
+ return string.format("expected %s got nil", str(k))
+ end
+ r = same(v, b[k])
+ if r ~= nil then
+ return r
+ end
+ end
+ return nil
+end
--- make sure we gracefully terminate, cleanup terminal state etc.
-os.exit = function(status)
- vis:exit(status)
+
+local msg = ""
+function describe(s, fn)
+ group = {
+ before_each = function()
+ end,
+ tests = {},
+ after_each = function()
+ end,
+
+ }
+ function before_each(fn) group.before_each = fn end
+ function after_each(fn) group.after_each = fn end
+ function it(s, fn)
+ table.insert(group.tests, {
+ s = s,
+ fn = fn,
+ assertions = {},
+ })
+ end
+ fn()
+ for j, t in pairs(group.tests) do
+ group.before_each()
+ assert = {
+ has_error = function(fn)
+ local status, err = pcall(fn)
+ if err == nil then
+ msg = msg..string.format("%s %s: expected error\n", s, t.s)
+ end
+ end,
+ truthy = function(stat)
+ if not (stat) then
+ msg = msg..string.format("%s %s: expected to be truthy: %s\n", s, t.s, str(stat))
+ end
+ end,
+ falsy = function(stat)
+ if (stat) then
+ msg = msg..string.format("%s %s: expected to be falsy: %s\n", s, t.s, str(stat))
+ end
+ end,
+ are = {
+ equal = function(a, b)
+ if a ~= b then
+ msg = msg..string.format("%s %s: expected %s - got %s\n", s, t.s, str(a), str(b))
+ end
+ end,
+ same = function(a, b)
+ r = same(a, b) -- same returns a string which is a reason why a & b are not equal
+ if r ~= nil then
+ msg = msg..string.format("%s %s: %s\n", s, t.s, r)
+ end
+ end,
+ },
+ }
+ t.fn()
+ group.after_each()
+ end
end
+
vis.events.subscribe(vis.events.WIN_OPEN, function(win)
-- test.in file passed to vis
local in_file = win.file.name
- if in_file then
- -- use the corresponding test.lua file
- lua_file = string.gsub(in_file, '%.in$', '.lua')
- local ok, msg = pcall(dofile, lua_file)
- if not ok then
- if type(msg) == 'string' then
- print(msg)
- end
- vis:exit(1)
- return
- end
+ if not in_file then
+ return
+ end
+
+ -- use the corresponding test.lua file
+ lua_file = string.gsub(in_file, '%.in$', '.lua')
+
+ local ok, err = pcall(dofile, lua_file)
+ if not ok then
+ print(tostring(err))
+ vis:exit(2) -- ERROR
+ elseif msg ~= "" then
+ io.write(msg) -- no newline
+ vis:exit(1) -- FAIL
+ else
+ vis:exit(0) -- SUCCESS
end
- vis:exit(0)
end)