forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_step.go
104 lines (81 loc) · 2.43 KB
/
run_step.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
package gocuke
import (
"reflect"
messages "github.com/cucumber/messages/go/v21"
)
func (r *scenarioRunner) runHook(def *stepDef) {
r.t.Helper()
typ := def.theFunc.Type()
expectedIn := len(def.specialArgs)
if expectedIn != typ.NumIn() {
r.t.Fatalf("expected %d in parameter(s) for function %+v, got %d", expectedIn, def.funcLoc, typ.NumIn())
}
values := make([]reflect.Value, expectedIn)
for i, arg := range def.specialArgs {
values[i] = reflect.ValueOf(arg.getValue(r))
}
def.theFunc.Call(values)
}
func (r *scenarioRunner) runStep(step *messages.PickleStep, def *stepDef) {
r.t.Helper()
r.step = step
for _, hook := range r.beforeStepHooks {
r.runHook(hook)
}
for _, hook := range r.afterStepHooks {
defer r.runHook(hook)
}
matches := def.regex.FindSubmatch([]byte(step.Text))
if len(matches) == 0 {
r.t.Fatalf("internal error: no matches found when matching %s against %s", def.regex.String(), step.Text)
}
matches = matches[1:]
numSpecialArgs := len(def.specialArgs)
expectedIn := len(matches) + numSpecialArgs
typ := def.theFunc.Type()
hasPickleArg := step.Argument != nil
if hasPickleArg {
expectedIn += 1
}
if expectedIn != typ.NumIn() {
r.t.Fatalf("expected %d in parameter(s) for function %+v, got %d", expectedIn, def.funcLoc, typ.NumIn())
}
values := make([]reflect.Value, expectedIn)
for i, arg := range def.specialArgs {
values[i] = reflect.ValueOf(arg.getValue(r))
}
for i, match := range matches {
values[i+numSpecialArgs] = convertParamValue(r.t, string(match), typ.In(i+numSpecialArgs), def.funcLoc)
}
// pickleArg goes last
if hasPickleArg {
i := expectedIn - 1
pickleArgType := typ.In(i)
// only one of DataTable or DocString is valid
if pickleArgType == dataTableType {
if step.Argument.DataTable == nil {
r.t.Fatalf("expected non-nil DataTable")
}
dataTable := DataTable{
t: r.t,
table: step.Argument.DataTable,
}
values[i] = reflect.ValueOf(dataTable)
} else if pickleArgType == docStringType {
if step.Argument.DocString == nil {
r.t.Fatalf("expected non-nil DocString")
}
docString := DocString{
MediaType: step.Argument.DocString.MediaType,
Content: step.Argument.DocString.Content,
}
values[i] = reflect.ValueOf(docString)
} else {
r.t.Fatalf("unexpected parameter type %v in function %s", pickleArgType, def.funcLoc)
}
}
if r.verbose {
r.t.Logf("Step: %s", step.Text)
}
def.theFunc.Call(values)
}