-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample_test.go
More file actions
302 lines (254 loc) · 8.08 KB
/
example_test.go
File metadata and controls
302 lines (254 loc) · 8.08 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
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
package assert_test
import (
"errors"
"fmt"
"testing"
"github.com/adamluzsi/testcase/assert"
)
func ExampleMust() {
var tb testing.TB
// create an assertion helper which will fail the testing context with .Fatal(...) in case of a failed assert.
assert.Must(tb).True(true)
}
func ExampleShould() {
var tb testing.TB
// create an assertion helper which will fail the testing context with .Error(...) in case of a failed assert.
assert.Should(tb).True(true)
}
func ExampleAsserter_True() {
var tb testing.TB
assert.Must(tb).True(true, "optional assertion explanation")
}
func ExampleAsserter_False() {
var tb testing.TB
assert.Must(tb).False(false, "optional assertion explanation")
}
func ExampleAsserter_Nil() {
var tb testing.TB
assert.Must(tb).Nil(nil, "optional assertion explanation")
}
func ExampleAsserter_NotNil() {
var tb testing.TB
assert.Must(tb).NotNil(errors.New("42"), "optional assertion explanation")
}
func ExampleAsserter_Equal() {
var tb testing.TB
assert.Must(tb).Equal(true, true, "optional assertion explanation")
}
func ExampleAsserter_NotEqual() {
var tb testing.TB
assert.Must(tb).NotEqual(true, false, "optional assertion explanation")
}
func ExampleAsserter_Contain() {
var tb testing.TB
assert.Must(tb).Contain([]int{1, 2, 3}, 3, "optional assertion explanation")
assert.Must(tb).Contain([]int{1, 2, 3}, []int{1, 2}, "optional assertion explanation")
assert.Must(tb).Contain(map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 42}, "optional assertion explanation")
}
func ExampleAsserter_NotContain() {
var tb testing.TB
assert.Must(tb).NotContain([]int{1, 2, 3}, 42, "optional assertion explanation")
assert.Must(tb).NotContain([]int{1, 2, 3}, []int{42}, "optional assertion explanation")
assert.Must(tb).NotContain(map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 13}, "optional assertion explanation")
}
func ExampleAsserter_ContainExactly() {
var tb testing.TB
assert.Must(tb).ContainExactly([]int{1, 2, 3}, []int{2, 3, 1}, "optional assertion explanation") // true
assert.Must(tb).ContainExactly([]int{1, 2, 3}, []int{1, 42, 2}, "optional assertion explanation") // false
}
func ExampleAsserter_Panic() {
var tb testing.TB
assert.Must(tb).Panic(func() { panic("boom") }, "optional assertion explanation")
}
func ExampleAsserter_NotPanic() {
var tb testing.TB
assert.Must(tb).NotPanic(func() { /* no boom */ }, "optional assertion explanation")
}
func ExampleAsserter_AnyOf() {
var tb testing.TB
var list []interface {
Foo() int
Bar() bool
Baz() string
}
assert.Must(tb).AnyOf(func(anyOf *assert.AnyOf) {
for _, testingCase := range list {
anyOf.Test(func(it assert.It) {
it.Must.True(testingCase.Bar())
})
}
})
}
func ExampleAnyOf_listOfInterface() {
var tb testing.TB
type ExampleInterface interface {
Foo() int
Bar() bool
Baz() string
}
anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal}
for _, v := range []ExampleInterface{} {
anyOf.Test(func(it assert.It) {
it.Must.True(v.Bar())
})
}
anyOf.Finish()
}
func ExampleAnyOf_listOfCompositedStructuresWhereOnlyTheEmbededValueIsRelevant() {
var tb testing.TB
type BigStruct struct {
ID string // not relevant for the test
A, B, C, D, E int // not relevant data as well
WrappedStruct struct {
A, B, C int // relevant data for the test
}
}
anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal}
for _, v := range []BigStruct{} {
anyOf.Test(func(it assert.It) {
it.Must.Equal(42, v.WrappedStruct.A)
it.Must.Equal(1, v.WrappedStruct.B)
it.Must.Equal(2, v.WrappedStruct.C)
})
}
anyOf.Finish()
}
func ExampleAnyOf_listOfStructuresWithIrrelevantValues() {
var tb testing.TB
type StructWithDynamicValues struct {
IrrelevantStateValue int // not relevant data for the test
ImportantValue int
}
anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal}
for _, v := range []StructWithDynamicValues{} {
anyOf.Test(func(it assert.It) {
it.Must.Equal(42, v.ImportantValue)
})
}
anyOf.Finish()
}
func ExampleAnyOf_structWithManyAcceptableState() {
var tb testing.TB
type ExampleStruct struct {
Type string
A, B, C int
}
var es ExampleStruct
anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal}
anyOf.Test(func(it assert.It) {
it.Must.Equal(`foo`, es.Type)
it.Must.Equal(1, es.A)
it.Must.Equal(2, es.B)
it.Must.Equal(3, es.C)
})
anyOf.Test(func(it assert.It) {
it.Must.Equal(`foo`, es.Type)
it.Must.Equal(3, es.A)
it.Must.Equal(2, es.B)
it.Must.Equal(1, es.C)
})
anyOf.Test(func(it assert.It) {
it.Must.Equal(`bar`, es.Type)
it.Must.Equal(11, es.A)
it.Must.Equal(12, es.B)
it.Must.Equal(13, es.C)
})
anyOf.Test(func(it assert.It) {
it.Must.Equal(`baz`, es.Type)
it.Must.Equal(21, es.A)
it.Must.Equal(22, es.B)
it.Must.Equal(23, es.C)
})
anyOf.Finish()
}
type ExamplePublisherEvent struct{ V int }
type ExamplePublisher struct{}
func (ExamplePublisher) Publish(event ExamplePublisherEvent) {}
func (ExamplePublisher) Subscribe(func(event ExamplePublisherEvent)) {}
func (ExamplePublisher) Wait() {}
func (ExamplePublisher) Close() error { return nil }
func ExampleAnyOf_fanOutPublishing() {
var tb testing.TB
publisher := ExamplePublisher{}
anyOf := &assert.AnyOf{TB: tb, Fn: tb.Fatal}
for i := 0; i < 42; i++ {
publisher.Subscribe(func(event ExamplePublisherEvent) {
anyOf.Test(func(it assert.It) {
it.Must.Equal(42, event.V)
})
})
}
publisher.Publish(ExamplePublisherEvent{V: 42})
publisher.Wait()
assert.Must(tb).Nil(publisher.Close())
anyOf.Finish()
}
func ExampleAsserter_Empty() {
var tb testing.TB
assert.Must(tb).Empty([]int{}) // pass
assert.Must(tb).Empty([]int{42}) // fail
assert.Must(tb).Empty([42]int{}) // pass
assert.Must(tb).Empty([42]int{42}) // fail
assert.Must(tb).Empty(map[int]int{}) // pass
assert.Must(tb).Empty(map[int]int{42: 24}) // fail
assert.Must(tb).Empty("") // pass
assert.Must(tb).Empty("42") // fail
}
func ExampleAsserter_NotEmpty() {
var tb testing.TB
assert.Must(tb).NotEmpty([]int{42}, "optional assertion explanation")
assert.Must(tb).NotEmpty([]int{}) // fail
assert.Must(tb).NotEmpty([]int{42}) // pass
assert.Must(tb).NotEmpty([42]int{}) // fail
assert.Must(tb).NotEmpty([42]int{42}) // pass
assert.Must(tb).NotEmpty(map[int]int{}) // fail
assert.Must(tb).NotEmpty(map[int]int{42: 24}) // pass
assert.Must(tb).NotEmpty("") // fail
assert.Must(tb).NotEmpty("42") // pass
}
func ExampleAsserter_ErrorIs() {
var tb testing.TB
actualErr := errors.New("boom")
assert.Must(tb).ErrorIs(errors.New("boom"), actualErr) // passes for equality
assert.Must(tb).ErrorIs(errors.New("boom"), fmt.Errorf("wrapped error: %w", actualErr)) // passes for wrapped errors
}
type ExampleEqualable struct {
IrrelevantExportedField int
relevantUnexportedValue int
}
func (es ExampleEqualable) IsEqual(oth ExampleEqualable) bool {
return es.relevantUnexportedValue == oth.relevantUnexportedValue
}
func ExampleAsserter_Equal_isEqualFunctionUsedForComparison() {
var tb testing.TB
expected := ExampleEqualable{
IrrelevantExportedField: 42,
relevantUnexportedValue: 24,
}
actual := ExampleEqualable{
IrrelevantExportedField: 4242,
relevantUnexportedValue: 24,
}
assert.Must(tb).Equal(expected, actual) // passes as by IsEqual terms the two value is equal
}
type ExampleEqualableWithError struct {
IrrelevantExportedField int
relevantUnexportedValue int
IsEqualErr error
}
func (es ExampleEqualableWithError) IsEqual(oth ExampleEqualableWithError) (bool, error) {
return es.relevantUnexportedValue == oth.relevantUnexportedValue, es.IsEqualErr
}
func ExampleAsserter_Equal_isEqualFunctionThatSupportsErrorReturning() {
var tb testing.TB
expected := ExampleEqualableWithError{
IrrelevantExportedField: 42,
relevantUnexportedValue: 24,
IsEqualErr: errors.New("sadly something went wrong"),
}
actual := ExampleEqualableWithError{
IrrelevantExportedField: 42,
relevantUnexportedValue: 24,
}
assert.Must(tb).Equal(expected, actual) // fails because the error returned from the IsEqual function.
}