• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gravitton / assert / 24771305055

22 Apr 2026 09:37AM UTC coverage: 94.595% (+0.9%) from 93.697%
24771305055

push

github

tomas-novotny
Fix tests

280 of 296 relevant lines covered (94.59%)

13.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.71
/assert.go
1
package assert
2

3
import (
4
        "encoding/json"
5
        "errors"
6
        "regexp"
7
)
8

9
// Testing is an interface wrapper around *testing.T
10
type Testing interface {
11
        Helper()
12
        Errorf(format string, args ...any)
13
}
14

15
// Fail reports a failure message via t.Errorf.
16
func Fail(t Testing, message string) bool {
×
17
        t.Helper()
×
18

×
19
        t.Errorf("%s", message)
×
20

×
21
        return false
×
22
}
×
23

24
// Failf reports a formatted failure message via t.Errorf.
25
func Failf(t Testing, format string, args ...any) bool {
97✔
26
        t.Helper()
97✔
27

97✔
28
        t.Errorf(format, args...)
97✔
29

97✔
30
        return false
97✔
31
}
97✔
32

33
// True asserts that the specified value is true.
34
func True(t Testing, condition bool, messages ...string) bool {
2✔
35
        t.Helper()
2✔
36

2✔
37
        if !condition {
3✔
38
                return Failf(t, "%sShould be true", message(messages))
1✔
39
        }
1✔
40

41
        return true
1✔
42
}
43

44
// False asserts that the specified value is false.
45
func False(t Testing, condition bool, messages ...string) bool {
2✔
46
        t.Helper()
2✔
47

2✔
48
        if condition {
3✔
49
                return Failf(t, "%sShould be false", message(messages))
1✔
50
        }
1✔
51

52
        return true
1✔
53
}
54

