-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
256 lines (231 loc) · 6.04 KB
/
assert.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
// Package testutils contains a minimal set of utils for testing. The package is
// inspired by testify.
package testutils
import (
"bytes"
"errors"
"fmt"
"reflect"
)
type tHelper interface {
Helper()
}
// TestingT is an interface wrapper around *testing.T.
type TestingT interface {
Name() string
Errorf(format string, args ...any)
Log(...any)
FailNow()
}
// Assertions is a collection of assertion methods that can be used to test
// conditions in your tests.
type Assertions struct {
t TestingT
failImmediately bool
}
// NewAssert returns a new Assertions object that will log assertion failures.
func NewAssert(t TestingT) Assertions {
return Assertions{t, false}
}
// NewRequire returns a new Assertions object that will log assertion failures
// and stop test execution.
func NewRequire(t TestingT) Assertions {
return Assertions{t, true}
}
// Equal asserts that two objects are equal.
func (a Assertions) Equal(exp, act any, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if !equal(exp, act) {
a.log(fmt.Sprintf("expected '%v', got: '%v'", exp, act), msgAndArgs...)
return false
}
return true
}
// NotEqual asserts that two objects are not equal.
func (a Assertions) NotEqual(exp, act any, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if !equal(exp, act) {
a.log(fmt.Sprintf("expected '%v', got: '%v'", exp, act), msgAndArgs...)
return false
}
return true
}
// Error asserts that a function returned an error (i.e. not `nil`).
func (a Assertions) Error(err error, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if err == nil {
a.log("expected an error", msgAndArgs...)
return false
}
return true
}
// NoError asserts that a function returned no error (i.e. `nil`).
func (a Assertions) NoError(err error, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if err != nil {
a.log(fmt.Sprintf("expected no error, got: '%v'", err), msgAndArgs...)
return false
}
return true
}
// EqualError asserts that a function returned an error (i.e. not `nil`) and
// that it is equal to the provided error.
func (a Assertions) EqualError(expErr, actErr error, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if expErr == nil {
return a.NoError(actErr)
} else if !errors.Is(actErr, expErr) {
a.log(fmt.Sprintf("expected error '%v', got: '%v'", expErr, actErr), msgAndArgs...)
return false
}
return true
}
// Panic asserts that the code inside the specified PanicTestFunc panics.
func (a Assertions) Panic(f func(), msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
defer func() {
if r := recover(); r == nil {
a.log("expected a panic", msgAndArgs...)
}
}()
f()
return true
}
// NotPanic asserts that the code inside the specified PanicTestFunc does not
// panic.
func (a Assertions) NotPanic(f func(), msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
defer func() {
if r := recover(); r != nil {
a.log(fmt.Sprintf("expected no panic, got: '%v'", r), msgAndArgs...)
}
}()
f()
return true
}
// False asserts that the specified value is false.
func (a Assertions) False(exp bool, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if exp {
a.log("expected false, got true", msgAndArgs...)
return false
}
return true
}
// True asserts that the specified value is true.
func (a Assertions) True(exp bool, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if !exp {
a.log("expected true, got false", msgAndArgs...)
return false
}
return true
}
// IsType asserts that the specified object is of the specified type.
func (a Assertions) IsType(expType, obj any, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if !equal(reflect.TypeOf(obj), reflect.TypeOf(expType)) {
a.log(fmt.Sprintf("expected object to be of type %v, was %v", reflect.TypeOf(expType), reflect.TypeOf(obj)), msgAndArgs...)
return false
}
return true
}
// Nil asserts that the specified object is nil.
func (a Assertions) Nil(obj any, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if obj != nil {
a.log(fmt.Sprintf("expected object to be nil, was %v", obj), msgAndArgs...)
return false
}
return true
}
// NotNil asserts that the specified object is not nil.
func (a Assertions) NotNil(obj any, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
if obj == nil {
a.log("expected object not to be nil", msgAndArgs...)
return false
}
return true
}
// Len asserts that the specified object has specific length.
func (a Assertions) Len(obj any, length int, msgAndArgs ...any) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
rv := reflect.ValueOf(obj)
switch rv.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
if rv.Len() != length {
a.log(fmt.Sprintf("expected object to have length %v, was %v", length, rv.Len()), msgAndArgs...)
return false
}
default:
a.log(fmt.Sprintf("expected object to be of type array, chan, map, slice or string, was %v", rv.Kind()), msgAndArgs...)
return false
}
return true
}
// log is a helper function that formats the message and logs it.
func (a Assertions) log(defaultMsg string, msgAndArgs ...any) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
msg := defaultMsg
if len(msgAndArgs) > 0 {
msg = fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
name := a.t.Name()
if name != "" {
msg = fmt.Sprintf("[%s] %s", name, msg)
}
if a.failImmediately {
a.t.Log(msg)
a.t.FailNow()
} else {
a.t.Errorf(msg)
}
}
// equal is a helper function that compares two objects and returns true if they
// are equal.
func equal(expected, actual any) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}