-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.go
66 lines (52 loc) · 1.63 KB
/
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
package expandvars
import (
"context"
"github.com/cucumber/godog"
)
// StepExpander expands variables in cucumber steps.
type StepExpander struct {
expanders []interface{}
expand Expander
}
// RegisterExpander registers only the expander to the test suite to let it work.
// There will be no registration of step definition in this method.
func (m *StepExpander) RegisterExpander(s *godog.ScenarioContext) {
s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
m.expand = chainExpanders(m.expanders...)
return nil, nil
})
s.StepContext().Before(func(_ context.Context, st *godog.Step) (context.Context, error) {
expandStep(st, m.expand)
return nil, nil
})
}
// RegisterContext register everything to the test suite.
func (m *StepExpander) RegisterContext(s *godog.ScenarioContext) {
m.RegisterExpander(s)
}
// NewStepExpander initiates a new variable expanders for cucumber steps.
func NewStepExpander(expanders ...interface{}) *StepExpander {
return &StepExpander{
expanders: expanders,
}
}
// ExpandStep expands variables in the step definition using an expander.
func ExpandStep(st *godog.Step, expanders ...interface{}) {
expandStep(st, chainExpanders(expanders...))
}
func expandStep(st *godog.Step, expand Expander) {
st.Text = doExpand(expand, st.Text)
if st.Argument == nil {
return
}
if st.Argument.DocString != nil {
st.Argument.DocString.Content = doExpand(expand, st.Argument.DocString.Content)
}
if st.Argument.DataTable != nil {
for _, row := range st.Argument.DataTable.Rows {
for _, cell := range row.Cells {
cell.Value = doExpand(expand, cell.Value)
}
}
}
}