forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt_progress_test.go
392 lines (311 loc) · 8.95 KB
/
fmt_progress_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
package godog
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/spikerlabs/godog/colors"
"github.com/spikerlabs/godog/gherkin"
)
func TestProgressFormatterOutput(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(sampleGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{
Path: "any.feature",
Feature: feat,
Content: []byte(sampleGherkinFeature),
}},
initializer: func(s *Suite) {
s.Step(`^passing$`, func() error { return nil })
s.Step(`^failing$`, func() error { return fmt.Errorf("errored") })
s.Step(`^pending$`, func() error { return ErrPending })
},
}
expected := `
...F-.P-.UU.....F..P..U 23
--- Failed steps:
Scenario: failing scenario # any.feature:10
When failing # any.feature:11
Error: errored
Scenario Outline: outline # any.feature:22
When failing # any.feature:24
Error: errored
8 scenarios (2 passed, 2 failed, 2 pending, 2 undefined)
23 steps (14 passed, 2 failed, 2 pending, 3 undefined, 2 skipped)
0s
You can implement step definitions for undefined steps with these snippets:
func undefined() error {
return godog.ErrPending
}
func nextUndefined() error {
return godog.ErrPending
}
func FeatureContext(s *godog.Suite) {
s.Step(` + "`^undefined$`" + `, undefined)
s.Step(` + "`^next undefined$`" + `, nextUndefined)
}`
expected = trimAllLines(expected)
r.run()
actual := trimAllLines(buf.String())
shouldMatchOutput(expected, actual, t)
}
func trimAllLines(s string) string {
var lines []string
for _, ln := range strings.Split(strings.TrimSpace(s), "\n") {
lines = append(lines, strings.TrimSpace(ln))
}
return strings.Join(lines, "\n")
}
var basicGherkinFeature = `
Feature: basic
Scenario: passing scenario
When one
Then two
`
func TestProgressFormatterWhenStepPanics(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() error { panic("omg") })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
out := buf.String()
if idx := strings.Index(out, "godog/fmt_progress_test.go:108"); idx == -1 {
t.Fatalf("expected to find panic stacktrace, actual:\n%s", out)
}
}
func TestProgressFormatterWithPassingMultisteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^sub2$`, func() Steps { return Steps{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{"sub1", "sub2"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
}
func TestProgressFormatterWithFailingMultisteps(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat, Path: "some.feature"}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return fmt.Errorf("errored") })
s.Step(`^sub2$`, func() Steps { return Steps{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{"sub1", "sub2"} })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
expected := `
.F 2
--- Failed steps:
Scenario: passing scenario # some.feature:4
Then two # some.feature:6
Error: sub2: sub-sub: errored
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
0s
`
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
shouldMatchOutput(expected, actual, t)
}
func shouldMatchOutput(expected, actual string, t *testing.T) {
act := []byte(actual)
exp := []byte(expected)
if len(act) != len(exp) {
t.Fatalf("content lengths do not match, expected: %d, actual %d, actual output:\n%s", len(exp), len(act), actual)
}
for i := 0; i < len(exp); i++ {
if act[i] == exp[i] {
continue
}
cpe := make([]byte, len(exp))
copy(cpe, exp)
e := append(exp[:i], '^')
e = append(e, cpe[i:]...)
cpa := make([]byte, len(act))
copy(cpa, act)
a := append(act[:i], '^')
a = append(a, cpa[i:]...)
t.Fatalf("expected output does not match:\n%s\n\n%s", string(a), string(e))
}
}
func TestProgressFormatterWithPanicInMultistep(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub1$`, func() error { return nil })
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^sub2$`, func() []string { return []string{"sub-sub", "sub1", "one"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() []string { return []string{"sub1", "sub2"} })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
}
func TestProgressFormatterMultistepTemplates(t *testing.T) {
feat, err := gherkin.ParseFeature(strings.NewReader(basicGherkinFeature))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^sub-sub$`, func() error { return nil })
s.Step(`^substep$`, func() Steps { return Steps{"sub-sub", `unavailable "John" cost 5`, "one", "three"} })
s.Step(`^one$`, func() error { return nil })
s.Step(`^(t)wo$`, func(s string) Steps { return Steps{"undef", "substep"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
expected := `
.U 2
1 scenarios (1 undefined)
2 steps (1 passed, 1 undefined)
0s
You can implement step definitions for undefined steps with these snippets:
func undef() error {
return godog.ErrPending
}
func unavailableCost(arg1 string, arg2 int) error {
return godog.ErrPending
}
func three() error {
return godog.ErrPending
}
func FeatureContext(s *godog.Suite) {
s.Step(` + "`^undef$`" + `, undef)
s.Step(` + "`^unavailable \"([^\"]*)\" cost (\\d+)$`" + `, unavailableCost)
s.Step(` + "`^three$`" + `, three)
}
`
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
if actual != expected {
t.Fatalf("expected output does not match: %s", actual)
}
}
func TestProgressFormatterWhenMultiStepHasArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two:
"""
text
"""
`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two:$`, func(doc *gherkin.DocString) Steps { return Steps{"one"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
}
func TestProgressFormatterWhenMultiStepHasStepWithArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var subStep = `three:
"""
content
"""`
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{subStep} })
s.Step(`^three:$`, func(doc *gherkin.DocString) error { return nil })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
expected := `
.F 2
--- Failed steps:
Scenario: passing scenario # :4
Then two # :6
Error: nested steps cannot be multiline and have table or content body argument
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
0s
`
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
if actual != expected {
t.Fatalf("expected output does not match: %s", actual)
}
}