-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
236 lines (178 loc) · 6.56 KB
/
assert.go
File metadata and controls
236 lines (178 loc) · 6.56 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
package assert
import (
"encoding/json"
"errors"
)
// Testing is an interface wrapper around *testing.T
type Testing interface {
Helper()
Errorf(format string, args ...any)
}
// Fail reports a failure message through
func Fail(t Testing, message string) bool {
t.Helper()
t.Errorf(message)
return false
}
// Failf reports a failure formatted message through
func Failf(t Testing, format string, args ...any) bool {
t.Helper()
t.Errorf(format, args...)
return false
}
// True asserts that the specified value is true.
func True(t Testing, condition bool, messages ...string) bool {
t.Helper()
if !condition {
return Failf(t, "%sShould be true", message(messages))
}
return true
}
// False asserts that the specified value is false.
func False(t Testing, condition bool, messages ...string) bool {
t.Helper()
if condition {
return Failf(t, "%sShould be false", message(messages))
}
return true
}
// Equal asserts that two objects are equal.
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
func Equal[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if !equal(actual, expected) {
return Failf(t, "%sShould be equal:\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// NotEqual asserts that the specified values are NOT equal.
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
func NotEqual[T Comparable](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if equal(actual, expected) {
return Failf(t, "%sShould not be equal\n actual: %s", message(messages), print(actual))
}
return true
}
// EqualDelta asserts that two numeric values difference is less then delta.
//
// Method panics if delta value is not positive.
func EqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
t.Helper()
if !equalDelta(actual, expected, delta) {
return Failf(t, "%sShould be equal in delta:\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// Same asserts that two pointers reference the same object.
//
// Both arguments must be pointer variables. Pointer variable equality is
// determined based on the equality of both type and value.
func Same[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if valid, ok := same[T](expected, actual); !ok {
return Failf(t, "%sShould be pointers\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
} else if !valid {
return Failf(t, "%sShould be same\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
}
return true
}
// NotSame asserts that two pointers do NOT reference the same object.
//
// Both arguments must be pointer variables. Pointer variable equality is
// determined based on the equality of both type and value.
func NotSame[T Reference](t Testing, actual, expected T, messages ...string) bool {
t.Helper()
if valid, ok := same(expected, actual); !ok {
Failf(t, "%sShould be pointers\n actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
} else if valid {
return Failf(t, "%sShould not be same\n actual: %s", message(messages), print(actual))
}
return true
}
// Length asserts that object have given length.
func Length[S Iterable[any]](t Testing, object S, expected int, messages ...string) bool {
t.Helper()
if actual := length(object); actual != expected {
return Failf(t, "%sShould have element length\n object: %#v\n actual: %d\nexpected: %d", message(messages), object, actual, expected)
}
return true
}
// Contains asserts that object contains given element
//
// Works with strings, arrays, slices, maps values and channels
func Contains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, ok := contains(object, element); !ok {
return Failf(t, "%sShould be iterable\n object: %#v", object)
} else if !found {
return Failf(t, "%sShould contain element\n object: %#v\n element: %#v", message(messages), object, element)
}
return true
}
// NotContains asserts that object do NOT contains given element
//
// Works with strings, arrays, slices, maps values and channels
func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
t.Helper()
if found, ok := contains(object, element); !ok {
Failf(t, "%sShould be iterable\n object: %#v", object)
} else if found {
return Failf(t, "%sShould not contain element\n object: %#v\n element: %#v", message(messages), object, element)
}
return true
}
// Error asserts that error is NOT nil
func Error(t Testing, err error, messages ...string) bool {
t.Helper()
if err == nil {
return Failf(t, "%sShould be error")
}
return true
}
// NoError asserts that error is nil
func NoError(t Testing, err error, messages ...string) bool {
t.Helper()
if err != nil {
return Failf(t, "%sShould not be error\n error: %#v", message(messages), err)
}
return true
}
// ErrorIs asserts that error is unwrappable to given target
func ErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if !errors.Is(err, target) {
return Failf(t, "%sShould be same error\n error: %#v\n target: %#v", message(messages), err, target)
}
return true
}
// NotErrorIs asserts that error is NOT unwrappable to given target
func NotErrorIs(t Testing, err error, target error, messages ...string) bool {
t.Helper()
if errors.Is(err, target) {
return Failf(t, "%sShould not be same error\n error: %#v", message(messages), err, target)
}
return true
}
// EqualJSON asserts that JSON strings are equal
func EqualJSON(t Testing, actual, expected string, messages ...string) bool {
t.Helper()
var actualJSON, expectedJSON any
if err := json.Unmarshal([]byte(actual), &actualJSON); err != nil {
return Failf(t, "%sShould be valid JSON\n actual: %s\n err: %v", message(messages), expected, err)
}
if err := json.Unmarshal([]byte(expected), &expectedJSON); err != nil {
return Failf(t, "%sShould be valid JSON\nexpected: %s\n err: %v", message(messages), expected, err)
}
return Equal(t, actualJSON, expectedJSON)
}
// JSON asserts that object can be marshall to expected JSON string
func JSON(t Testing, actual any, expected string, messages ...string) bool {
t.Helper()
s, err := json.Marshal(actual)
return NoError(t, err, messages...) && EqualJSON(t, string(s), expected, messages...)
}