-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsUnitAsync.ts
149 lines (129 loc) · 5.32 KB
/
tsUnitAsync.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
import {
Test,
TestContext,
TestClass,
FakeFactory,
ITestRunLimiter,
TestDescription,
TestDefinition,
FunctionPropertyHelper } from './tsUnit';
export {
Test,
TestContext,
TestClass,
FakeFactory,
ITestRunLimiter,
TestDescription,
TestDefinition
} from './tsUnit';
export class TestAsync extends Test {
private _currentTestPromises: Promise<any>[];
private runAll(tests: TestDefinition[], testRunLimiter: ITestRunLimiter): Promise<this> {
let thisTest = tests[0];
var testClass = thisTest.testClass;
var dynamicTestClass = <any>testClass;
var testsGroupName = thisTest.name;
var propertyNames = FunctionPropertyHelper.getFunctionNames(testClass);
let functions: string[] = [];
for (var j = 0; j < propertyNames.length; j++) {
let unitTestName = propertyNames[j];
if (!this.isReservedFunctionName(unitTestName) &&
!(unitTestName.substring(0, this.privateMemberPrefix.length) === this.privateMemberPrefix) &&
!(typeof dynamicTestClass[unitTestName] !== 'function') &&
(!testRunLimiter || testRunLimiter.isTestActive(unitTestName))) {
functions.push(unitTestName);
}
}
let remainingTests = tests.slice(1);
var promise = this.runAllFunctions(thisTest, functions, testRunLimiter);
if (remainingTests.length) {
return promise.then(() => this.runAll(remainingTests, testRunLimiter));
}
return promise;
}
private runAllFunctions(thisTest: TestDefinition, functionNames: string[], testRunLimiter: ITestRunLimiter): Promise<this> {
let unitTestName = functionNames[0];
let remainingFunctions = functionNames.slice(1);
var testClass = thisTest.testClass;
var dynamicTestClass = <any>testClass;
var testsGroupName = thisTest.name;
var promise: Promise<this>;
if (typeof dynamicTestClass[unitTestName].parameters !== 'undefined') {
let parameters = dynamicTestClass[unitTestName].parameters;
promise = this.runAllParameters(thisTest, unitTestName, 0, testRunLimiter);
} else {
promise = this.runSingleTestAsync(testClass, unitTestName, testsGroupName);
}
if (remainingFunctions.length > 0) {
promise = promise.then(() => this.runAllFunctions(thisTest, remainingFunctions, testRunLimiter));
}
promise.then((x) => {
testClass.tearDown && testClass.tearDown();
return x;
}, (err) => {
testClass.tearDown && testClass.tearDown();
throw err;
});
return promise;
}
private runAllParameters(
thisTest:TestDefinition,
unitTestName: string,
parameterIndex:number,
testRunLimiter: ITestRunLimiter
):Promise<this> {
let testClass = thisTest.testClass;
let dynamicTestClass = <any>testClass;
let testsGroupName = thisTest.name;
let parameters = dynamicTestClass[unitTestName].parameters;
let maxIndex = parameters.length - 1;
var index = parameterIndex;
if (testRunLimiter) {
while (index < parameters.length && !testRunLimiter.isParametersSetActive(index)) {
++index;
}
}
if (index < parameters.length) {
return this.runSingleTestAsync(testClass, unitTestName, testsGroupName, parameters, index)
.then(() => this.runAllParameters(thisTest, unitTestName, index+1, testRunLimiter));
}
return Promise.resolve(this);
}
protected runSingleTestAsync(
testClass: TestClass,
unitTestName: string,
testsGroupName: string,
parameters: any[][] | null = null,
parameterSetIndex: number | null = null
):Promise<this> {
// running everything inside .then saves us a try/catch
return Promise.resolve().then(() => {
testClass.setUp && testClass.setUp();
var dynamicTestClass: any = testClass;
var args = (parameterSetIndex !== null) ? parameters![parameterSetIndex] : null;
return dynamicTestClass[unitTestName].apply(testClass, args);
}).then(() => {
this.passes.push(new TestDescription(testsGroupName, unitTestName, parameterSetIndex, 'OK'));
return this;
}, (err:any) => {
this.errors.push(new TestDescription(testsGroupName, unitTestName, parameterSetIndex, err.toString()));
return this;
});
}
runAsync(testRunLimiter: ITestRunLimiter | null = null): Promise<this> {
var parameters: any[][] | null = null;
var testContext = new TestContext();
if (testRunLimiter == null) {
testRunLimiter = this.testRunLimiter;
}
var tests = this.tests;
if (testRunLimiter != null) {
tests = tests.filter((x) => testRunLimiter!.isTestsGroupActive(x.name));
}
return this.runAll(tests, testRunLimiter);
}
run():this {
console.log("use runAsync");
throw new Error("use runAsync");
}
}