forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
292 lines (240 loc) · 6.65 KB
/
suite.go
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package godog
import (
"fmt"
"reflect"
"strings"
"github.com/cucumber/messages-go/v10"
"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/models"
"github.com/cucumber/godog/internal/storage"
"github.com/cucumber/godog/internal/utils"
)
var errorInterface = reflect.TypeOf((*error)(nil)).Elem()
// ErrUndefined is returned in case if step definition was not found
var ErrUndefined = fmt.Errorf("step is undefined")
// ErrPending should be returned by step definition if
// step implementation is pending
var ErrPending = fmt.Errorf("step implementation is pending")
type suite struct {
steps []*models.StepDefinition
fmt Formatter
storage *storage.Storage
failed bool
randomSeed int64
stopOnFailure bool
strict bool
// suite event handlers
beforeScenarioHandlers []func(*Scenario)
beforeStepHandlers []func(*Step)
afterStepHandlers []func(*Step, error)
afterScenarioHandlers []func(*Scenario, error)
}
func (s *suite) matchStep(step *messages.Pickle_PickleStep) *models.StepDefinition {
def := s.matchStepText(step.Text)
if def != nil && step.Argument != nil {
def.Args = append(def.Args, step.Argument)
}
return def
}
func (s *suite) runStep(pickle *messages.Pickle, step *messages.Pickle_PickleStep, prevStepErr error) (err error) {
// run before step handlers
for _, f := range s.beforeStepHandlers {
f(step)
}
match := s.matchStep(step)
s.storage.MustInsertStepDefintionMatch(step.AstNodeIds[0], match)
s.fmt.Defined(pickle, step, match.GetInternalStepDefinition())
// user multistep definitions may panic
defer func() {
if e := recover(); e != nil {
err = &traceError{
msg: fmt.Sprintf("%v", e),
stack: callStack(),
}
}
if prevStepErr != nil {
return
}
if err == ErrUndefined {
return
}
sr := models.NewStepResult(pickle.Id, step.Id, match)
switch err {
case nil:
sr.Status = models.Passed
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Passed(pickle, step, match.GetInternalStepDefinition())
case ErrPending:
sr.Status = models.Pending
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Pending(pickle, step, match.GetInternalStepDefinition())
default:
sr.Status = models.Failed
sr.Err = err
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Failed(pickle, step, match.GetInternalStepDefinition(), err)
}
// run after step handlers
for _, f := range s.afterStepHandlers {
f(step, err)
}
}()
if undef, err := s.maybeUndefined(step.Text, step.Argument); err != nil {
return err
} else if len(undef) > 0 {
if match != nil {
match = &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Expr: match.Expr,
Handler: match.Handler,
},
Args: match.Args,
HandlerValue: match.HandlerValue,
Nested: match.Nested,
Undefined: undef,
}
}
sr := models.NewStepResult(pickle.Id, step.Id, match)
sr.Status = models.Undefined
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Undefined(pickle, step, match.GetInternalStepDefinition())
return ErrUndefined
}
if prevStepErr != nil {
sr := models.NewStepResult(pickle.Id, step.Id, match)
sr.Status = models.Skipped
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Skipped(pickle, step, match.GetInternalStepDefinition())
return nil
}
err = s.maybeSubSteps(match.Run())
return
}
func (s *suite) maybeUndefined(text string, arg interface{}) ([]string, error) {
step := s.matchStepText(text)
if nil == step {
return []string{text}, nil
}
var undefined []string
if !step.Nested {
return undefined, nil
}
if arg != nil {
step.Args = append(step.Args, arg)
}
for _, next := range step.Run().(Steps) {
lines := strings.Split(next, "\n")
// @TODO: we cannot currently parse table or content body from nested steps
if len(lines) > 1 {
return undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
if len(lines[0]) > 0 && lines[0][len(lines[0])-1] == ':' {
return undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
undef, err := s.maybeUndefined(next, nil)
if err != nil {
return undefined, err
}
undefined = append(undefined, undef...)
}
return undefined, nil
}
func (s *suite) maybeSubSteps(result interface{}) error {
if nil == result {
return nil
}
if err, ok := result.(error); ok {
return err
}
steps, ok := result.(Steps)
if !ok {
return fmt.Errorf("unexpected error, should have been []string: %T - %+v", result, result)
}
for _, text := range steps {
if def := s.matchStepText(text); def == nil {
return ErrUndefined
} else if err := s.maybeSubSteps(def.Run()); err != nil {
return fmt.Errorf("%s: %+v", text, err)
}
}
return nil
}
func (s *suite) matchStepText(text string) *models.StepDefinition {
for _, h := range s.steps {
if m := h.Expr.FindStringSubmatch(text); len(m) > 0 {
var args []interface{}
for _, m := range m[1:] {
args = append(args, m)
}
// since we need to assign arguments
// better to copy the step definition
return &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Expr: h.Expr,
Handler: h.Handler,
},
Args: args,
HandlerValue: h.HandlerValue,
Nested: h.Nested,
}
}
}
return nil
}
func (s *suite) runSteps(pickle *messages.Pickle, steps []*messages.Pickle_PickleStep) (err error) {
for _, step := range steps {
stepErr := s.runStep(pickle, step, err)
switch stepErr {
case ErrUndefined:
// do not overwrite failed error
if err == ErrUndefined || err == nil {
err = stepErr
}
case ErrPending:
err = stepErr
case nil:
default:
err = stepErr
}
}
return
}
func (s *suite) shouldFail(err error) bool {
if err == nil {
return false
}
if err == ErrUndefined || err == ErrPending {
return s.strict
}
return true
}
func isEmptyFeature(pickles []*messages.Pickle) bool {
for _, pickle := range pickles {
if len(pickle.Steps) > 0 {
return false
}
}
return true
}
func (s *suite) runPickle(pickle *messages.Pickle) (err error) {
if len(pickle.Steps) == 0 {
pr := models.PickleResult{PickleID: pickle.Id, StartedAt: utils.TimeNowFunc()}
s.storage.MustInsertPickleResult(pr)
s.fmt.Pickle(pickle)
return ErrUndefined
}
// run before scenario handlers
for _, f := range s.beforeScenarioHandlers {
f(pickle)
}
pr := models.PickleResult{PickleID: pickle.Id, StartedAt: utils.TimeNowFunc()}
s.storage.MustInsertPickleResult(pr)
s.fmt.Pickle(pickle)
// scenario
err = s.runSteps(pickle, pickle.Steps)
// run after scenario handlers
for _, f := range s.afterScenarioHandlers {
f(pickle, err)
}
return
}