forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_scenario.go
123 lines (104 loc) · 2.2 KB
/
run_scenario.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
package gocuke
import (
"reflect"
"testing"
messages "github.com/cucumber/messages/go/v22"
"pgregory.net/rapid"
"github.com/regen-network/gocuke/internal/tag"
)
func (r *docRunner) runScenario(t *testing.T, pickle *messages.Pickle, verbose bool) {
t.Helper()
tags := tag.NewTagsFromPickleTags(pickle.Tags)
if r.tagExpr != nil && !tags.Match(r.tagExpr) {
t.SkipNow()
}
if testing.Short() {
if r.shortTagExpr != nil && !tags.Match(r.shortTagExpr) {
t.SkipNow()
}
}
if globalTagExpr != nil {
if !tags.Match(globalTagExpr) {
t.SkipNow()
}
}
stepDefs := make([]*stepDef, len(pickle.Steps))
for i, step := range pickle.Steps {
stepDefs[i] = r.findStep(t, step)
}
if t.Failed() {
return
}
useRapid := r.suiteUsesRapid
if !useRapid {
for _, def := range stepDefs {
if def.usesRapid() {
useRapid = true
break
}
}
}
if useRapid {
rapid.Check(t, func(rt *rapid.T) {
t := &rapidT{rt}
(&scenarioRunner{
docRunner: r,
t: t,
pickle: pickle,
stepDefs: stepDefs,
verbose: verbose,
}).runTestCase()
})
} else {
(&scenarioRunner{
docRunner: r,
t: t,
pickle: pickle,
stepDefs: stepDefs,
verbose: verbose,
}).runTestCase()
}
}
type scenarioRunner struct {
*docRunner
t TestingT
s interface{}
pickle *messages.Pickle
stepDefs []*stepDef
step *messages.PickleStep
verbose bool
}
func (r *scenarioRunner) runTestCase() {
r.t.Helper()
var val reflect.Value
needPtr := r.suiteType.Kind() == reflect.Ptr
if needPtr {
val = reflect.New(r.suiteType.Elem())
} else {
val = reflect.New(r.suiteType)
}
for _, injector := range r.suiteInjectors {
val.Elem().FieldByName(injector.field.Name).Set(reflect.ValueOf(injector.getValue(r)))
}
if needPtr {
r.s = val.Interface()
} else {
r.s = val.Elem().Interface()
}
for _, hook := range r.beforeHooks {
r.runHook(hook)
}
for _, hook := range r.afterHooks {
switch t := r.t.(type) {
case *rapidT:
defer r.runHook(hook)
case interface{ Cleanup(func()) }:
t.Cleanup(func() { r.runHook(hook) })
default:
defer r.runHook(hook)
}
}
for i, step := range r.pickle.Steps {
r.runStep(step, r.stepDefs[i])
}
}