aboutsummaryrefslogtreecommitdiff
path: root/src/check
blob: 99983e259186ddbd03611da4d3d8258890ff7917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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