-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_nil.go
63 lines (51 loc) · 1.31 KB
/
assert_nil.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
package actually
import (
"reflect"
"testing"
)
// Nil asserts that a test data you got is <nil>
/*
actually.Got(a).Nil(t) // If `a` is <nil>, then pass.
*/
func (a *testingA) Nil(t *testing.T, testNames ...string) *testingA {
invalidCall(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()
if !a.isNil() {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectNilButNotNil)
}
return a
}
// NotNil asserts that a test data you got is NOT <nil>
/*
actually.Got(a).NotNil(t) // If `a` is NOT <nil>, then pass.
*/
func (a *testingA) NotNil(t *testing.T, testNames ...string) *testingA {
invalidCall(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()
if a.isNil() {
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectIsNotNil)
}
return a
}
func (a *testingA) isNil() bool {
if a.got == nil {
return true
}
return isSpecialNil(a.got)
}
func isSpecialNil(gotv any) bool {
v := reflect.ValueOf(gotv)
k := v.Kind()
return isSpecialKind(k) && v.IsNil()
}
func isSpecialKind(k reflect.Kind) bool {
// Special Kind is either one: Chan || Func || Interface || Map || Pointer || Slice || UnsafePointer
// See https://github.com/golang/go/blob/8d68b388d4d1debec8d349adac58dd9f1cb03d25/src/reflect/type.go#L262-L267
return (k >= reflect.Chan && k <= reflect.Slice) || k == reflect.UnsafePointer
}