aboutsummaryrefslogtreecommitdiff
path: root/src/check
diff options
context:
space:
mode:
Diffstat (limited to 'src/check')
-rwxr-xr-xsrc/check94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/check b/src/check
new file mode 100755
index 0000000..99983e2
--- /dev/null
+++ b/src/check
@@ -0,0 +1,94 @@
+#!/usr/bin/env lua
+
+function setup_package()
+ local str = debug.getinfo(2, "S").source:sub(2)
+ local path = str:match("(.*/)") or "./"
+ package.path = path .. '?.lua;' .. package.path
+ package.path = path .. '/?/init.lua;' .. package.path
+end
+
+setup_package()
+
+require('extensions')
+require('lib')
+
+local function table_length(t)
+ local count = 0
+
+ for _ in pairs(t) do
+ count = count + 1
+ end
+
+ return count
+end
+
+local function inspect(root)
+ return require('inspect')(root, {
+ newline = ' ',
+ indent = '',
+ })
+end
+
+assert = {
+ equals = function(expected, actual)
+ if type(expected) == 'table' and type(expected) == 'table' then
+ if table_length(expected) ~= table_length(actual) then
+ error("assertion failed: " .. inspect(expected) .. " != " .. inspect(actual))
+ end
+
+ for key, value in pairs(expected) do
+ assert.equals(value, actual[key])
+ end
+ else
+ if expected ~= actual then
+ error("assertion failed: " .. inspect(expected) .. " != " .. inspect(actual))
+ end
+ end
+ end,
+}
+
+function show_error(file, msg)
+ io.write('\x1b[2m' .. file .. '\x1b[0m: \x1b[31mfailed\x1b[0m\n')
+ io.write('\t' .. msg:gsub('\n', '\n\t') .. '\n')
+end
+
+function show_success(file)
+ io.write('\x1b[2m' .. file .. '\x1b[0m: \x1b[32msuccess\x1b[0m\n')
+end
+
+function show_skip(file)
+ io.write('\x1b[2m' .. file .. ' -- skipping\x1b[0m\n')
+end
+
+function test_module(module_path)
+ local status, ret = pcall(dofile, module_path)
+
+ if not status then
+ show_skip(module_path)
+ return
+ end
+
+ local mod = ret
+ local has_err = false
+
+ if type(mod) ~= 'table' or mod.tests == nil then
+ show_success(module_path)
+ return
+ end
+
+ for name, test in pairs(mod.tests) do
+ local status, ret = pcall(test)
+ if not status then
+ has_err = true
+ show_error(module_path, ret)
+ end
+ end
+
+ if not has_err then
+ show_success(module_path)
+ end
+end
+
+for _, a in ipairs(arg) do
+ test_module(a)
+end