-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoHotUnit.ahk
278 lines (225 loc) · 7.45 KB
/
AutoHotUnit.ahk
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#SingleInstance Force
#Warn All, StdOut
FileEncoding("UTF-8")
global ahu := AutoHotUnitManager(AutoHotUnitCLIReporter())
class AutoHotUnitSuite {
assert := AutoHotUnitAsserter()
; Executed once before any tests execute
beforeAll() {
}
; Executed once before each test is executed
beforeEach() {
}
; Executed once after each test is executed
afterEach() {
}
; Executed once after all tests have executed
afterAll() {
}
}
class AutoHotUnitManager {
Suite := AutoHotUnitSuite
suites := []
__New(reporter) {
this.reporter := reporter
}
RegisterSuite(SuiteSubclasses*)
{
for i, subclass in SuiteSubclasses {
this.suites.push(subclass)
}
}
RunSuites() {
this.reporter.onRunStart()
for i, suiteClass in this.suites {
suiteInstance := suiteClass()
suiteName := suiteInstance.__Class
this.reporter.onSuiteStart(suiteName)
testNames := []
for propertyName in suiteInstance.base.OwnProps() {
; If the property name starts with an underscore, skip it
underScoreIndex := InStr(propertyName, "_")
if (underScoreIndex == 1) {
continue
}
; If the property name is one of the Suite base class methods, skip it
if (propertyName == "beforeAll" || propertyName == "beforeEach" || propertyName == "afterEach" || propertyName == "afterAll") {
continue
}
if (GetMethod(suiteInstance, propertyName) is Func) {
testNames.push(propertyName)
}
}
try {
suiteInstance.beforeAll()
} catch Error as e {
this.reporter.onTestResult("beforeAll", "failed", "", e)
continue
}
for j, testName in testNames {
try {
suiteInstance.beforeEach()
} catch Error as e {
this.reporter.onTestResult(testName, "failed", "beforeEach", e)
continue
}
try {
local method := GetMethod(suiteInstance, testName)
method(suiteInstance)
} catch Error as e {
this.reporter.onTestResult(testName, "failed", "test", e)
continue
}
try {
suiteInstance.afterEach()
} catch Error as e {
this.reporter.onTestResult(testName, "failed", "afterEach", e)
continue
}
this.reporter.onTestResult(testName, "passed", "", "")
}
try {
suiteInstance.afterAll()
} catch Error as e {
this.reporter.onTestResult("afterAll", "failed", "", e)
continue
}
this.reporter.onSuiteEnd(suiteName)
}
this.reporter.onRunComplete()
}
}
class AutoHotUnitCLIReporter {
currentSuiteName := ""
failures := []
red := "[31m"
green := "[32m"
reset := "[0m"
printLine(str) {
FileAppend(str, "*", "UTF-8")
FileAppend("`r`n", "*")
}
onRunStart() {
this.printLine("Starting test run`r`n")
}
onSuiteStart(suiteName) {
this.printLine(suiteName ":")
this.currentSuiteName := suiteName
}
onTestStart(testName) {
}
onTestResult(testName, status, where, error) {
if (status != "passed" && status != "failed") {
throw Error("Invalid status: " . status)
}
prefix := this.green . "."
if (status == "failed") {
prefix := this.red . "x"
}
this.printLine(" " prefix " " testName " " status this.reset)
if (status == "failed") {
this.printLine(this.red " " error.Message this.reset)
this.failures.push(this.currentSuiteName "." testName " " where " failed:`r`n " error.Message)
}
}
onSuiteEnd(suiteName) {
}
onRunComplete() {
this.printLine("")
postfix := "All tests passed."
if (this.failures.Length > 0) {
postfix := this.failures.Length . " test(s) failed."
}
this.printLine("Test run complete. " postfix)
if (this.failures.Length > 0) {
this.printLine("")
}
for i, failure in this.failures {
this.printLine(this.red failure this.reset)
}
Exit(this.failures.Length)
}
}
class AutoHotUnitAsserter {
static deepEqual(actual, expected) {
if (actual is Array && expected is Array) {
if (actual.Length != expected.Length) {
return false
}
for i, actualItem in actual {
if (!this.deepEqual(actualItem, expected[i])) {
return false
}
}
return true
}
return actual == expected
}
static getPrintableValue(value) {
if (value is Array) {
str := "["
for i, item in value {
if (i > 1) {
str .= ", "
}
str .= this.getPrintableValue(item)
}
str .= "]"
return str
}
return value
}
equal(actual, expected) {
if (!AutoHotUnitAsserter.deepEqual(actual, expected)) {
throw Error("Assertion failed: " . AutoHotUnitAsserter.getPrintableValue(actual) . " != " . AutoHotUnitAsserter.getPrintableValue(expected))
}
}
notEqual(actual, expected) {
if (actual == expected) {
throw Error("Assertion failed: " . actual . " == " . expected)
}
}
isTrue(actual) {
if (actual != true) {
throw Error("Assertion failed: " . actual . " is not true")
}
}
isFalse(actual) {
if (actual == true) {
throw Error("Assertion failed: " . actual . " is not false")
}
}
isEmpty(actual) {
if (actual != "") {
throw Error("Assertion failed: " . actual . " is not empty")
}
}
notEmpty(actual) {
if (actual == "") {
throw Error("Assertion failed: " . actual . " is empty")
}
}
fail(message) {
throw Error("Assertion failed: " . message)
}
isAbove(actual, expected) {
if (actual <= expected) {
throw Error("Assertion failed: " . actual . " is not above " . expected)
}
}
isAtLeast(actual, expected) {
if (actual < expected) {
throw Error("Assertion failed: " . actual . " is not at least " . expected)
}
}
isBelow(actual, expected) {
if (actual >= expected) {
throw Error("Assertion failed: " . actual . " is not below " . expected)
}
}
isAtMost(actual, expected) {
if (actual > expected) {
throw Error("Assertion failed: " . actual . " is not at most " . expected)
}
}
}