forked from approvals/go-approval-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
approvals_test.go
207 lines (172 loc) · 4.24 KB
/
approvals_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
package approvals
import (
"encoding/xml"
"fmt"
"os"
"strings"
"testing"
"github.com/customerio/go-approval-tests/reporters"
)
func TestMain(m *testing.M) {
r := UseReporter(reporters.NewContinuousIntegrationReporter())
defer r.Close()
UseFolder("testdata")
os.Exit(m.Run())
}
func TestVerifyStringApproval(t *testing.T) {
r := UseReporter(reporters.NewContinuousIntegrationReporter())
defer r.Close()
VerifyString(t, "Hello World!")
}
func TestReporterFromSetup(t *testing.T) {
VerifyString(t, hello("World"))
}
type ExampleTestCaseParameters struct {
name string
value string
}
var ExampleParameterizedTestcases = []ExampleTestCaseParameters{
{name: "Normal", value: "Sue"},
{name: "Long", value: "Chandrasekhar"},
{name: "Short", value: "A"},
{name: "Composed name", value: "Karl-Martin"},
}
// hello world function that can be the system-under-test
func hello(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
func TestParameterizedTests(t *testing.T) {
t.Parallel()
for _, tc := range ExampleParameterizedTestcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
VerifyString(t, hello(tc.value))
})
}
}
func TestVerifyXMLStruct(t *testing.T) {
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) {
xmlContent := struct {
Title string
}{
Title: "Hello World!",
}
VerifyXMLStruct(t, xmlContent)
}
func TestVerifyXMLBytes(t *testing.T) {
xmlb := []byte("<Test><Title>Hello World!</Title><Name>Peter Pan</Name><Age>100</Age></Test>")
VerifyXMLBytes(t, xmlb)
}
func TestVerifyBadXMLBytes(t *testing.T) {
xmlb := []byte("Test></Test>")
VerifyXMLBytes(t, xmlb)
}
func TestVerifyJSONStruct(t *testing.T) {
json := struct {
Title string
Name string
Age int
}{
Title: "Hello World!",
Name: "Peter Pan",
Age: 100,
}
VerifyJSONStruct(t, json)
}
func TestVerifyJSONBytes(t *testing.T) {
jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
VerifyJSONBytes(t, jsonb)
}
func TestVerifyBadJSONBytes(t *testing.T) {
jsonb := []byte("{ foo: \"bar\", \"age\": 42, \"bark\": \"woof\" }")
VerifyJSONBytes(t, jsonb)
}
func TestVerifyMap(t *testing.T) {
m := map[string]string{
"dog": "bark",
"cat": "meow",
}
VerifyMap(t, m)
}
func TestVerifyMapBadMap(t *testing.T) {
m := "foo"
VerifyMap(t, m)
}
func TestVerifyMapEmptyMap(t *testing.T) {
m := map[string]string{}
VerifyMap(t, m)
}
func TestVerifyArray(t *testing.T) {
xs := []string{"dog", "cat", "bird"}
VerifyArray(t, xs)
}
func TestVerifyArrayBadArray(t *testing.T) {
xs := "string"
VerifyArray(t, xs)
}
func TestVerifyArrayEmptyArray(t *testing.T) {
var xs []string
VerifyArray(t, xs)
}
func TestVerifyArrayTransformation(t *testing.T) {
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) {
xs := []string{"Christopher", "Llewellyn"}
VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, xs)
}
func TestVerifyAllCombinationsForSkipped(t *testing.T) {
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) {
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) {
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})
}