-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathapprovals_test.go
More file actions
277 lines (234 loc) · 5.83 KB
/
approvals_test.go
File metadata and controls
277 lines (234 loc) · 5.83 KB
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
package approvals
import (
"encoding/xml"
"fmt"
"os"
"regexp"
"strings"
"testing"
"github.com/approvals/go-approval-tests/reporters"
)
// begin-snippet: test_main_with_reporter
func TestMain(m *testing.M) {
r := UseReporter(reporters.NewContinuousIntegrationReporter())
defer r.Close()
UseFolder("testdata")
os.Exit(m.Run())
}
// end-snippet
func TestVerifyStringApproval(t *testing.T) {
// begin-snippet: inline_reporter
r := UseReporter(reporters.NewContinuousIntegrationReporter())
defer r.Close()
// end-snippet
VerifyString(t, "Hello World!")
}
func TestReporterFromSetup(t *testing.T) {
t.Parallel()
VerifyString(t, hello("World"))
}
type ExampleTestCaseParameters struct {
name string
value string
}
// hello world function that can be the system-under-test
func hello(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
// begin-snippet: parameterized_test_with_subtests
var ExampleParameterizedTestcases = []ExampleTestCaseParameters{
{name: "Normal", value: "Sue"},
{name: "Long", value: "Chandrasekhar"},
{name: "Short", value: "A"},
{name: "Composed name", value: "Karl-Martin"},
}
func TestParameterizedTests(t *testing.T) {
for _, tc := range ExampleParameterizedTestcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
VerifyString(t, hello(tc.value))
})
}
}
// end-snippet
func TestVerifyXMLStruct(t *testing.T) {
t.Parallel()
json := struct {
XMLName xml.Name `xml:"Test"`
Title string
Name string
Age int
}{
Title: "Hello World!",
Name: "Peter Pan",
Age: 100,
}
VerifyXMLStruct(t, json)
}
func TestVerifyBadXMLStruct(t *testing.T) {
t.Parallel()
xmlContent := struct {
Title string
}{
Title: "Hello World!",
}
VerifyXMLStruct(t, xmlContent)
}
func TestVerifyXMLBytes(t *testing.T) {
t.Parallel()
xmlb := []byte("<Test><Title>Hello World!</Title><Name>Peter Pan</Name><Age>100</Age></Test>")
VerifyXMLBytes(t, xmlb)
}
func TestVerifyBadXMLBytes(t *testing.T) {
t.Parallel()
xmlb := []byte("Test></Test>")
VerifyXMLBytes(t, xmlb)
}
func TestVerifyJSONStruct(t *testing.T) {
t.Parallel()
json := struct {
Title string
Name string
Age int
}{
Title: "Hello World!",
Name: "Peter Pan",
Age: 100,
}
VerifyJSONStruct(t, json)
}
func TestVerifyJSONBytes(t *testing.T) {
t.Parallel()
jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
VerifyJSONBytes(t, jsonb)
}
func TestVerifyBadJSONBytes(t *testing.T) {
t.Parallel()
jsonb := []byte("{ foo: \"bar\", \"age\": 42, \"bark\": \"woof\" }")
VerifyJSONBytes(t, jsonb)
}
func TestVerifyJSONBytesWithScrubbedIds(t *testing.T) {
t.Parallel()
scrubber := CreateJSONScrubber("id", regexp.MustCompile(`\d+`))
jsonb := []byte(`{ "items": [{ "id": 1 }, { "id": 2 }] }`)
VerifyJSONBytes(t, jsonb, Options().WithScrubber(scrubber))
}
func TestVerifyJSONBytesAcceptsValidJSONTypes(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
json string
}{
{name: "array", json: "[1, 2, 3]"},
{name: "object", json: "{\"foo\": \"bar\"}"},
{name: "number", json: "1"},
{name: "float", json: "1.0"},
{name: "exponential", json: "1.0e10"},
{name: "true", json: "true"},
{name: "false", json: "false"},
{name: "null", json: "null"},
} {
t.Run(tc.name, func(t *testing.T) {
VerifyJSONBytes(t, []byte(tc.json))
})
}
}
func TestVerifyMap(t *testing.T) {
t.Parallel()
m := map[string]string{
"dog": "bark",
"cat": "meow",
}
VerifyMap(t, m)
}
func TestVerifyMapBadMap(t *testing.T) {
t.Parallel()
m := "foo"
VerifyMap(t, m)
}
func TestVerifyMapEmptyMap(t *testing.T) {
t.Parallel()
m := map[string]string{}
VerifyMap(t, m)
}
func TestVerifyArray(t *testing.T) {
t.Parallel()
xs := []string{"dog", "cat", "bird"}
VerifyArray(t, xs)
}
func TestVerifyArrayBadArray(t *testing.T) {
t.Parallel()
xs := "string"
VerifyArray(t, xs)
}
func TestVerifyArrayEmptyArray(t *testing.T) {
t.Parallel()
var xs []string
VerifyArray(t, xs)
}
func TestVerifyArrayTransformation(t *testing.T) {
t.Parallel()
xs := []string{"Christopher", "Llewellyn"}
VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) })
}
func TestVerifyAllCombinationsFor1(t *testing.T) {
t.Parallel()
xs := []string{"Christopher", "Llewellyn"}
VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, xs)
}
func TestVerifyAllCombinationsForSkipped(t *testing.T) {
t.Parallel()
xs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
VerifyAllCombinationsFor1(
t,
"skipped divisible by 3",
func(x interface{}) string {
if x.(int)%3 == 0 {
return SkipThisCombination
}
return fmt.Sprintf("%v", x)
},
xs)
}
func TestVerifyAllCombinationsFor2(t *testing.T) {
t.Parallel()
xs1 := []string{"Christopher", "Llewellyn"}
xs2 := []int{0, 1}
VerifyAllCombinationsFor2(
t,
"character at",
func(s interface{}, i interface{}) string { return fmt.Sprintf("%c", s.(string)[i.(int)]) },
xs1,
xs2)
}
func TestVerifyAllCombinationsFor9(t *testing.T) {
t.Parallel()
xs1 := []string{"Christopher"}
VerifyAllCombinationsFor9(
t,
"sum numbers",
func(s, i2, i3, i4, i5, i6, i7, i8, i9 interface{}) string {
sum := i2.(int) + i3.(int) + i4.(int) + i5.(int) + i6.(int) + i7.(int) + i8.(int) + i9.(int)
return fmt.Sprintf("%v[%v]", s, sum)
},
xs1,
[]int{0, 1},
[]int{2, 3},
[]int{4, 5},
[]int{6, 7},
[]int{8, 9},
[]int{10, 11},
[]int{12, 13},
[]int{14, 15})
}
func TestVerifyStringWithNonExistentFolder(t *testing.T) {
nonExistentDir := "testdata/nonexistent_subdir"
UseFolder(nonExistentDir)
defer UseFolder("testdata")
fakeT := NewTestFailableWithName("TestVerifyStringWithNonExistentFolder")
VerifyString(fakeT, "Hello from a missing folder!")
if !fakeT.Failed() {
t.Error("Expected approval mismatch failure, but test did not fail")
}
}