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 | 1×
1×
3×
1×
1×
1×
1×
1×
1×
1×
60×
1×
1×
1×
263×
263×
263×
263×
263×
263×
263×
213×
50×
1×
50×
50×
50×
50×
1×
213×
213×
1×
263×
263×
356×
263×
1×
263×
263×
263×
263×
1×
1×
1×
13×
13×
13×
13×
1×
12×
1×
60×
60×
60×
13×
47×
1×
263×
263×
60×
203×
1×
| import { MatchError } from "./errors/match-error";
import { TestSet } from "./test-set";
import "reflect-metadata";
export class TestRunner {
private _testsFocussed: boolean;
private _currentTestSet: TestSet;
private _currentTestId: number = 0;
private _currentTestFixtureIndex: number = 0;
private _currentTestIndex: number = 0;
private _currentTestCaseIndex: number = 0;
public run(testSet: TestSet) {
this._currentTestSet = testSet;
Iif (this._currentTestSet.testFixtures.length === 0) {
process.stderr.write("no tests to run\n");
process.exit(1);
}
else {
let totalTestCount = this._currentTestSet.testFixtures.map(x => x.tests.map((y: any) => y.testCases.length).reduce((a: number, b: number) => a + b)).reduce((a: number, b: number) => a + b);
process.stdout.write("TAP version 13\n");
process.stdout.write(`1..${totalTestCount}\n`);
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
}
}
private _runTest(testFixture: any, test: any, testCaseArguments: Array<any>) {
this._currentTestId++;
let setupFunctions: Array<string> = Reflect.getMetadata("alsatian:setup", testFixture.fixture);
Iif (setupFunctions) {
setupFunctions.forEach(setupFunction => {
testFixture.fixture[setupFunction].call(testFixture.fixture);
});
}
try {
Iif (test.isAsync) {
let timeout = false;
let promise: any = testFixture.fixture[test.key].apply(testFixture.fixture, testCaseArguments);
promise.then(() => {
// TODO: Cancel promise on timeout instead;
if (!timeout) {
this._notifySuccess(test, testCaseArguments);
}
})
.catch((error: Error) => {
this._handleError(error, test, testCaseArguments);
});
let timeoutCheck = setTimeout(() => {
// TODO: Cancel promise on timeout instead;
timeout = true;
this._handleError(new /*MatchError("longer than 500ms", "less than 500ms",*/Error("The test exceeded the given timeout."), test, testCaseArguments);
}, 500);
}
else {
testFixture.fixture[test.key].apply(testFixture, testCaseArguments);
this._notifySuccess(test, testCaseArguments);
}
}
catch (error) {
this._handleError(error, test, testCaseArguments);
}
}
private _handleError(error: Error, test: any, testCaseArguments: Array<any>) {
process.stdout.write(`not ok ${this._getTestDescription(test, testCaseArguments)}\n`);
Eif (error instanceof MatchError) {
process.stdout.write(` ---\n message: "${error.message}"\n severity: fail\n data:\n got: ${JSON.stringify(error.actualValue)}\n expect: ${JSON.stringify(error.expectedValue)}\n ...\n`);
}
else {
console.log(error);
process.stdout.write("# Unknown Error\n");
}
this._teardown();
}
private _notifySuccess(test: any, testCaseArguments: Array<any>) {
process.stdout.write(`ok ${this._getTestDescription(test, testCaseArguments)}\n`);
this._teardown();
}
private _getTestDescription = (test: any, testCaseArguments: Array<any>) => {
let testDescription = `${this._currentTestId} - ${test.description}`;
if (testCaseArguments !== undefined) {
testDescription += ` [ ${testCaseArguments.map(x => JSON.stringify(x) || "undefined").join(", ")} ]`;
}
return testDescription;
}
private _teardown() {
let testFixture = this._currentTestSet.testFixtures[this._currentTestFixtureIndex];
let teardownFunctions: Array<string> = Reflect.getMetadata("alsatian:teardown", testFixture.fixture);
Iif (teardownFunctions) {
teardownFunctions.forEach(teardownFunction => {
testFixture.fixture[teardownFunction].call(testFixture.fixture);
});
}
this._runNextTestCase();
}
private _exit() {
process.exit(0);
}
private _runNextTestFixture() {
this._currentTestFixtureIndex++;
this._currentTestIndex = 0;
this._currentTestCaseIndex = 0;
if (!this._currentTestSet.testFixtures[this._currentTestFixtureIndex]) {
this._exit();
}
else {
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments)
}
}
private _runNextTest() {
this._currentTestIndex++;
this._currentTestCaseIndex = 0;
if (!this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]) {
this._runNextTestFixture();
}
else {
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments)
}
}
private _runNextTestCase = () => {
this._currentTestCaseIndex++;
if (!this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex]) {
this._runNextTest();
}
else {
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex],
this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments)
}
}
}
|