-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
assert_test.go
252 lines (227 loc) · 6 KB
/
assert_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
package assert
import (
"fmt"
"os"
"testing"
)
type Data struct {
Str string
Num int64
}
func TestEqual(t *testing.T) {
assertOk(t, "IdenticalStruct", func(t testing.TB) {
Equal(t, Data{"expected", 1234}, Data{"expected", 1234})
})
assertOk(t, "Zero length byte arrays", func(t testing.TB) {
Equal(t, []byte(""), []byte(nil))
})
assertOk(t, "Identical byte arrays", func(t testing.TB) {
Equal(t, []byte{4, 2}, []byte{4, 2})
})
assertOk(t, "Identical numbers", func(t testing.TB) {
Equal(t, 42, 42)
})
assertFail(t, "DifferentStruct", func(t testing.TB) {
Equal(t, Data{"expected\ntext", 1234}, Data{"actual\ntext", 1234})
})
assertFail(t, "Different bytes arrays", func(t testing.TB) {
Equal(t, []byte{2, 4}, []byte{4, 2})
})
assertFail(t, "Different numbers", func(t testing.TB) {
Equal(t, 42, 43)
})
assertOk(t, "Exclude", func(t testing.TB) {
Equal(t, Data{Str: "expected", Num: 1234}, Data{Str: "expected"}, Exclude[int64]())
})
}
func TestEqualStrings(t *testing.T) {
assertFail(t, "IdenticalStrings", func(t testing.TB) {
Equal(t, "hello\nworld", "goodbye\nworld")
})
}
func TestNotEqual(t *testing.T) {
assertOk(t, "DifferentFieldValue", func(t testing.TB) {
NotEqual(t, Data{"expected", 1234}, Data{"expected", 1235})
})
assertFail(t, "SameValue", func(t testing.TB) {
NotEqual(t, Data{"expected", 1234}, Data{"expected", 1234})
})
assertFail(t, "Exclude", func(t testing.TB) {
NotEqual(t, Data{Str: "expected", Num: 1234}, Data{Str: "expected"}, Exclude[int64]())
})
}
func TestContains(t *testing.T) {
assertOk(t, "Found", func(t testing.TB) {
Contains(t, "a haystack with a needle in it", "needle")
})
assertFail(t, "NotFound", func(t testing.TB) {
Contains(t, "a haystack with a needle in it", "screw")
})
}
func TestNotContains(t *testing.T) {
assertOk(t, "NotFound", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "screw")
})
assertFail(t, "Found", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "needle")
})
}
func TestSliceContains(t *testing.T) {
assertOk(t, "Found", func(t testing.TB) {
SliceContains(t, []string{"hello", "world"}, "hello")
})
assertFail(t, "NotFound", func(t testing.TB) {
SliceContains(t, []string{"hello", "world"}, "goodbye")
})
}
func TestNotSliceContains(t *testing.T) {
assertOk(t, "NotFound", func(t testing.TB) {
NotSliceContains(t, []string{"hello", "world"}, "goodbye")
})
assertFail(t, "Found", func(t testing.TB) {
NotSliceContains(t, []string{"hello", "world"}, "hello")
})
}
func TestEqualError(t *testing.T) {
assertOk(t, "SameMessage", func(t testing.TB) {
EqualError(t, fmt.Errorf("hello"), "hello")
})
assertOk(t, "Nil", func(t testing.TB) {
EqualError(t, nil, "")
})
assertFail(t, "MessageMismatch", func(t testing.TB) {
EqualError(t, fmt.Errorf("hello"), "goodbye")
})
}
func TestError(t *testing.T) {
assertOk(t, "Error", func(t testing.TB) {
Error(t, fmt.Errorf("hello"))
})
assertFail(t, "Nil", func(t testing.TB) {
Error(t, nil)
})
}
func TestNoError(t *testing.T) {
assertOk(t, "Nil", func(t testing.TB) {
NoError(t, nil)
})
assertFail(t, "Error", func(t testing.TB) {
NoError(t, fmt.Errorf("hello"))
})
}
func TestZero(t *testing.T) {
assertOk(t, "Struct", func(t testing.TB) {
Zero(t, Data{})
})
assertOk(t, "NilSlice", func(t testing.TB) {
var slice []int
Zero(t, slice)
})
assertFail(t, "NonEmptyStruct", func(t testing.TB) {
Zero(t, Data{Str: "str"})
})
assertFail(t, "NonEmptySlice", func(t testing.TB) {
slice := []int{1, 2, 3}
Zero(t, slice)
})
assertOk(t, "ZeroLenSlice", func(t testing.TB) {
slice := []int{}
Zero(t, slice)
})
}
func TestNotZero(t *testing.T) {
assertOk(t, "PopulatedStruct", func(t testing.TB) {
notZero := Data{Str: "hello"}
NotZero(t, notZero)
})
assertFail(t, "EmptyStruct", func(t testing.TB) {
zero := Data{}
NotZero(t, zero)
})
assertFail(t, "NilSlice", func(t testing.TB) {
var slice []int
NotZero(t, slice)
})
assertFail(t, "ZeroLenSlice", func(t testing.TB) {
slice := []int{}
NotZero(t, slice)
})
assertOk(t, "Slice", func(t testing.TB) {
slice := []int{1, 2, 3}
NotZero(t, slice)
})
}
func TestIsError(t *testing.T) {
assertOk(t, "SameError", func(t testing.TB) {
IsError(t, fmt.Errorf("os error: %w", os.ErrClosed), os.ErrClosed)
})
assertFail(t, "DifferentError", func(t testing.TB) {
IsError(t, fmt.Errorf("not an os error"), os.ErrClosed)
})
}
func TestInvalidFormatMsg(t *testing.T) {
Panics(t, func() {
NotZero(t, Data{}, 123)
})
}
func TestNotIsError(t *testing.T) {
assertFail(t, "SameError", func(t testing.TB) {
NotIsError(t, fmt.Errorf("os error: %w", os.ErrClosed), os.ErrClosed)
})
assertOk(t, "DifferentError", func(t testing.TB) {
NotIsError(t, fmt.Errorf("not an os error"), os.ErrClosed)
})
}
func TestDiff(t *testing.T) {
Equal(t, "-before\n+after\n", Diff("before", "after"))
}
func TestHasSuffix(t *testing.T) {
assertOk(t, "Suffix", func(t testing.TB) {
HasSuffix(t, "hello", "lo")
})
assertFail(t, "NoSuffix", func(t testing.TB) {
HasSuffix(t, "hello", "world")
})
}
func TestHasPrefix(t *testing.T) {
assertOk(t, "Prefix", func(t testing.TB) {
HasPrefix(t, "hello", "he")
})
assertFail(t, "NoPrefix", func(t testing.TB) {
HasPrefix(t, "hello", "world")
})
}
type testTester struct {
*testing.T
failed string
}
func (t *testTester) Fatalf(message string, args ...interface{}) {
t.failed = fmt.Sprintf(message, args...)
}
func (t *testTester) Fatal(args ...interface{}) {
t.failed = fmt.Sprint(args...)
}
func assertFail(t *testing.T, name string, fn func(t testing.TB)) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
tester := &testTester{T: t}
fn(tester)
if tester.failed == "" {
t.Fatal("Should have failed")
} else {
t.Log(tester.failed)
}
})
}
func assertOk(t *testing.T, name string, fn func(t testing.TB)) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
tester := &testTester{T: t}
fn(tester)
if tester.failed != "" {
t.Fatal("Should not have failed with:\n", tester.failed)
}
})
}