55
// Same asserts that two pointers reference the same object.
56
//
57
// Both arguments must be pointer variables. Pointer variable equality is
58
// determined based on the equality of both type and value.
59
func Same[T Reference](t Testing, actual, expected T, messages ...string) bool {
13✔
60
        t.Helper()
13✔
61

13✔
62
        if valid, ok := same[T](expected, actual); !ok {
16✔
63
                return Failf(t, "%sShould be pointers\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
3✔
64
        } else if !valid {
17✔
65
                return Failf(t, "%sShould be same\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
4✔
66
        }
4✔
67

68
        return true
6✔
69
}
70

71
// NotSame asserts that two pointers do NOT reference the same object.
72
//
73
// Both arguments must be pointer variables. Pointer variable equality is
74
// determined based on the equality of both type and value.
75
func NotSame[T Reference](t Testing, actual, expected T, messages ...string) bool {
13✔
76
        t.Helper()
13✔
77

13✔
78
        if valid, ok := same(expected, actual); !ok {
16✔
79
                Failf(t, "%sShould be pointers\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
3✔
80
        } else if valid {
19✔
81
                return Failf(t, "%sShould not be same\n  actual: %s", message(messages), print(actual))
6✔
82
        }
6✔
83

84
        return true
7✔
85
}
86

87
// Equal asserts that two objects are equal.
88
//
89
// Pointer variable equality is determined based on the equality of the
90
// referenced values (as opposed to the memory addresses).
91
func Equal[T Comparable](t Testing, actual, expected T, messages ...string) bool {
33✔
92
        t.Helper()
33✔
93

33✔
94
        if !equal(actual, expected) {
41✔
95
                return Failf(t, "%sShould be equal:\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
8✔
96
        }
8✔
97

98
        return true
25✔
99
}
100

101
// NotEqual asserts that the specified values are NOT equal.
102
//
103
// Pointer variable equality is determined based on the equality of the
104
// referenced values (as opposed to the memory addresses).
105
func NotEqual[T Comparable](t Testing, actual, expected T, messages ...string) bool {
24✔
106
        t.Helper()
24✔
107

24✔
108
        if equal(actual, expected) {
42✔
109
                return Failf(t, "%sShould not be equal\n  actual: %s", message(messages), print(actual))
18✔
110
        }
18✔
111

112
        return true
6✔
113
}
114

115
// EqualDelta asserts that two numeric values difference is less than delta.
116
//
117
// Panics if delta is negative.
118
func EqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
14✔
119
        t.Helper()
14✔
120

14✔
121
        if !equalDelta(actual, expected, delta) {
20✔
122
                return Failf(t, "%sShould be equal in delta:\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
6✔
123
        }
6✔
124

125
        return true
8✔
126
}
127

128
// NotEqualDelta asserts that two numeric values difference is greater than delta.
129
//
130
// Panics if delta is negative.
131
func NotEqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...string) bool {
14✔
132
        t.Helper()
14✔
133

14✔
134
        if equalDelta(actual, expected, delta) {
22✔
135
                return Failf(t, "%sShould not be equal in delta:\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
8✔
136
        }
8✔
137

138
        return true
6✔
139
}
140

141
// Greater asserts that actual is greater than expected.
142
func Greater[T Numeric](t Testing, actual, expected T, messages ...string) bool {
7✔
143
        t.Helper()
7✔
144

7✔
145
        if actual <= expected {
10✔
146
                return Failf(t, "%sShould be greater\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
3✔
147
        }
3✔
148

149
        return true
4✔
150
}
151

152
// GreaterOrEqual asserts that actual is greater than or equal to expected.
153
func GreaterOrEqual[T Numeric](t Testing, actual, expected T, messages ...string) bool {
6✔
154
        t.Helper()
6✔
155

6✔
156
        if actual < expected {
8✔
157
                return Failf(t, "%sShould be greater or equal\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
2✔
158
        }
2✔
159

160
        return true
4✔
161
}
162

163
// Less asserts that actual is less than expected.
164
func Less[T Numeric](t Testing, actual, expected T, messages ...string) bool {
7✔
165
        t.Helper()
7✔
166

7✔
167
        if actual >= expected {
10✔
168
                return Failf(t, "%sShould be less\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
3✔
169
        }
3✔
170

171
        return true
4✔
172
}
173

174
// LessOrEqual asserts that actual is less than or equal to expected.
175
func LessOrEqual[T Numeric](t Testing, actual, expected T, messages ...string) bool {
6✔
176
        t.Helper()
6✔
177

6✔
178
        if actual > expected {
8✔
179
                return Failf(t, "%sShould be less or equal\n  actual: %s\nexpected: %s", message(messages), print(actual), print(expected))
2✔
180
        }
2✔
181

182
        return true
4✔
183
}
184

185
// Length asserts that object has given length.
186
func Length[S Iterable[any]](t Testing, object S, expected int, messages ...string) bool {
5✔
187
        t.Helper()
5✔
188

5✔
189
        if actual := length(object); actual != expected {
6✔
190
                return Failf(t, "%sShould have element length\n  object: %#v\n  actual: %d\nexpected: %d", message(messages), object, actual, expected)
1✔
191
        }
1✔
192

193
        return true
4✔
194
}
195

196
// Empty asserts that object has zero length.
197
func Empty[S Iterable[any]](t Testing, object S, messages ...string) bool {
6✔
198
        t.Helper()
6✔
199

6✔
200
        if length(object) != 0 {
9✔
201
                return Failf(t, "%sShould be empty\n  object: %#v", message(messages), object)
3✔
202
        }
3✔
203

204
        return true
3✔
205
}
206

207
// NotEmpty asserts that object has non-zero length.
208
func NotEmpty[S Iterable[any]](t Testing, object S, messages ...string) bool {
6✔
209
        t.Helper()
6✔
210

6✔
211
        if length(object) == 0 {
9✔
212
                return Failf(t, "%sShould not be empty\n  object: %#v", message(messages), object)
3✔
213
        }
3✔
214

215
        return true
3✔
216
}
217

218
// Contains asserts that object contains given element.
219
//
220
// Works with strings, arrays, slices, maps values and channels.
221
func Contains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
7✔
222
        t.Helper()
7✔
223

7✔
224
        if found, ok := contains(object, element); !ok {
8✔
225
                return Failf(t, "%sShould be iterable\n  object: %#v", message(messages), object)
1✔
226
        } else if !found {
10✔
227
                return Failf(t, "%sShould contain element\n  object: %#v\n element: %#v", message(messages), object, element)
3✔
228
        }
3✔
229

230
        return true
3✔
231
}
232

233
// NotContains asserts that object does NOT contain given element.
234
//
235
// Works with strings, arrays, slices, maps values and channels.
236
func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E, messages ...string) bool {
7✔
237
        t.Helper()
7✔
238

7✔
239
        if found, ok := contains(object, element); !ok {
8✔
240
                Failf(t, "%sShould be iterable\n  object: %#v", message(messages), object)
1✔
241
        } else if found {
10✔
242
                return Failf(t, "%sShould not contain element\n  object: %#v\n element: %#v", message(messages), object, element)
3✔
243
        }
3✔
244

245
        return true
4✔
246
}
247

248
// Error asserts that error is NOT nil.
249
//
250
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
251
func Error(t Testing, err error, messages ...string) bool {
3✔
252
        t.Helper()
3✔
253

3✔
254
        if isNilError(err) {
5✔
255
                return Failf(t, "%sShould be error", message(messages))
2✔
256
        }
2✔
257

258
        return true
1✔
259
}
260

261
// NoError asserts that error is nil.
262
//
263
// A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
264
func NoError(t Testing, err error, messages ...string) bool {
6✔
265
        t.Helper()
6✔
266

6✔
267
        if !isNilError(err) {
7✔
268
                return Failf(t, "%sShould not be error\n   error: %#v", message(messages), err)
1✔
269
        }
1✔
270

271
        return true
5✔
272
}
273

274
// ErrorIs asserts that error is unwrappable to given target.
275
func ErrorIs(t Testing, err error, target error, messages ...string) bool {
4✔
276
        t.Helper()
4✔
277

4✔
278
        if !errors.Is(err, target) {
5✔
279
                return Failf(t, "%sShould be same error\n   error: %#v\n  target: %#v", message(messages), err, target)
1✔
280
        }
1✔
281

282
        return true
3✔
283
}
284

285
// NotErrorIs asserts that error is NOT unwrappable to given target.
286
func NotErrorIs(t Testing, err error, target error, messages ...string) bool {
4✔
287
        t.Helper()
4✔
288

4✔
289
        if errors.Is(err, target) {
7✔
290
                return Failf(t, "%sShould not be same error\n   error: %#v\n  target: %#v", message(messages), err, target)
3✔
291
        }
3✔
292

293
        return true
1✔
294
}
295

296
// Matches asserts that a string matches the given regular expression.
297
func Matches(t Testing, actual, pattern string, messages ...string) bool {
5✔
298
        t.Helper()
5✔
299

5✔
300
        re, err := regexp.Compile(pattern)
5✔
301
        if err != nil {
6✔
302
                return Failf(t, "%sShould be valid regexp\n pattern: %s\n     err: %v", message(messages), pattern, err)
1✔
303
        }
1✔
304

305
        if !re.MatchString(actual) {
5✔
306
                return Failf(t, "%sShould match regexp\n  actual: %s\n pattern: %s", message(messages), actual, pattern)
1✔
307
        }
1✔
308

309
        return true
3✔
310
}
311

312
// NotMatches asserts that a string does NOT match the given regular expression.
313
func NotMatches(t Testing, actual, pattern string, messages ...string) bool {
4✔
314
        t.Helper()
4✔
315

4✔
316
        re, err := regexp.Compile(pattern)
4✔
317
        if err != nil {
4✔
318
                return Failf(t, "%sShould be valid regexp\n pattern: %s\n     err: %v", message(messages), pattern, err)
×
319
        }
×
320

321
        if re.MatchString(actual) {
7✔
322
                return Failf(t, "%sShould not match regexp\n  actual: %s\n pattern: %s", message(messages), actual, pattern)
3✔
323
        }
3✔
324

325
        return true
1✔
326
}
327

328
// EqualJSON asserts that JSON strings are equal.
329
func EqualJSON(t Testing, actual, expected string, messages ...string) bool {
11✔
330
        t.Helper()
11✔
331

11✔
332
        var actualJSON, expectedJSON any
11✔
333

11✔
334
        if err := json.Unmarshal([]byte(actual), &actualJSON); err != nil {
12✔
335
                return Failf(t, "%sShould be valid JSON\n  actual: %s\n     err: %v", message(messages), actual, err)
1✔
336
        }
1✔
337

338
        if err := json.Unmarshal([]byte(expected), &expectedJSON); err != nil {
11✔
339
                return Failf(t, "%sShould be valid JSON\nexpected: %s\n     err: %v", message(messages), expected, err)
1✔
340
        }
1✔
341

342
        return Equal(t, actualJSON, expectedJSON)
9✔
343
}
344

345
// JSON asserts that object can be marshaled to expected JSON string.
346
func JSON(t Testing, actual any, expected string, messages ...string) bool {
3✔
347
        t.Helper()
3✔
348

3✔
349
        s, err := json.Marshal(actual)
3✔
350

3✔
351
        return NoError(t, err, messages...) && EqualJSON(t, string(s), expected, messages...)
3✔
352
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc