forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep.js
More file actions
206 lines (177 loc) · 4.82 KB
/
step.js
File metadata and controls
206 lines (177 loc) · 4.82 KB
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
const store = require('./store');
const Secret = require('./secret');
const STACK_LINE = 4;
// using support objetcs for metastep detection
// deprecated
let support;
/**
* Each command in test executed through `I.` object is wrapped in Step.
* Step allows logging executed commands and triggers hook before and after step execution.
*/
class Step {
constructor(helper, name) {
this.actor = 'I'; // I = actor
this.helper = helper; // corresponding helper
this.name = name; // name of a step console
this.helperMethod = name; // helper method
this.status = 'pending';
this.prefix = this.suffix = '';
this.comment = '';
this.args = [];
this.stack = '';
this.setTrace();
}
setTrace() {
Error.captureStackTrace(this);
this.metaStep = detectMetaStep(this.stack.split('\n'));
}
setArguments(args) {
this.args = args;
}
run() {
this.args = Array.prototype.slice.call(arguments);
if (store.dryRun) {
this.setStatus('success');
return Promise.resolve(new Proxy({}, dryRunResolver()));
}
let result;
try {
result = this.helper[this.helperMethod].apply(this.helper, this.args);
this.setStatus('success');
} catch (err) {
this.setStatus('failed');
throw err;
}
return result;
}
setStatus(status) {
this.status = status;
if (this.metaStep) this.metaStep.setStatus(status);
}
humanize() {
return this.name
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// _ chars to spaces
.replace('_', ' ')
// uppercase the first character
.replace(/^(.)|\s(.)/g, $1 => $1.toLowerCase());
}
humanizeArgs() {
return this.args.map((arg) => {
if (typeof arg === 'string') {
return `"${arg}"`;
} else if (Array.isArray(arg)) {
try {
const res = JSON.stringify(arg);
return res;
} catch (err) {
return `[${arg.toString()}]`;
}
} else if (typeof arg === 'function') {
return arg.toString();
} else if (typeof arg === 'undefined') {
return `${arg}`;
} else if (arg instanceof Secret) {
return '*****';
} else if (arg.toString && arg.toString() !== '[object Object]') {
return arg.toString();
} else if (typeof arg === 'object') {
return JSON.stringify(arg);
}
return arg;
}).join(', ');
}
line() {
const lines = this.stack.split('\n');
if (lines[STACK_LINE]) return lines[STACK_LINE].trim();
return '';
}
toString() {
return `${this.prefix}${this.actor} ${this.humanize()} ${this.humanizeArgs()}${this.suffix}`;
}
toCode() {
return `${this.prefix}${this.actor}.${this.name}(${this.humanizeArgs()})${this.suffix}`;
}
isMetaStep() {
return this.constructor.name === 'MetaStep';
}
hasBDDAncestor() {
let hasBDD = false;
let processingStep;
processingStep = this;
while (processingStep.metaStep) {
if (processingStep.metaStep.actor.match(/^(Given|When|Then|And)/)) {
hasBDD = true;
break;
} else {
processingStep = processingStep.metaStep;
}
}
return hasBDD;
}
}
class MetaStep extends Step {
constructor(obj, method) {
super(null, method);
this.actor = obj;
}
humanize() {
return this.name;
}
setTrace() {
}
run() {
}
}
Step.MetaStep = MetaStep;
module.exports = Step;
function detectMetaStep(stack) {
if (store.debugMode) return; // no detection in debug
if (!support) loadSupportObjects(); // deprecated
for (let i = STACK_LINE; i < stack.length; i++) {
const line = stack[i].trim();
if (isTest(line) || isBDD(line)) break;
const fnName = line.match(/^at (\w+)\.(\w+)\s\(/);
if (!fnName) continue;
if (fnName[1] === 'Generator'
|| fnName[1] === 'recorder'
|| fnName[1] === 'Runner'
) { return; } // don't track meta steps inside generators
if (fnName[1] === 'Object') {
// detect PO name from includes
for (const name in support) {
const file = support[name];
if (line.indexOf(file) > -1) {
return new MetaStep(`${name}:`, fnName[2]);
}
}
}
return new MetaStep(`${fnName[1]}:`, fnName[2]);
}
}
function loadSupportObjects() {
const config = require('./config');
const path = require('path');
support = config.get('include', {});
if (support) {
for (const name in support) {
const file = support[name];
support[name] = path.join(global.codecept_dir, file);
}
}
}
function isTest(line) {
return line.trim().match(/^at Test\.Scenario/);
}
function isBDD(line) {
return line.trim().match(/^at (Given|When|Then)/);
}
function dryRunResolver() {
return {
get(target, prop) {
if (prop === 'toString') return () => '<VALUE>';
return new Proxy({}, dryRunResolver());
},
};
}