-
-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathbase.js
More file actions
180 lines (157 loc) · 4.35 KB
/
base.js
File metadata and controls
180 lines (157 loc) · 4.35 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
const color = require('chalk')
const Secret = require('../secret')
const { getCurrentTimeout } = require('./timeout')
const { ucfirst, humanizeString } = require('../utils')
const STACK_LINE = 5
/**
* 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.
* @param {string} name
*/
class Step {
constructor(name) {
/** @member {string} */
this.name = name
/** @member {Map<number, number>} */
this.timeouts = new Map()
/** @member {Array<*>} */
this.args = []
/** @member {Record<string,any>} */
this.opts = {}
/** @member {string} */
this.actor = 'I' // I = actor
/** @member {string} */
this.helperMethod = name // helper method
/** @member {string} */
this.status = 'pending'
/** @member {string} */
this.prefix = this.suffix = ''
/** @member {string} */
this.comment = ''
/** @member {any} */
this.metaStep = undefined
/** @member {string} */
this.stack = ''
this.setTrace()
}
setMetaStep(metaStep) {
this.metaStep = metaStep
}
run() {
throw new Error('Not implemented')
}
/**
* @returns {number|undefined}
*/
get timeout() {
return getCurrentTimeout(this.timeouts)
}
/**
* @param {number} timeout - timeout in milliseconds or 0 if no timeout
* @param {number} order - order defines the priority of timeout, timeouts set with lower order override those set with higher order.
* When order below 0 value of timeout only override if new value is lower
*/
setTimeout(timeout, order) {
this.timeouts.set(order, timeout)
}
/** @function */
setTrace() {
Error.captureStackTrace(this)
}
/** @param {Array<*>} args */
setArguments(args) {
this.args = args
}
setActor(actor) {
this.actor = actor || ''
}
/** @param {string} status */
setStatus(status) {
this.status = status
if (this.metaStep) {
this.metaStep.setStatus(status)
}
}
/** @return {string} */
humanize() {
return humanizeString(this.name)
}
/** @return {string} */
humanizeArgs() {
return this.args
.map(arg => {
if (!arg) {
return ''
}
if (typeof arg === 'string') {
return `"${arg}"`
}
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 arg.getMasked()
} else if (arg.toString && arg.toString() !== '[object Object]') {
return arg.toString()
} else if (typeof arg === 'object') {
const returnedArg = {}
for (const [key, value] of Object.entries(arg)) {
returnedArg[key] = value
if (value instanceof Secret) returnedArg[key] = value.getMasked()
}
return JSON.stringify(returnedArg)
}
return arg
})
.join(', ')
}
/** @return {string} */
line() {
const lines = this.stack.split('\n')
if (lines[STACK_LINE]) {
return lines[STACK_LINE].trim()
.replace(global.codecept_dir || '', '.')
.trim()
}
return ''
}
/** @return {string} */
toString() {
return ucfirst(`${this.prefix}${this.actor} ${this.humanize()} ${this.humanizeArgs()}${this.suffix}`).trim()
}
/** @return {string} */
toCliStyled() {
return `${this.prefix}${this.actor} ${color.italic(this.humanize())} ${color.yellow(this.humanizeArgs())}${this.suffix}`
}
/** @return {string} */
toCode() {
return `${this.prefix}${this.actor}.${this.name}(${this.humanizeArgs()})${this.suffix}`
}
isMetaStep() {
return this.constructor.name === 'MetaStep'
}
/** @return {boolean} */
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
}
}
module.exports = Step