-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgocumber.go
201 lines (157 loc) · 4.64 KB
/
gocumber.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
package gocumber
import (
"fmt"
"io/ioutil"
"regexp"
"github.com/muhqu/go-gherkin"
"github.com/muhqu/go-gherkin/nodes"
)
type Definition func([]string, StepNode)
type Definitions map[*regexp.Regexp]Definition
type StepNode nodes.StepNode
type Table nodes.TableNode
type matchedDefinition struct {
step nodes.StepNode
matches []string
definition Definition
}
func (m matchedDefinition) execute() { m.definition(m.matches, m.step) }
func (m matchedDefinition) pending() bool { return m.definition == nil }
type testingFramework interface {
Error(args ...interface{})
Log(args ...interface{})
}
func ColumnMap(table Table) map[string]string {
result := make(map[string]string)
for _, row := range table.Rows() {
result[row[0]] = row[1]
}
return result
}
func RowMaps(table Table) []map[string]string {
result := make([]map[string]string, 0, len(table.Rows())-1)
header := table.Rows()[0]
for _, row := range table.Rows()[1:] {
subRow := make(map[string]string)
for i, key := range header {
subRow[key] = row[i]
}
result = append(result, subRow)
}
return result
}
func (defs Definitions) Step(text string, def Definition) {
defs[regexp.MustCompile("^"+text+"$")] = def
}
func (defs Definitions) Exec(text string) (found bool) {
matched := defs.find(nodes.NewMutableStepNode("", text))
if matched == nil {
return false
}
matched.execute()
return true
}
func (defs Definitions) find(step nodes.StepNode) *matchedDefinition {
var matches []string
text := step.Text()
for re, definition := range defs {
matches = re.FindStringSubmatch(text)
if matches != nil {
return &matchedDefinition{
step: step,
matches: matches,
definition: definition,
}
}
}
return nil
}
func (defs Definitions) findAll(steps []nodes.StepNode) (matched []matchedDefinition, missing map[string]nodes.StepNode) {
missing = make(map[string]nodes.StepNode)
for _, step := range steps {
found := defs.find(step)
if found == nil {
missing[step.Text()] = step
} else {
matched = append(matched, *found)
}
}
return matched, missing
}
func (defs *Definitions) Given(text string, def Definition) { defs.Step(text, def) }
func (defs *Definitions) When(text string, def Definition) { defs.Step(text, def) }
func (defs *Definitions) Then(text string, def Definition) { defs.Step(text, def) }
func (defs *Definitions) Run(t testingFramework, file string) {
if buffer, err := ioutil.ReadFile(file); err != nil {
t.Error(err)
} else if feature, err := gherkin.ParseGherkinFeature(string(buffer)); err != nil {
t.Error(err)
} else {
ScenarioLoop:
for _, scenario := range feature.Scenarios() {
var steps []nodes.StepNode
if background := feature.Background(); background != nil {
steps = append(steps, background.Steps()...)
}
switch scenario := scenario.(type) {
case nodes.OutlineNode:
outlineSteps(scenario, func(step nodes.StepNode) {
steps = append(steps, step)
})
default:
steps = append(steps, scenario.Steps()...)
}
matched, missing := defs.findAll(steps)
if len(missing) > 0 {
for _, step := range missing {
t.Error(fmt.Errorf("Undefined step:\n%s %s", step.StepType(), step.Text()))
}
continue ScenarioLoop
}
t.Log("Scenario: " + scenario.Title())
for _, definition := range matched {
if definition.pending() {
t.Error(fmt.Errorf("Pending step:\n%s %s", definition.step.StepType(), definition.step.Text()))
continue ScenarioLoop
}
definition.execute()
}
}
}
}
func outlineSteps(outline nodes.OutlineNode, callback func(nodes.StepNode)) {
matcher := regexp.MustCompile(`<[^>]+>`)
replacements := make(map[string]string)
replace := func(text string) string {
return matcher.ReplaceAllStringFunc(text, func(key string) string {
return replacements[key]
})
}
header := outline.Examples().Table().Rows()[0]
for _, row := range outline.Examples().Table().Rows()[1:] {
for i, key := range header {
replacements["<"+key+">"] = row[i]
}
for _, original := range outline.Steps() {
step := nodes.NewMutableStepNode(original.StepType(), replace(original.Text()))
if original.Table() != nil {
table := nodes.NewMutableTableNode()
for _, row := range original.Table().Rows() {
replaced := make([]string, len(row))
for i, cell := range row {
replaced[i] = replace(cell)
}
table.AddRow(replaced)
}
step.SetTable(table)
} else if original.PyString() != nil {
pyString := nodes.NewMutablePyStringNode()
for _, line := range original.PyString().Lines() {
pyString.AddLine(replace(line))
}
step.SetPyString(pyString)
}
callback(step)
}
}
}