-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
gobdd_test.go
414 lines (336 loc) · 10.4 KB
/
gobdd_test.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package gobdd
import (
"errors"
"fmt"
"regexp"
"strings"
"testing"
msgs "github.com/cucumber/messages/go/v24"
"github.com/go-bdd/assert"
"github.com/stretchr/testify/require"
)
func TestScenarios(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/example.feature"))
compiled := regexp.MustCompile(`I add (\d+) and (\d+)`)
suite.AddRegexStep(compiled, add)
compiled = regexp.MustCompile(`the result should equal (\d+)`)
suite.AddRegexStep(compiled, check)
suite.Run()
}
func TestRule(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/example_rule.feature"))
compiled := regexp.MustCompile(`I add (\d+) and (\d+)`)
suite.AddRegexStep(compiled, add)
compiled = regexp.MustCompile(`the result should equal (\d+)`)
suite.AddRegexStep(compiled, check)
suite.Run()
}
func TestAddStepWithRegexp(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/example.feature"))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`the result should equal (\d+)`, check)
suite.Run()
}
func TestDifferentFuncTypes(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/func_types.feature"))
suite.AddStep(`I add ([+-]?[0-9]*[.]?[0-9]+) and ([+-]?[0-9]*[.]?[0-9]+)`, addf)
suite.AddStep(`the result should equal ([+-]?[0-9]*[.]?[0-9]+)`, checkf)
suite.Run()
}
func TestScenarioOutline(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/outline.feature"))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`the result should equal (\d+)`, check)
suite.Run()
}
func TestParameterTypes(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/parameter-types.feature"))
suite.AddStep(`I add {int} and {int}`, add)
suite.AddStep(`the result should equal {int}`, check)
suite.AddStep(`I add floats {float} and {float}`, addf)
suite.AddStep(`the result should equal float {float}`, checkf)
suite.AddStep(`the result should equal text {text}`, checkt)
suite.AddStep(`I use word {word}`, func(t StepTest, ctx Context, word string) {
if word != "pizza" {
t.Fatal("it should be pizza")
}
})
suite.AddStep(`I use text {text}`, func(t StepTest, ctx Context, text string) {
ctx.Set("stringRes", text)
})
suite.AddStep(`I concat word {word} and text {text}`, concat)
suite.AddStep(`I format text {text} with int {int}`, func(t StepTest, ctx Context, format string, value int) {
ctx.Set("stringRes", fmt.Sprintf(format, value))
})
suite.Run()
}
func TestArguments(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/argument.feature"))
suite.AddStep(`the result should equal argument:`, checkt)
suite.AddStep(`I concat text {text} and argument:`, concat)
suite.Run()
}
func TestDatatable(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/datatable.feature"))
suite.AddStep(`I concat all the columns and row together using {text} to separate the columns`, concatTable)
suite.AddStep(`the result should equal argument:`, checkt)
suite.Run()
}
func TestScenarioOutlineExecutesAllTests(t *testing.T) {
c := 0
suite := NewSuite(t, WithFeaturesPath("features/outline.feature"))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`the result should equal (\d+)`, func(t StepTest, ctx Context, sum int) {
c++
check(t, ctx, sum)
})
suite.Run()
if err := assert.Equals(2, c); err != nil {
t.Errorf("expected to run %d times but %d got", 2, c)
}
}
func TestStepFromExample(t *testing.T) {
s := NewSuite(t)
st, expr := s.stepFromExample("I add <d1> and <d2>", &msgs.TableRow{
Cells: []*msgs.TableCell{
{Value: "1"},
{Value: "2"},
},
}, []string{"<d1>", "<d2>"})
if err := assert.NotNil(st); err != nil {
t.Error(err)
}
if err := assert.Equals("I add 1 and 2", st); err != nil {
t.Error(err)
}
if err := assert.Equals(`I add (\d+) and (\d+)`, expr); err != nil {
t.Error(err)
}
}
func TestBackground(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/background.feature"))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`I concat word {word} and text {text}`, concat)
suite.AddStep(`the result should equal text {text}`, checkt)
suite.AddStep(`the result should equal (\d+)`, check)
suite.Run()
}
func TestTags(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/tags.feature"), WithTags("@tag"))
suite.AddStep(`fail the test`, fail)
suite.AddStep(`the test should pass`, pass)
suite.Run()
}
func TestFilterFeatureWithTags(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/filter_tags_*.feature"), WithTags("@run-this"))
c := false
suite.AddStep(`the test should pass`, func(_ StepTest, _ Context) {
c = true
})
suite.AddStep(`fail the test`, fail)
suite.Run()
if err := assert.Equals(true, c); err != nil {
t.Error(err)
}
}
func TestWithAfterScenario(t *testing.T) {
c := false
suite := NewSuite(t, WithFeaturesPath("features/empty.feature"), WithAfterScenario(func(ctx Context) {
c = true
}))
suite.Run()
if err := assert.Equals(true, c); err != nil {
t.Error(err)
}
}
func TestWithBeforeScenario(t *testing.T) {
c := false
suite := NewSuite(t, WithFeaturesPath("features/empty.feature"), WithBeforeScenario(func(ctx Context) {
c = true
}))
suite.Run()
if err := assert.Equals(true, c); err != nil {
t.Error(err)
}
}
func TestWithAfterStep(t *testing.T) {
c := 0
suite := NewSuite(t, WithFeaturesPath("features/background.feature"), WithAfterStep(func(ctx Context) {
c++
// feature should be *msgs.Feature
feature, err := ctx.Get(FeatureKey{})
require.NoError(t, err)
if _, ok := feature.(*msgs.Feature); !ok {
t.Errorf("expected feature but got %T", feature)
}
// scenario should be *msgs.Scenario
scenario, err := ctx.Get(ScenarioKey{})
require.NoError(t, err)
if _, ok := scenario.(*msgs.Scenario); !ok {
t.Errorf("expected scenario but got %T", scenario)
}
}))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`the result should equal (\d+)`, check)
suite.AddStep(`I concat word {word} and text {text}`, concat)
suite.AddStep(`the result should equal text {text}`, checkt)
suite.Run()
if err := assert.Equals(6, c); err != nil {
t.Error(err)
}
}
func TestWithBeforeStep(t *testing.T) {
c := 0
suite := NewSuite(t, WithFeaturesPath("features/background.feature"), WithBeforeStep(func(ctx Context) {
c++
}))
suite.AddStep(`I add (\d+) and (\d+)`, add)
suite.AddStep(`the result should equal (\d+)`, check)
suite.AddStep(`I concat word {word} and text {text}`, concat)
suite.AddStep(`the result should equal text {text}`, checkt)
suite.Run()
if err := assert.Equals(6, c); err != nil {
t.Error(err)
}
}
func TestIgnoredTags(t *testing.T) {
suite := NewSuite(t, WithFeaturesPath("features/ignored_*tags.feature"), WithIgnoredTags("@ignore"))
suite.AddStep(`the test should pass`, pass)
suite.AddStep(`fail the test`, fail)
suite.Run()
}
func TestInvalidFunctionSignature(t *testing.T) {
testCases := map[string]struct {
f interface{}
}{
"nil": {},
"func without return value": {f: func(ctx Context) {}},
"func with invalid return value": {f: func(ctx Context) int { return 0 }},
"func without arguments": {f: func() error { return errors.New("") }},
"func with invalid first argument": {f: func(i int) error { return errors.New("") }},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
tester := &mockTester{}
suite := NewSuite(tester)
suite.AddStep("", testCase.f)
suite.Run()
err := assert.Equals(1, tester.fatalCalled)
if err != nil {
t.Fatal(err)
}
})
}
}
func TestFailureOutput(t *testing.T) {
testCases := []struct {
name string
f interface{}
expectedErrors []string
}{
{name: "passes", f: pass, expectedErrors: nil},
{name: "returns error", f: failure, expectedErrors: []string{"the step failed"}},
{name: "step panics", f: panics, expectedErrors: []string{"the step panicked"}},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
def := stepDef{f: testCase.f}
tester := &mockTester{}
def.run(NewContext(), tester, nil)
err := assert.Equals(testCase.expectedErrors, tester.errors)
if err != nil {
t.Fatal(err)
}
})
}
}
func addf(_ StepTest, ctx Context, var1, var2 float32) {
res := var1 + var2
ctx.Set("sumRes", res)
}
func add(_ StepTest, ctx Context, var1, var2 int) {
res := var1 + var2
ctx.Set("sumRes", res)
}
func concat(_ StepTest, ctx Context, var1, var2 string) {
ctx.Set("stringRes", var1+var2)
}
func concatTable(_ StepTest, ctx Context, separator string, table msgs.DataTable) {
rows := make([]string, 0, len(table.Rows))
for _, row := range table.Rows {
values := make([]string, 0, len(row.Cells))
for _, cell := range row.Cells {
values = append(values, cell.Value)
}
rows = append(rows, strings.Join(values, separator))
}
ctx.Set("stringRes", strings.Join(rows, "\n"))
}
func checkt(t StepTest, ctx Context, text string) {
received, err := ctx.GetString("stringRes")
if err != nil {
t.Error(err.Error())
return
}
if text != received {
t.Errorf("expected %s but %s received", text, received)
}
}
func checkf(t StepTest, ctx Context, sum float32) {
received, err := ctx.Get("sumRes")
if err != nil {
t.Error(err.Error())
return
}
if sum != received {
t.Errorf("expected %f but %f received", sum, received)
}
}
func check(t StepTest, ctx Context, sum int) {
received, err := ctx.Get("sumRes")
if err != nil {
t.Error(err)
return
}
if sum != received {
t.Errorf("expected %d but %d received", sum, received)
}
}
func fail(t StepTest, _ Context) {
t.Error("the step should never be executed")
}
func failure(t StepTest, _ Context) {
t.Error("the step failed")
}
func panics(_ StepTest, _ Context) {
panic(errors.New("the step panicked"))
}
func pass(_ StepTest, _ Context) {}
type mockTester struct {
fatalCalled int
errors []string
}
func (m *mockTester) Log(...interface{}) {
}
func (m *mockTester) Logf(string, ...interface{}) {
}
func (m *mockTester) Fatal(...interface{}) {
m.fatalCalled++
}
func (m *mockTester) Fatalf(string, ...interface{}) {
}
func (m *mockTester) Error(a ...interface{}) {
m.errors = append(m.errors, fmt.Sprintf("%s", a...))
}
func (m *mockTester) Errorf(format string, a ...interface{}) {
m.errors = append(m.errors, fmt.Sprintf(format, a...))
}
func (m *mockTester) Parallel() {
}
func (m *mockTester) Fail() {
}
func (m *mockTester) FailNow() {
}
func (m *mockTester) Run(_ string, _ func(t *testing.T)) bool {
return true
}