/
td_nil.go
131 lines (112 loc) · 2.79 KB
/
td_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
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
// Copyright (c) 2018, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package td
import (
"reflect"
"github.com/maxatome/go-testdeep/internal/ctxerr"
)
type tdNil struct {
baseOKNil
}
var _ TestDeep = &tdNil{}
// summary(Nil): compares to nil
// input(Nil): nil,slice,map,ptr,chan,func
// Nil operator checks that data is nil (or is a non-nil interface,
// but containing a nil pointer.)
//
// var got *int
// td.Cmp(t, got, td.Nil()) // succeeds
// td.Cmp(t, got, nil) // fails as (*int)(nil) ≠ untyped nil
// td.Cmp(t, got, (*int)(nil)) // succeeds
//
// but:
//
// var got fmt.Stringer = (*bytes.Buffer)(nil)
// td.Cmp(t, got, td.Nil()) // succeeds
// td.Cmp(t, got, nil) // fails, as the interface is not nil
// got = nil
// td.Cmp(t, got, nil) // succeeds
//
// See also [Empty], [NotNil] and [Zero].
func Nil() TestDeep {
return &tdNil{
baseOKNil: newBaseOKNil(3),
}
}
func (n *tdNil) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
if !got.IsValid() {
return nil
}
switch got.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface,
reflect.Map, reflect.Ptr, reflect.Slice:
if got.IsNil() {
return nil
}
}
if ctx.BooleanError {
return ctxerr.BooleanError
}
return ctx.CollectError(&ctxerr.Error{
Message: "non-nil",
Got: got,
Expected: n,
})
}
func (n *tdNil) String() string {
return "nil"
}
type tdNotNil struct {
baseOKNil
}
var _ TestDeep = &tdNotNil{}
// summary(NotNil): checks that data is not nil
// input(NotNil): nil,slice,map,ptr,chan,func
// NotNil operator checks that data is not nil (or is a non-nil
// interface, containing a non-nil pointer.)
//
// got := &Person{}
// td.Cmp(t, got, td.NotNil()) // succeeds
// td.Cmp(t, got, td.Not(nil)) // succeeds too, but be careful it is first
// // because of got type *Person ≠ untyped nil so prefer NotNil()
//
// but:
//
// var got fmt.Stringer = (*bytes.Buffer)(nil)
// td.Cmp(t, got, td.NotNil()) // fails
// td.Cmp(t, got, td.Not(nil)) // succeeds, as the interface is not nil
//
// See also [Nil], [NotEmpty] and [NotZero].
func NotNil() TestDeep {
return &tdNotNil{
baseOKNil: newBaseOKNil(3),
}
}
func (n *tdNotNil) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
if got.IsValid() {
switch got.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface,
reflect.Map, reflect.Ptr, reflect.Slice:
if !got.IsNil() {
return nil
}
// All other kinds are non-nil by nature
default:
return nil
}
}
if ctx.BooleanError {
return ctxerr.BooleanError
}
return ctx.CollectError(&ctxerr.Error{
Message: "nil value",
Got: got,
Expected: n,
})
}
func (n *tdNotNil) String() string {
return "not nil"
}