-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
106 lines (86 loc) · 2.03 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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"reflect"
"testing"
)
// Note: this file contains some assertion utilities.
// assertEqual checks if 2 values are equal.
// Returns successful assertion status.
func assertEqual(t *testing.T, expected any, actual any) bool {
t.Helper()
if !reflect.DeepEqual(expected, actual) {
t.Errorf(
"\n\t"+`expected "%+v" (%T),`+
"\n\t"+`but got "%+v" (%T)`+"\n",
expected, expected,
actual, actual,
)
return false
}
return true
}
// assertNotNil checks if value passed is not nil.
// Returns successful assertion status.
func assertNotNil(t *testing.T, actual any) bool {
t.Helper()
if isNil(actual) {
t.Error("should not be nil")
return false
}
return true
}
// assertNil checks if value passed is nil.
// Returns successful assertion status.
func assertNil(t *testing.T, actual any) bool {
t.Helper()
if !isNil(actual) {
t.Errorf("expected nil, but got %+v", actual)
return false
}
return true
}
// requireNil fails the test immediately if passed value is not nil.
func requireNil(t *testing.T, actual any) {
t.Helper()
if !isNil(actual) {
t.Errorf("expected nil, but got %+v", actual)
t.FailNow()
}
}
// assertTrue checks if value passed is true.
// Returns successful assertion status.
func assertTrue(t *testing.T, actual bool) bool {
t.Helper()
if !actual {
t.Error("should be true")
return false
}
return true
}
// isNil checks an interface if it is nil.
func isNil(object any) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
switch kind {
case reflect.Ptr:
return value.IsNil()
case reflect.Slice:
return value.IsNil()
case reflect.Map:
return value.IsNil()
case reflect.Interface:
return value.IsNil()
case reflect.Func:
return value.IsNil()
case reflect.Chan:
return value.IsNil()
}
return false
}