-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSTest.ts
352 lines (310 loc) · 11.1 KB
/
TSTest.ts
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
module TSTest
{
class AssertInfo
{
actual: any;
expected: any;
name: string;
}
/** Collection of assertion methods */
export class Assert
{
/** Asserts that a value is true */
public static isTrue(b: boolean, name: string = ""): void
{
Assert.areIdentical(true, b, name);
}
/** Asserts that a value is false */
public static isFalse(b: boolean, name: string = ""): void
{
Assert.areIdentical(false, b, name);
}
/** Determines if two objects are identical using strict equality operator (===) */
public static areIdentical(expected: any, actual: any, name: string = ""): void
{
if (expected !== actual)
{
Assert.fail(expected, actual, name);
}
}
/** Determines if two objects are not identical using strict equality operator (===) */
public static notIdentical(expected: any, actual: any, name: string = ""): void
{
if (expected === actual)
{
Assert.fail(expected, actual, name);
}
}
/** Determines if two objects are equal using equality operator (==) */
public static areEqual(expected: any, actual: any, name: string = ""): void
{
if (expected != actual)
{
Assert.fail(expected, actual, name);
}
}
/** Determines if two objects are not equal using equality operator (==) */
public static notEqual(expected: any, actual: any, name: string = ""): void
{
if (expected == actual)
{
Assert.fail(expected, actual, name);
}
}
/** Throws a fail exception */
public static fail(expected: any, actual: any, name: string = ""): void
{
var info = new AssertInfo();
info.name = name;
info.expected = expected;
info.actual = actual;
throw info;
}
}
export interface ILogger
{
/** Starts a new test group with the specified name */
startTestGroup(name: string): void;
/** Ends a test group */
endTestGroup(): void;
/** Logs a test failed message */
testFailed(expected: string, actual: string, name?: string): void;
/** Logs a test passed message */
testPassed(name?: string): void;
/** Logs a message */
log(msg: string): void;
/** Logs an error message */
error(msg: string): void;
}
/** Implements a logger that logs to the page */
export class ElementLogger implements ILogger
{
public logElement: HTMLElement;
private curElement: HTMLElement;
/** @param parent (optional) The parent element to append results to. If not defined appends to document. */
constructor(parent?: HTMLElement)
{
this.logElement = this.createDiv("", "tstest-log");
if (!parent)
{
document.documentElement.appendChild(this.logElement);
}
else
{
parent.appendChild(this.logElement);
}
this.curElement = this.logElement;
this.addStyle();
}
public startTestGroup(name: string): void
{
var header = this.createDiv(name, "test-header", "test-pass");
this.curElement.appendChild(header);
var group = this.createDiv("", "test-group");
this.curElement.appendChild(group);
this.curElement = group;
}
public endTestGroup(): void
{
this.curElement = this.curElement.parentElement;
}
public testFailed(expected: string, actual: string, name = "Result"): void
{
var msg = (name || "unnamed") + ": Expected: " + expected + ", Actual: " + actual;
var div = this.createDiv(msg, "test-fail");
this.curElement.appendChild(div);
this.propogateFail();
}
public testPassed(name = "Result"): void
{
var msg = name + ": Passed";
var div = this.createDiv(msg, "test-pass");
this.curElement.appendChild(div);
}
public log(msg: string): void
{
var div = this.createDiv(msg);
this.curElement.appendChild(div);
}
public error(msg: string): void
{
var div = this.createDiv(msg, "test-error");
this.curElement.appendChild(div);
this.propogateFail();
}
private propogateFail()
{
// Change the class of each parent group's header to fail
var header = <HTMLElement>this.curElement.previousElementSibling;
while (header && header.classList.contains("test-header"))
{
header.classList.remove("test-pass");
header.classList.add("test-fail");
header = <HTMLElement>header.parentElement.previousElementSibling;
}
}
private createDiv(text: string, ...classNames: string[]): HTMLElement
{
var div = document.createElement("div");
if (text) div.textContent = text;
classNames.forEach((name) => div.classList.add(name));
return div;
}
private addStyle()
{
var style = document.createElement("style");
style.type = "text/css";
var css =
".tstest-log { font-size: 1em; }" +
".tstest-log .test-pass { color: green; }" +
".tstest-log .test-fail { color: red; }" +
".tstest-log .test-error { color: red; }" +
".tstest-log .test-group{padding-left: 2em; border-left: 1px dotted silver;}" +
".tstest-log .test-header{font-weight: bold; font-size: 1.2em;}";
style.appendChild(document.createTextNode(css));
this.logElement.appendChild(style);
}
}
/** Implements a logger that logs to the console */
export class ConsoleLogger implements ILogger
{
private indent = "";
public startTestGroup(name: string): void
{
this.log(name);
this.indent += " ";
}
public endTestGroup(): void
{
this.indent = this.indent.slice(2);
}
public testFailed(expected: string, actual: string, name = "Result"): void
{
var msg = (name || "unnamed") + ": Expected: " + expected + ", Actual: " + actual;
this.error(msg);
}
public testPassed(name = "Result"): void
{
var msg = name + ": Passed";
this.log(msg);
}
public log(msg: string): void
{
console.log(this.indent + msg);
}
public error(msg: string): void
{
console.error(this.indent + msg);
}
}
/** Base class for all unit test classes. Test methods must be prefixed with "test". */
export class UnitTest
{
public assert = TSTest.Assert;
/** Setup method called before any tests have been run. Override to provide custom setup. */
public setUp(): void
{
}
/** Tear down method called after all tests have been run. Override to provide custom teardown. */
public tearDown(): void
{
}
/** Setup method called before each test method is called. Override to provide custom setup. */
public setUpTest(): void
{
}
/** Tear down method called after each test method has been called. Override to provide custom teardown. */
public tearDownTest(): void
{
}
/** Iterates over all of the test methods (those that start with "test") */
public each(callback: (fn: Function, name: string) => any): void
{
for (var name in this)
{
if (name.indexOf("test") === 0 && typeof (this[name]) === 'function')
{
callback(this[name], name);
}
}
}
}
/** Defines a unit test suite, which is a collection of unit tests */
export class UnitTestSuite
{
private unitTests: UnitTest[] = [];
private errorCount = 0;
private logs: ILogger[] = [];
constructor()
{
this.addLogger(new ConsoleLogger());
}
/** Adds a logger to log test output to */
public addLogger(logger: ILogger): UnitTestSuite
{
this.logs.push(logger);
return this;
}
/** Adds a unit test class */
public addUnitTest(test: UnitTest): UnitTestSuite
{
this.unitTests.push(test);
return this;
}
/** Runs all of the unit tests added by addUNitTest() */
public run(): void
{
this.logs.forEach((log) => log.startTestGroup("Starting Tests: " + new Date().toString() + "..."));
var count = 0;
this.errorCount = 0;
this.unitTests.forEach((unitTest) =>
{
count += this.runUnitTest(unitTest);
});
this.logs.forEach((log) => log.endTestGroup());
this.logs.forEach((log) => log.startTestGroup(count + " Tests Completed: " + new Date().toString()));
this.logs.forEach((log) => log.log((count - this.errorCount) + " tests passed"));
if (this.errorCount)
{
this.logs.forEach((log) => log.error(this.errorCount + " tests failed"));
}
this.logs.forEach((log) => log.endTestGroup());
}
private runUnitTest(unitTest: UnitTest): number
{
this.logs.forEach((log) => log.startTestGroup("Test Group: " + unitTest["constructor"]["name"]));
var count = 0;
unitTest.setUp();
unitTest.each((testFn: Function, name: string) =>
{
this.logs.forEach((log) => log.startTestGroup("Test: " + name));
unitTest.setUpTest();
try
{
testFn.call(unitTest);
this.logs.forEach((log) => log.testPassed(name));
}
catch (ex)
{
this.errorCount++;
if (ex instanceof AssertInfo)
{
// Assertion exception
this.logs.forEach((log) => log.testFailed(ex.expected, ex.actual, ex.name));
}
else
{
// Must be a system exception
this.logs.forEach((log) => log.error(ex));
}
}
unitTest.tearDownTest();
count++;
this.logs.forEach((log) => log.endTestGroup());
});
unitTest.tearDown();
this.logs.forEach((log) => log.endTestGroup());
return count;
}
}
}