-
Notifications
You must be signed in to change notification settings - Fork 4
/
app_test.go
435 lines (331 loc) · 11.3 KB
/
app_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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//nolint:goconst // These are auto generated tests.
package app_test
import (
"bytes"
"testing"
gherkin "github.com/cucumber/gherkin/go/v28"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hedhyw/gherkingen/v4/internal/app"
)
const testVersion = "0.0.1"
func TestApplicationCommandLineTool(t *testing.T) {
t.Parallel()
t.Run("User wants to generate the output in given format", func(t *testing.T) {
t.Parallel()
type testCase struct {
Feature string `field:"<feature>"`
Format string `field:"<format>"`
Assertion string `field:"<assertion>"`
}
testCases := map[string]testCase{
"app.feature_go_does": {"app.feature", "go", "does"},
"app.feature_json_does": {"app.feature", "json", "does"},
"app.feature_raw_does": {"app.feature", "raw", "does"},
"app.feature_invalid_does_not": {"app.feature", "invalid", "does not"},
"notfound.feature_raw_does_not": {"notfound.feature", "raw", "does not"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <format> is given.
arguments := []string{"-format", testCase.Format}
// And <feature> is provided.
arguments = append(arguments, testCase.Feature)
// Then the output should be generated.
runApp(t, arguments, testCase.Assertion == "does")
})
}
})
t.Run("User wants to see usage information", func(t *testing.T) {
t.Parallel()
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--help": {"--help"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <flag> is provided.
arguments := []string{testCase.Flag}
// Then usage should be printed.
runApp(t, arguments, true)
})
}
})
t.Run("User wants to list built-in templates", func(t *testing.T) {
t.Parallel()
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--list": {"--list"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <flag> is provided.
arguments := []string{testCase.Flag}
// Then templates should be printed.
runApp(t, arguments, true)
})
}
})
t.Run("User asks for a version", func(t *testing.T) {
t.Parallel()
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--version": {"--version"},
"-version": {"-version"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <flag> is provided.
arguments := []string{testCase.Flag}
// Then version is printed.
out := runApp(t, arguments, true)
assert.Contains(t, out, testVersion)
})
}
})
t.Run("User wants to generate the output for a feature written in a specific natural language", func(t *testing.T) {
t.Parallel()
type testCase struct {
Language string `field:"<language>"`
Feature string `field:"<feature>"`
Assertion string `field:"assertion"`
}
testCases := map[string]testCase{
"en_../generator/examples/simple.feature_does": {"en", "../generator/examples/simple.feature", "does"},
"en-pirate_../generator/examples/simple.en-pirate.feature_does": {"en-pirate", "../generator/examples/simple.en-pirate.feature", "does"},
"unsupported_app.feature_does_not": {"unsupported", "app.feature", "does not"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When the <language> is given.
arguments := []string{
"-language",
testCase.Language,
}
// And the <feature> is provided.
arguments = append(arguments, testCase.Feature)
// Then the output should be generated.
runApp(t, arguments, testCase.Assertion == "does")
})
}
})
t.Run("User wants to see all supported natural languages", func(t *testing.T) {
t.Parallel()
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"-languages": {"-languages"},
"--languages": {"--languages"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When the <flag> is provided.
arguments := []string{testCase.Flag}
// Then the list of supported natural languages should be printed.
out := runApp(t, arguments, true)
dialectProvider := gherkin.DialectsBuiltin()
dialect := dialectProvider.GetDialect(gherkin.DefaultDialect)
assert.Contains(t, out, dialect.Name)
assert.Contains(t, out, dialect.Language)
assert.Contains(t, out, dialect.Native)
})
}
})
t.Run("User wants to see consistent supported natural languages output", func(t *testing.T) {
t.Parallel()
// When the user lists supported natural languages several times.
arguments := []string{"-languages"}
out1 := runApp(t, arguments, true)
out2 := runApp(t, arguments, true)
// Then the output is the same.
assert.Equal(t, out1, out2)
})
t.Run("User wants to be able to specify the natural language of a feature in its file name", func(t *testing.T) {
t.Parallel()
type testCase struct {
Language string `field:"<language>"`
Feature string `field:"<feature>"`
Assertion string `field:"assertion"`
}
testCases := map[string]testCase{
"en-pirate_./testdata/pirate.feature_does": {"en-pirate", "./testdata/pirate.feature", "does"},
"_./testdata/pirate.feature_does_not": {"", "./testdata/pirate.feature", "does not"},
"_./testdata/pirate.sample.en-pirate.feature_does": {"", "./testdata/pirate.sample.en-pirate.feature", "does"},
"wrong_./testdata/pirate.sample.en-pirate.feature_does_not": {"wrong", "./testdata/pirate.sample.en-pirate.feature", "does not"},
"en_./testdata/english.feature_does": {"en", "./testdata/english.feature", "does"},
"_./testdata/english.feature_does": {"", "./testdata/english.feature", "does"},
"_./testdata/english.sample.en.feature_does": {"", "./testdata/english.sample.en.feature", "does"},
"wrong_./testdata/english.sample.en.feature_does_not": {"wrong", "./testdata/english.sample.en.feature", "does not"},
"_app.feature_does": {"", "app.feature", "does"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
var arguments []string
// When the <language> is specified by the flag.
if testCase.Language != "" {
arguments = append(arguments, "-language", testCase.Language)
}
// And the <feature> is given.
arguments = append(arguments, testCase.Feature)
// Then the output should be generated.
runApp(t, arguments, testCase.Assertion == "does")
})
}
})
}
func TestApplicationCommandLineToolCustom(t *testing.T) {
t.Parallel()
t.Run("User wants to use custom template", func(t *testing.T) {
t.Parallel()
type testCase struct {
Feature string `field:"<feature>"`
Template string `field:"<template>"`
}
testCases := map[string]testCase{
"app.feature_../assets/std.simple.v1.go.tmpl": {"app.feature", "../assets/std.simple.v1.go.tmpl"},
"app.feature_@/std.simple.v1.go.tmpl": {"app.feature", "@/std.simple.v1.go.tmpl"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <template> is provided.
arguments := []string{"-template", testCase.Template}
// And <feature> is provided.
arguments = append(arguments, testCase.Feature)
// Then the output should be generated.
runApp(t, arguments, true)
})
}
})
t.Run("User wants to set custom package", func(t *testing.T) {
t.Parallel()
type testCase struct {
Package string `field:"<package>"`
}
testCases := map[string]testCase{
"app_test": {"app_test"},
"example_test": {"example_test"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When <package> is provided.
arguments := []string{"-package", testCase.Package, "app.feature"}
// Then the output should contain <package>.
out := runApp(t, arguments, true)
assert.Contains(t, out, testCase.Package)
})
}
})
t.Run("User wants to generate a permanent json output", func(t *testing.T) {
t.Parallel()
type testCase struct {
TheSameIDs bool `field:"<TheSameIDs>"`
}
testCases := map[string]testCase{
"true": {true},
"false": {false},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When -format is json.
arguments := []string{"-format", "json"}
// And -permanent-ids is <TheSameIDs>.
if testCase.TheSameIDs {
arguments = append(arguments, "-permanent-ids")
}
// Then calling generation twice will produce the same output <TheSameIDs>.
arguments = append(arguments, "app.feature")
firstOut := runApp(t, arguments, true)
secondOut := runApp(t, arguments, true)
if testCase.TheSameIDs {
assert.Equal(t, firstOut, secondOut)
} else {
assert.NotEqual(t, firstOut, secondOut)
}
})
}
})
}
func TestApplicationCommandLineToolFailures(t *testing.T) {
t.Parallel()
t.Run("User provides an invalid flag", func(t *testing.T) {
t.Parallel()
// When flag -invalid is provided.
arguments := []string{
"-invalid",
"app.feature",
}
// Then an error is returned.
runApp(t, arguments, false)
})
t.Run("User specifies a file, but the file is not found", func(t *testing.T) {
t.Parallel()
type testCase struct {
Feature string `field:"<feature>"`
Template string `field:"<template>"`
}
testCases := map[string]testCase{
"app.feature_not_found": {"app.feature", "not_found"},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
// When inexistent <template> is provided.
arguments := []string{"-template", testCase.Template}
// And <feature> is provided.
arguments = append(arguments, testCase.Feature)
// Then the user receives an error.
runApp(t, arguments, false)
})
}
})
}
func TestApplicationCommandLineToolParallel(t *testing.T) {
t.Parallel()
t.Run("User wants to run tests in parallel", func(t *testing.T) {
t.Parallel()
// When `scenario.feature` is given.
arguments := []string{"../generator/examples/scenario.feature"}
// Then generated code contains `t.Parallel()`.
assert.Contains(t, runApp(t, arguments, true), "t.Parallel()")
})
t.Run("User wants to run tests sequentially", func(t *testing.T) {
t.Parallel()
// When `-disable-go-parallel` is provided.
arguments := []string{"-disable-go-parallel"}
// And `scenario.feature` is given.
arguments = append(arguments, "../generator/examples/scenario.feature")
// Then generated code doesn't contain `t.Parallel()`.
assert.NotContains(t, runApp(t, arguments, true), "t.Parallel()")
})
}
func runApp(tb testing.TB, arguments []string, ok bool) string {
tb.Helper()
tb.Log("running application with arguments", arguments)
var buf bytes.Buffer
err := app.Run(arguments, &buf, testVersion)
if ok {
require.NoError(tb, err)
gotLen := buf.Len()
assert.NotZero(tb, gotLen)
} else {
require.Error(tb, err, buf.String())
}
return buf.String()
}