-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsole_reporter.luau
203 lines (183 loc) · 6.25 KB
/
console_reporter.luau
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-- usage:
-- local reporter = require("@frktest/reporters/console_reporter")
-- reporter.init()
-- -- run tests
local types = require("../types")
local utils = require("../utils")
local inspect = require("../lib/inspect")
local color = require("./runtimes/color")
type Failure = types.Failure
type DiffEntry<T> = types.DiffEntry<T>
type TableDiff = types.TableDiff
type TestError = types.TestError
local reporter = {}
local _state = {}
_state.indent = 0
_state.current_suite = nil :: types.Suite?
_state.current_test = nil :: types.Test?
_state.suite_printed = false
_state.test_printed = false
_state = require("../state").get_state("console_reporter", _state)
function print_indent(s: string, offset: number?)
if offset == nil then
offset = 0
end
local indent = _state.indent + offset :: number
return print(utils.make_indent(indent) .. s)
end
function print_test_info_once()
if _state.current_suite ~= nil and _state.suite_printed == false then
_state.suite_printed = true
print_indent(`Suite: {color("purple", _state.current_suite.name)}`, -2)
print("")
end
if _state.current_test ~= nil and _state.test_printed == false then
_state.test_printed = true
print_indent(`Test: {color("purple", _state.current_test.name)}`, -1)
print("")
end
end
function make_array_diff_string<T>(diff: { [number]: DiffEntry<T> }): string
local make_line = function<T>(e: DiffEntry<T>): string
local colors = {
["+"] = "green",
["-"] = "red",
["~"] = "yellow",
}
local value_s
if e.t == "~" then
value_s = `{(e.v :: any).a} == {(e.v :: any).b}`
else
value_s = `{e.v}`
end
return color(colors[e.t], `{e.t} [{e.k}]: {value_s}`)
end
local message = {}
for _, d in ipairs(diff) do
table.insert(message, make_line(d))
end
return table.concat(message, "\n")
end
function make_table_diff_string(diff: TableDiff): string
local make_line = function<T>(i: number, e: DiffEntry<any>): string
local colors = {
["+"] = "green",
["-"] = "red",
["~"] = "yellow",
}
local value_s = ""
local inspect_opts = { newline = "", indent = "" }
if e.t == "~" then
value_s = `{inspect((e.v :: any).a, inspect_opts)} == {inspect((e.v :: any).b, inspect_opts)}`
else
value_s = inspect(e.v, inspect_opts)
end
return color(colors[e.t], `{utils.make_indent(i)}{e.t} [{e.k}]: {value_s}`)
end
local message = {}
local function add_to_diff_message(diff: TableDiff, i: number)
for _, v in ipairs(diff.diff) do
table.insert(message, make_line(i, v))
end
if diff.sub ~= nil then
for k, v in pairs(diff.sub) do
table.insert(message, `{utils.make_indent(i)} [{k}]:`)
add_to_diff_message(v, i + 1)
end
end
end
add_to_diff_message(diff, 0)
return table.concat(message, "\n")
end
function handle_complex_info(type: string, data: any): string?
if type == "ArrayDiff" then
return make_array_diff_string(data :: { [number]: DiffEntry<any> })
elseif type == "TableDiff" then
return make_table_diff_string(data :: TableDiff)
end
return nil
end
function reporter.print_failure(type: "CHECK" | "REQUIRE", f: Failure)
print_test_info_once()
print_indent(`{color("red", `{type} FAILED:`)} {f.location.file}:{f.location.line}`)
print_indent(`{color("yellow", f.message)}`, 1)
if f.complex_info ~= nil then
local complex_msg = handle_complex_info(f.complex_info.type, f.complex_info.data)
if complex_msg ~= nil then
print(utils.indent_multiline(complex_msg, _state.indent + 1))
end
end
if f.user_msg ~= nil then
print_indent("with message:")
print_indent(color("yellow", f.user_msg), 1)
end
print("")
end
function reporter.print_test_error(e: TestError)
print_test_info_once()
print_indent(`{color("red", "ERROR:")} {e.location.file}:{e.location.line}`)
print_indent(`{color("yellow", e.error)}`, 1)
print_indent("trace:")
print(`{color("yellow", utils.indent_multiline(utils.trim(e.strace), _state.indent + 1))}`)
print("")
end
function reporter.print_counts(tests: number, suites: number)
print(`Running - {tests} tests | {suites} suites...`)
print("")
end
function reporter.print_summary(ran: number, passed: number, failed: number, skipped: number)
print("--------------------")
local summary = `Tests Ran: {ran}`
if passed > 0 then
summary = summary .. " | " .. color("green", `Passed: {passed}`)
end
if failed > 0 then
summary = summary .. " | " .. color("red", `Failed: {failed}`)
end
if skipped > 0 then
summary = summary .. " | " .. color("yellow", `Skipped: {skipped}`)
end
print(summary)
local status = if failed == 0 then color("green", "OK") else color("red", "FAIL")
status = if ran == 0 then color("yellow", "SKIPPED") else status
print(`status: {status}`)
end
function reporter.init()
-- register testing events to keep track of current testing state
local test = require("../test")
test.on_suite_enter(function(s)
_state.suite_printed = false
_state.current_suite = s
_state.indent += 1
end)
test.on_suite_leave(function(_)
_state.current_suite = nil
_state.indent -= 1
end)
test.on_test_enter(function(t)
_state.test_printed = false
_state.current_test = t
_state.indent += 1
end)
test.on_test_leave(function(_)
_state.current_test = nil
_state.indent -= 1
end)
test.on_exec_begin(function(c)
reporter.print_counts(c.tests, c.suites)
end)
test.on_exec_end(function(r)
reporter.print_summary(r.ran, r.passed, r.failed, r.skipped)
end)
test.on_test_error(function(e)
reporter.print_test_error(e)
end)
local assert = require("../assert")
assert.on_check_failed(function(f)
reporter.print_failure("CHECK", f)
end)
assert.on_require_failed(function(f)
reporter.print_failure("REQUIRE", f)
end)
end
return reporter