-
Notifications
You must be signed in to change notification settings - Fork 6
/
TestRunner.ts
237 lines (190 loc) · 8.83 KB
/
TestRunner.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
import {Flask} from "./Flask"
import {AsyncIterator} from "./helpers/AsyncIterator"
declare function require(module:string);
declare let console:any;
export function feature(value:string){
return function(target){
TestRunner.setMetadata(target.name,"__feature","value", value);
}
}
export function jiraTask(value:string){
return function(target){
TestRunner.setMetadata(target.name,"__jiratask","value", value);
}
}
export function asyncTest(){
return function(target:any, propertyKey, descriptor: PropertyDescriptor){
TestRunner.setMetadata(target.constructor.name,propertyKey,"isAsync", true);
}
}
export function scenario(value:string){
return function(target:any, propertyKey: string, descriptor: PropertyDescriptor){
TestRunner.setMetadata(target.constructor.name,propertyKey,"scenario", value);
}
}
export function outcome(value:string){
return function(target:any, propertyKey: string, descriptor: PropertyDescriptor){
TestRunner.setMetadata(target.constructor.name,propertyKey,"thentext", value);
}
}
export class TestRunner {
private static _metadata = {};
public static setMetadata(className, method, key, value){
if (TestRunner._metadata[className] === undefined)
TestRunner._metadata[className] = {};
if (TestRunner._metadata[className][method] === undefined)
TestRunner._metadata[className][method] = {};
TestRunner._metadata[className][method][key] = value;
}
public static getMetadata(className, method){
if (TestRunner._metadata[className] !== undefined)
if (TestRunner._metadata[className][method] !== undefined)
return TestRunner._metadata[className][method];
}
public static getMetadataForClass(className){
return TestRunner._metadata[className];
}
public static getAllMetadata(){
return TestRunner._metadata;
}
private static getScenarioMethods(className){
let outData = [];
let metdata = TestRunner._metadata[className];
if (metdata != undefined){
for (let key in metdata){
if (!key.startsWith("__"))
outData.push(key);
}
}
return outData;
}
public static run(arr:any): Promise<any>{
Flask.initialize();
if (arr instanceof Array){
let promiseArray = [];
arr.forEach((item)=>{
let pObj = TestRunner._run(new item());
promiseArray.push(pObj);
});
return Promise.all(promiseArray);
} else {
return TestRunner._run(new arr());
}
}
private static _run (typeObj: any): Promise<any>{
return new Promise<any>((mainResolve,mainReject)=>{
if (typeObj.inject !== undefined)
typeObj.inject();
let metadataClass = TestRunner.getMetadataForClass(typeObj.constructor.name);
if (metadataClass == undefined)
return;
let methods = TestRunner.getScenarioMethods(typeObj.constructor.name);
let feature = metadataClass["__feature"];
feature = (feature == undefined) ? typeObj.constructor.name : feature.value;
let jiraTask = metadataClass["__jiratask"];
jiraTask = (jiraTask == undefined) ? "" : "(" + jiraTask.value + ") ";
let resultObject = {feature: feature, jiraTask:jiraTask, scenarios:[]};
let iterator = new AsyncIterator(methods, {});
iterator.logic((attributeKey,inObj, asyncDone)=>{
if (typeObj[attributeKey] !== undefined)
if (typeof typeObj[attributeKey] === "function"){
let metadata = TestRunner.getMetadata(typeObj.constructor.name, attributeKey);
let scenario:string = metadata.scenario == undefined ? "UNKNOWN SCENARIO" : metadata.scenario;
let isAsync:boolean = metadata.isAsync == undefined ? false : metadata.isAsync;
let scenarioText = "Scenario : " + scenario;
let controller:any = (function(thentext,scenario){
let lastFunc = "given";
let scenarioObj = {name:scenario, given:[],when:[],then:[]};
if (thentext!==undefined)
scenarioObj.then.push({message: "Outcome: " + thentext});
let lastThen:any = scenarioObj.then.length > 0 ? scenarioObj.then[scenarioObj.then.length-1]: {};
function write (text, tag, hideCaption = false){
if (!hideCaption){
scenarioObj[lastFunc].push(tag + " : " + text)
}
}
let controllerMethods = {
given: function(text, hideCaption){
lastFunc = "given";
write(text,"Given", hideCaption);
},
when: function(text, hideCaption){
lastFunc = "when";
write(text,"When", hideCaption);
},
then: function(text, hideCaption){
if (thentext !== undefined)
return;
lastFunc = "then";
write("","Then", hideCaption);
},
and: function(text){
write(text,"And");
controllerMethods[lastFunc](text,true);
},
logError: function (e){
lastThen.exception = e;
},
getScenarioObject: function(){
return scenarioObj;
}
};
return controllerMethods;
})(metadata.thentext,scenarioText);
if (isAsync){
try {
let promiseObj = typeObj[attributeKey](controller.given,controller.when,controller.then,controller.and);
promiseObj
.then(()=>{
asyncDone(controller.getScenarioObject());
})
.catch((e)=>{
controller.logError(e);
asyncDone(controller.getScenarioObject());
});
}
catch (e){
controller.logError(e);
asyncDone(controller.getScenarioObject());
}
}else {
try{
typeObj[attributeKey](controller.given,controller.when,controller.then,controller.and);
}
catch (e){
console.log(e);
controller.logError(e);
}
asyncDone(controller.getScenarioObject());
}
}
});
iterator.onCompleteOne((scenario)=>{
resultObject.scenarios.push(scenario)
});
iterator.onComplete(()=>{
let rObj = resultObject;
console.log ("Feature " + rObj.jiraTask + ": ", rObj.feature);
rObj.scenarios.forEach((scenario)=>{
console.log ("\t" + scenario.name);
let caption = "";
scenario.given.forEach(txt => {caption += txt;});
scenario.when.forEach(txt => {caption += txt;});
if (caption)
console.log ("\t" + caption);
for (let i=0;i<scenario.then.length;i++){
let thenFunc = scenario.then[i];
console.log ("\t" + thenFunc.message);
if (thenFunc.exception)
console.log ("\x1b[31m","\tTest Failed (x) : " + thenFunc.exception, "\x1b[0m");
else
console.log ("\x1b[32m","\tTest Passed (/)", "\x1b[0m");
}
});
console.log ("\n");
mainResolve(resultObject);
});
iterator.start();
});
}
}