forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickles.go
266 lines (238 loc) · 7.98 KB
/
pickles.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
package gherkin
import (
"github.com/cucumber/messages/go/v22"
"strings"
)
func Pickles(gherkinDocument messages.GherkinDocument, uri string, newId func() string) []*messages.Pickle {
pickles := make([]*messages.Pickle, 0)
if gherkinDocument.Feature == nil {
return pickles
}
language := gherkinDocument.Feature.Language
pickles = compileFeature(pickles, *gherkinDocument.Feature, uri, language, newId)
return pickles
}
func compileFeature(pickles []*messages.Pickle, feature messages.Feature, uri string, language string, newId func() string) []*messages.Pickle {
featureBackgroundSteps := make([]*messages.Step, 0)
featureTags := feature.Tags
for _, child := range feature.Children {
if child.Background != nil {
featureBackgroundSteps = append(featureBackgroundSteps, child.Background.Steps...)
}
if child.Rule != nil {
pickles = compileRule(pickles, child.Rule, featureTags, featureBackgroundSteps, uri, language, newId)
}
if child.Scenario != nil {
if len(child.Scenario.Examples) == 0 {
pickles = compileScenario(pickles, featureBackgroundSteps, child.Scenario, featureTags, uri, language, newId)
} else {
pickles = compileScenarioOutline(pickles, child.Scenario, featureTags, featureBackgroundSteps, uri, language, newId)
}
}
}
return pickles
}
func compileRule(
pickles []*messages.Pickle,
rule *messages.Rule,
featureTags []*messages.Tag,
featureBackgroundSteps []*messages.Step,
uri string,
language string,
newId func() string,
) []*messages.Pickle {
ruleBackgroundSteps := make([]*messages.Step, 0)
ruleBackgroundSteps = append(ruleBackgroundSteps, featureBackgroundSteps...)
tags := append(featureTags, rule.Tags...)
for _, child := range rule.Children {
if child.Background != nil {
ruleBackgroundSteps = append(ruleBackgroundSteps, child.Background.Steps...)
}
if child.Scenario != nil {
if len(child.Scenario.Examples) == 0 {
pickles = compileScenario(pickles, ruleBackgroundSteps, child.Scenario, tags, uri, language, newId)
} else {
pickles = compileScenarioOutline(pickles, child.Scenario, tags, ruleBackgroundSteps, uri, language, newId)
}
}
}
return pickles
}
func compileScenarioOutline(
pickles []*messages.Pickle,
scenario *messages.Scenario,
inheritedTags []*messages.Tag,
backgroundSteps []*messages.Step,
uri string,
language string,
newId func() string,
) []*messages.Pickle {
for _, examples := range scenario.Examples {
if examples.TableHeader == nil {
continue
}
variableCells := examples.TableHeader.Cells
for _, valuesRow := range examples.TableBody {
valueCells := valuesRow.Cells
computedPickleSteps := make([]*messages.PickleStep, 0)
pickleBackgroundSteps := make([]*messages.PickleStep, 0)
if len(scenario.Steps) > 0 {
pickleBackgroundSteps = pickleSteps(backgroundSteps, newId)
}
// translate computedPickleSteps based on valuesRow
previous := messages.PickleStepType_UNKNOWN
for _, step := range scenario.Steps {
text := step.Text
for i, variableCell := range variableCells {
text = strings.Replace(text, "<"+variableCell.Value+">", valueCells[i].Value, -1)
}
pickleStep := pickleStep(step, variableCells, valuesRow, newId, previous)
previous = pickleStep.Type
computedPickleSteps = append(computedPickleSteps, pickleStep)
}
// translate pickle name
name := scenario.Name
for i, key := range variableCells {
name = strings.Replace(name, "<"+key.Value+">", valueCells[i].Value, -1)
}
if len(computedPickleSteps) > 0 {
computedPickleSteps = append(pickleBackgroundSteps, computedPickleSteps...)
}
id := newId()
tags := pickleTags(append(inheritedTags, append(scenario.Tags, examples.Tags...)...))
pickles = append(pickles, &messages.Pickle{
Id: id,
Uri: uri,
Steps: computedPickleSteps,
Tags: tags,
Name: name,
Language: language,
AstNodeIds: []string{scenario.Id, valuesRow.Id},
})
}
}
return pickles
}
func compileScenario(
pickles []*messages.Pickle,
backgroundSteps []*messages.Step,
scenario *messages.Scenario,
inheritedTags []*messages.Tag,
uri string,
language string,
newId func() string,
) []*messages.Pickle {
steps := make([]*messages.PickleStep, 0)
if len(scenario.Steps) > 0 {
pickleBackgroundSteps := pickleSteps(backgroundSteps, newId)
steps = append(pickleBackgroundSteps, pickleSteps(scenario.Steps, newId)...)
}
tags := pickleTags(append(inheritedTags, scenario.Tags...))
id := newId()
pickles = append(pickles, &messages.Pickle{
Id: id,
Uri: uri,
Steps: steps,
Tags: tags,
Name: scenario.Name,
Language: language,
AstNodeIds: []string{scenario.Id},
})
return pickles
}
func pickleDataTable(table *messages.DataTable, variableCells []*messages.TableCell, valueCells []*messages.TableCell) *messages.PickleTable {
pickleTableRows := make([]*messages.PickleTableRow, len(table.Rows))
for i, row := range table.Rows {
pickleTableCells := make([]*messages.PickleTableCell, len(row.Cells))
for j, cell := range row.Cells {
pickleTableCells[j] = &messages.PickleTableCell{
Value: interpolate(cell.Value, variableCells, valueCells),
}
}
pickleTableRows[i] = &messages.PickleTableRow{Cells: pickleTableCells}
}
return &messages.PickleTable{Rows: pickleTableRows}
}
func pickleDocString(docString *messages.DocString, variableCells []*messages.TableCell, valueCells []*messages.TableCell) *messages.PickleDocString {
return &messages.PickleDocString{
MediaType: interpolate(docString.MediaType, variableCells, valueCells),
Content: interpolate(docString.Content, variableCells, valueCells),
}
}
func pickleTags(tags []*messages.Tag) []*messages.PickleTag {
ptags := make([]*messages.PickleTag, len(tags))
for i, tag := range tags {
ptags[i] = &messages.PickleTag{
Name: tag.Name,
AstNodeId: tag.Id,
}
}
return ptags
}
func pickleSteps(steps []*messages.Step, newId func() string) []*messages.PickleStep {
pickleSteps := make([]*messages.PickleStep, len(steps))
previous := messages.PickleStepType_UNKNOWN
for i, step := range steps {
pickleStep := pickleStep(step, nil, nil, newId, previous)
previous = pickleStep.Type
pickleSteps[i] = pickleStep
}
return pickleSteps
}
func pickleStep(
step *messages.Step,
variableCells []*messages.TableCell,
valuesRow *messages.TableRow,
newId func() string,
previous messages.PickleStepType,
) *messages.PickleStep {
var valueCells []*messages.TableCell
if valuesRow != nil {
valueCells = valuesRow.Cells
}
pickleStep := &messages.PickleStep{
Id: newId(),
Text: interpolate(step.Text, variableCells, valueCells),
Type: mapType(step.KeywordType, previous),
AstNodeIds: []string{step.Id},
}
if valuesRow != nil {
pickleStep.AstNodeIds = append(pickleStep.AstNodeIds, valuesRow.Id)
}
if step.DataTable != nil {
pickleStep.Argument = &messages.PickleStepArgument{
DataTable: pickleDataTable(step.DataTable, variableCells, valueCells),
}
}
if step.DocString != nil {
pickleStep.Argument = &messages.PickleStepArgument{
DocString: pickleDocString(step.DocString, variableCells, valueCells),
}
}
return pickleStep
}
func mapType(keywordType messages.StepKeywordType, previous messages.PickleStepType) messages.PickleStepType {
switch keywordType {
case messages.StepKeywordType_UNKNOWN:
return messages.PickleStepType_UNKNOWN
case messages.StepKeywordType_CONTEXT:
return messages.PickleStepType_CONTEXT
case messages.StepKeywordType_ACTION:
return messages.PickleStepType_ACTION
case messages.StepKeywordType_OUTCOME:
return messages.PickleStepType_OUTCOME
case messages.StepKeywordType_CONJUNCTION:
return previous
default:
panic("Bad enum value for StepKeywordType")
}
}
func interpolate(s string, variableCells []*messages.TableCell, valueCells []*messages.TableCell) string {
if variableCells == nil || valueCells == nil {
return s
}
for i, variableCell := range variableCells {
s = strings.Replace(s, "<"+variableCell.Value+">", valueCells[i].Value, -1)
}
return s
}