-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickles.go
240 lines (215 loc) · 8.66 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
package gherkin
import (
"crypto/sha1"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/acst/gherkin/messages"
"strings"
"unicode/utf8"
)
func Pickles(gherkinDocument messages.GherkinDocument, uri string, source 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, source)
return pickles
}
func compileFeature(pickles []*messages.Pickle, feature messages.GherkinDocument_Feature, uri string, language string, source string) []*messages.Pickle {
backgroundSteps := make([]*messages.Pickle_PickleStep, 0)
featureTags := feature.Tags
for _, child := range feature.Children {
switch t := child.Value.(type) {
case *messages.GherkinDocument_Feature_FeatureChild_Background:
backgroundSteps = append(backgroundSteps, pickleSteps(t.Background.Steps)...)
case *messages.GherkinDocument_Feature_FeatureChild_Rule_:
pickles = compileRule(pickles, child.GetRule(), featureTags, backgroundSteps, uri, language, source)
case *messages.GherkinDocument_Feature_FeatureChild_Scenario:
scenario := t.Scenario
if len(scenario.GetExamples()) == 0 {
pickles = compileScenario(pickles, backgroundSteps, scenario, featureTags, uri, language, source)
} else {
pickles = compileScenarioOutline(pickles, scenario, featureTags, backgroundSteps, uri, language, source)
}
default:
panic(fmt.Sprintf("unexpected %T feature child", child))
}
}
return pickles
}
func compileRule(pickles []*messages.Pickle, rule *messages.GherkinDocument_Feature_FeatureChild_Rule, tags []*messages.GherkinDocument_Feature_Tag, steps []*messages.Pickle_PickleStep, uri string, language string, source string) []*messages.Pickle {
backgroundSteps := make([]*messages.Pickle_PickleStep, 0)
backgroundSteps = append(backgroundSteps, steps...)
for _, child := range rule.Children {
switch t := child.Value.(type) {
case *messages.GherkinDocument_Feature_FeatureChild_RuleChild_Background:
backgroundSteps = append(backgroundSteps, pickleSteps(t.Background.Steps)...)
case *messages.GherkinDocument_Feature_FeatureChild_RuleChild_Scenario:
scenario := t.Scenario
if len(scenario.GetExamples()) == 0 {
pickles = compileScenario(pickles, backgroundSteps, scenario, tags, uri, language, source)
} else {
pickles = compileScenarioOutline(pickles, scenario, tags, backgroundSteps, uri, language, source)
}
default:
panic(fmt.Sprintf("unexpected %T feature child", child))
}
}
return pickles
}
func compileScenarioOutline(pickles []*messages.Pickle, scenario *messages.GherkinDocument_Feature_Scenario, featureTags []*messages.GherkinDocument_Feature_Tag, backgroundSteps []*messages.Pickle_PickleStep, uri string, language string, source string) []*messages.Pickle {
for _, examples := range scenario.Examples {
if examples.TableHeader == nil {
continue
}
variableCells := examples.TableHeader.Cells
for _, values := range examples.TableBody {
valueCells := values.Cells
tags := pickleTags(append(featureTags, append(scenario.Tags, examples.Tags...)...))
pickleSteps := make([]*messages.Pickle_PickleStep, 0)
// translate pickleSteps based on values
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, valueCells)
pickleStep.Locations = append(pickleStep.Locations, values.Location)
pickleSteps = append(pickleSteps, pickleStep)
}
// translate pickle name
name := scenario.Name
for i, key := range variableCells {
name = strings.Replace(name, "<"+key.Value+">", valueCells[i].Value, -1)
}
if len(pickleSteps) > 0 {
pickleSteps = append(backgroundSteps, pickleSteps...)
}
locations := make([]*messages.Location, 0)
locations = append(locations, scenario.Location)
locations = append(locations, values.Location)
pickles = append(pickles, &messages.Pickle{
Id: makeId(source, locations),
Uri: uri,
Steps: pickleSteps,
Tags: tags,
Name: name,
Language: language,
Locations: locations,
})
}
}
return pickles
}
func makeId(source string, locations []*messages.Location) string {
hash := sha1.New()
hash.Write([]byte(source))
for _, location := range locations {
err := binary.Write(hash, binary.LittleEndian, location.Line)
if err != nil {
panic(err)
}
err = binary.Write(hash, binary.LittleEndian, location.Column)
if err != nil {
panic(err)
}
}
bs := hash.Sum(nil)
return hex.EncodeToString(bs)
}
func compileScenario(pickles []*messages.Pickle, backgroundSteps []*messages.Pickle_PickleStep, scenario *messages.GherkinDocument_Feature_Scenario, featureTags []*messages.GherkinDocument_Feature_Tag, uri string, language string, source string) []*messages.Pickle {
steps := make([]*messages.Pickle_PickleStep, 0)
if len(scenario.Steps) > 0 {
steps = append(backgroundSteps, pickleSteps(scenario.Steps)...)
}
tags := pickleTags(append(featureTags, scenario.Tags...))
locations := make([]*messages.Location, 0)
locations = append(locations, scenario.Location)
pickles = append(pickles, &messages.Pickle{
Id: makeId(source, locations),
Uri: uri,
Steps: steps,
Tags: tags,
Name: scenario.Name,
Language: language,
Locations: locations,
})
return pickles
}
func pickleDataTable(table *messages.GherkinDocument_Feature_Step_DataTable, variableCells, valueCells []*messages.GherkinDocument_Feature_TableRow_TableCell) *messages.PickleStepArgument_PickleTable {
pickleTableRows := make([]*messages.PickleStepArgument_PickleTable_PickleTableRow, len(table.Rows))
for i, row := range table.Rows {
pickleTableCells := make([]*messages.PickleStepArgument_PickleTable_PickleTableRow_PickleTableCell, len(row.Cells))
for j, cell := range row.Cells {
pickleTableCells[j] = &messages.PickleStepArgument_PickleTable_PickleTableRow_PickleTableCell{
Location: cell.Location,
Value: interpolate(cell.Value, variableCells, valueCells),
}
}
pickleTableRows[i] = &messages.PickleStepArgument_PickleTable_PickleTableRow{Cells: pickleTableCells}
}
return &messages.PickleStepArgument_PickleTable{Rows: pickleTableRows}
}
func pickleDocString(docString *messages.GherkinDocument_Feature_Step_DocString, variableCells, valueCells []*messages.GherkinDocument_Feature_TableRow_TableCell) *messages.PickleStepArgument_PickleDocString {
return &messages.PickleStepArgument_PickleDocString{
Location: docString.Location,
ContentType: interpolate(docString.ContentType, variableCells, valueCells),
Content: interpolate(docString.Content, variableCells, valueCells),
}
}
func pickleTags(tags []*messages.GherkinDocument_Feature_Tag) []*messages.Pickle_PickleTag {
ptags := make([]*messages.Pickle_PickleTag, len(tags))
for i, tag := range tags {
ptags[i] = &messages.Pickle_PickleTag{
Location: tag.Location,
Name: tag.Name,
}
}
return ptags
}
func pickleSteps(steps []*messages.GherkinDocument_Feature_Step) []*messages.Pickle_PickleStep {
pickleSteps := make([]*messages.Pickle_PickleStep, len(steps))
for i, step := range steps {
pickleStep := pickleStep(step, nil, nil)
pickleSteps[i] = pickleStep
}
return pickleSteps
}
func pickleStep(step *messages.GherkinDocument_Feature_Step, variableCells, valueCells []*messages.GherkinDocument_Feature_TableRow_TableCell) *messages.Pickle_PickleStep {
loc := &messages.Location{
Line: uint32(step.Location.Line),
Column: step.Location.Column + uint32(utf8.RuneCountInString(step.Keyword)),
}
locations := make([]*messages.Location, 0)
locations = append(locations, loc)
pickleStep := &messages.Pickle_PickleStep{
Text: interpolate(step.Text, variableCells, valueCells),
Locations: locations,
}
if step.GetDataTable() != nil {
pickleStep.Argument = &messages.PickleStepArgument{
Message: &messages.PickleStepArgument_DataTable{
DataTable: pickleDataTable(step.GetDataTable(), variableCells, valueCells),
},
}
}
if step.GetDocString() != nil {
pickleStep.Argument = &messages.PickleStepArgument{
Message: &messages.PickleStepArgument_DocString{
DocString: pickleDocString(step.GetDocString(), variableCells, valueCells),
},
}
}
return pickleStep
}
func interpolate(s string, variableCells, valueCells []*messages.GherkinDocument_Feature_TableRow_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
}