-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp_test.go
272 lines (233 loc) · 7.62 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
package app_test
import (
"bytes"
"flag"
"testing"
"github.com/hedhyw/gherkingen/v2/internal/app"
"github.com/hedhyw/gherkingen/v2/pkg/bdd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testVersion = "0.0.1"
func TestApplicationCommandLineTool(t *testing.T) {
f := bdd.NewFeature(t, "Application command line tool")
f.Scenario("User wants to generate the output in given format", func(_ *testing.T, f *bdd.Feature) {
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"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("<format> is given", func() {
arguments = append(arguments, "-format", tc.Format)
})
f.And("<feature> is provided", func() {
arguments = append(arguments, tc.Feature)
})
f.Then("the output should be generated", func() {
runApp(t, arguments, tc.Assertion == "does")
})
})
})
f.Scenario("User wants to see usage information", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--help": {"--help"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("<flag> is provided", func() {
arguments = append(arguments, tc.Flag)
})
f.Then("usage should be printed", func() {
runApp(t, arguments, true)
})
})
})
f.Scenario("User wants to list built-in templates", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--list": {"--list"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("<flag> is provided", func() {
arguments = append(arguments, tc.Flag)
})
f.Then("templates should be printed", func() {
runApp(t, arguments, true)
})
})
})
f.Scenario("User wants to use custom template", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Feature string `field:"<feature>"`
Template string `field:"<template>"`
}
testCases := map[string]testCase{
"app.feature_../assets/std.struct.v1.go.tmpl": {"app.feature", "../assets/std.struct.v1.go.tmpl"},
"app.feature_@/std.struct.v1.go.tmpl": {"app.feature", "@/std.struct.v1.go.tmpl"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.And("<template> is provided", func() {
arguments = append(arguments, "-template", tc.Template)
})
f.When("<feature> is provided", func() {
arguments = append(arguments, tc.Feature)
})
f.Then("the output should be generated", func() {
runApp(t, arguments, true)
})
})
})
f.Scenario("User wants to set custom package", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Package string `field:"<package>"`
}
testCases := map[string]testCase{
"app_test": {"app_test"},
"example_test": {"example_test"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("<package> is provided", func() {
arguments = append(arguments, "-package", tc.Package, "app.feature")
})
f.Then("the output should contain <package>", func() {
out := runApp(t, arguments, true)
assert.Contains(t, out, tc.Package)
})
})
})
f.Scenario("User wants to generate a permanent json output", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
TheSameIDs bool `field:"<TheSameIDs>"`
}
testCases := map[string]testCase{
"true": {true},
"false": {false},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("format is json", func() {
arguments = append(arguments, "-format", "json")
})
f.And("-permanent-ids is <TheSameIDs>", func() {
if tc.TheSameIDs {
arguments = append(arguments, "-permanent-ids")
}
})
f.Then("calling generation twice will produce the same output <TheSameIDs>", func() {
arguments = append(arguments, "app.feature")
firstOut := runApp(t, arguments, true)
secondOut := runApp(t, arguments, true)
if tc.TheSameIDs {
assert.Equal(t, firstOut, secondOut)
} else {
assert.NotEqual(t, firstOut, secondOut)
}
})
})
})
f.Scenario("User gives an invalid flag", func(t *testing.T, f *bdd.Feature) {
arguments := []string{}
f.When("flag -invalid is provided", func() {
arguments = append(arguments, "-invalid")
})
f.Then("a generation failed", func() {
arguments = append(arguments, "app.feature")
runApp(t, arguments, false)
})
})
f.Scenario("User wants to know version", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Flag string `field:"<flag>"`
}
testCases := map[string]testCase{
"--version": {"--version"},
"-version": {"-version"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("<flag> is provided", func() {
arguments = append(arguments, tc.Flag)
})
f.Then("version is printed", func() {
out := runApp(t, arguments, true)
assert.Contains(t, out, testVersion)
})
})
})
f.Scenario("User specifies a file, but the file is not found", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Feature string `field:"<feature>"`
Template string `field:"<template>"`
}
testCases := map[string]testCase{
"app.feature_not_found": {"app.feature", "not_found"},
}
f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("inexistent <template> is provided", func() {
arguments = append(arguments, "-template", tc.Template)
})
f.And("<feature> is provided", func() {
arguments = append(arguments, tc.Feature)
})
f.Then("the user receives an error", func() {
runApp(t, arguments, false)
})
})
})
f.Scenario("User wants to run tests in parallel", func(t *testing.T, f *bdd.Feature) {
arguments := []string{}
f.When("`-go-parallel` is provided", func() {
arguments = append(arguments, "-go-parallel")
})
f.And("`app.feature` is given", func() {
arguments = append(arguments, "../generator/examples/scenario.feature")
})
f.Then("generated code contains `t.Parallel()`", func() {
assert.Contains(t, runApp(t, arguments, true), "t.Parallel()")
})
})
f.Scenario("User wants to run tests sequentially", func(t *testing.T, f *bdd.Feature) {
arguments := []string{}
f.When("`-go-parallel` is provided", func() {
// Go on.
})
f.And("`app.feature` is given", func() {
arguments = append(arguments, "../generator/examples/scenario.feature")
})
f.Then("generated code doesn't contain `t.Parallel()`", func() {
assert.NotContains(t, runApp(t, arguments, true), "t.Parallel()")
})
})
}
func runApp(tb testing.TB, arguments []string, ok bool) string {
tb.Helper()
flag.CommandLine = flag.NewFlagSet("", flag.PanicOnError)
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)
}
return buf.String()
}