| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 13 |
) |
| 14 |
|
| 15 |
type tdNil struct { |
| 16 |
baseOKNil |
| 17 |
} |
| 18 |
|
| 19 |
var _ TestDeep = &tdNil{} |
| 20 |
|
| 21 |
// summary(Nil): compares to nil |
| 22 |
// input(Nil): nil,slice,map,ptr,chan,func |
| 23 |
|
| 24 |
// Nil operator checks that data is nil (or is a non-nil interface, |
| 25 |
// but containing a nil pointer.) |
| 26 |
// |
| 27 |
// var got *int |
| 28 |
// td.Cmp(t, got, td.Nil()) // succeeds |
| 29 |
// td.Cmp(t, got, nil) // fails as (*int)(nil) ≠ untyped nil |
| 30 |
// td.Cmp(t, got, (*int)(nil)) // succeeds |
| 31 |
// |
| 32 |
// but: |
| 33 |
// |
| 34 |
// var got fmt.Stringer = (*bytes.Buffer)(nil) |
| 35 |
// td.Cmp(t, got, td.Nil()) // succeeds |
| 36 |
// td.Cmp(t, got, nil) // fails, as the interface is not nil |
| 37 |
// got = nil |
| 38 |
// td.Cmp(t, got, nil) // succeeds |
| 39 |
// |
| 40 |
// See also [Empty], [NotNil] and [Zero]. |
| 41 |
func Nil() TestDeep { |
| 42 |
return &tdNil{ |
| 43 |
baseOKNil: newBaseOKNil(3), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func (n *tdNil) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 48 |
if !got.IsValid() { |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
switch got.Kind() { |
| 53 |
case reflect.Chan, reflect.Func, reflect.Interface, |
| 54 |
reflect.Map, reflect.Ptr, reflect.Slice: |
| 55 |
if got.IsNil() { |
| 56 |
return nil |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ctx.BooleanError { |
| 61 |
return ctxerr.BooleanError |
| 62 |
} |
| 63 |
return ctx.CollectError(&ctxerr.Error{ |
| 64 |
Message: "non-nil", |
| 65 |
Got: got, |
| 66 |
Expected: n, |
| 67 |
}) |
| 68 |
} |
| 69 |
|
| 70 |
func (n *tdNil) String() string { |
| 71 |
return "nil" |
| 72 |
} |
| 73 |
|
| 74 |
type tdNotNil struct { |
| 75 |
baseOKNil |
| 76 |
} |
| 77 |
|
| 78 |
var _ TestDeep = &tdNotNil{} |
| 79 |
|
| 80 |
// summary(NotNil): checks that data is not nil |
| 81 |
// input(NotNil): nil,slice,map,ptr,chan,func |
| 82 |
|
| 83 |
// NotNil operator checks that data is not nil (or is a non-nil |
| 84 |
// interface, containing a non-nil pointer.) |
| 85 |
// |
| 86 |
// got := &Person{} |
| 87 |
// td.Cmp(t, got, td.NotNil()) // succeeds |
| 88 |
// td.Cmp(t, got, td.Not(nil)) // succeeds too, but be careful it is first |
| 89 |
// // because of got type *Person ≠ untyped nil so prefer NotNil() |
| 90 |
// |
| 91 |
// but: |
| 92 |
// |
| 93 |
// var got fmt.Stringer = (*bytes.Buffer)(nil) |
| 94 |
// td.Cmp(t, got, td.NotNil()) // fails |
| 95 |
// td.Cmp(t, got, td.Not(nil)) // succeeds, as the interface is not nil |
| 96 |
// |
| 97 |
// See also [Nil], [NotEmpty] and [NotZero]. |
| 98 |
func NotNil() TestDeep { |
| 99 |
return &tdNotNil{ |
| 100 |
baseOKNil: newBaseOKNil(3), |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func (n *tdNotNil) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 105 |
if got.IsValid() { |
| 106 |
switch got.Kind() { |
| 107 |
case reflect.Chan, reflect.Func, reflect.Interface, |
| 108 |
reflect.Map, reflect.Ptr, reflect.Slice: |
| 109 |
if !got.IsNil() { |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
// All other kinds are non-nil by nature |
| 114 |
default: |
| 115 |
return nil |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if ctx.BooleanError { |
| 120 |
return ctxerr.BooleanError |
| 121 |
} |
| 122 |
return ctx.CollectError(&ctxerr.Error{ |
| 123 |
Message: "nil value", |
| 124 |
Got: got, |
| 125 |
Expected: n, |
| 126 |
}) |
| 127 |
} |
| 128 |
|
| 129 |
func (n *tdNotNil) String() string { |
| 130 |
return "not nil" |
| 131 |
} |