| 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 tdZero struct { |
| 16 |
baseOKNil |
| 17 |
} |
| 18 |
|
| 19 |
var _ TestDeep = &tdZero{} |
| 20 |
|
| 21 |
// summary(Zero): checks data against its zero'ed conterpart |
| 22 |
// input(Zero): all |
| 23 |
|
| 24 |
// Zero operator checks that data is zero regarding its type. |
| 25 |
// |
| 26 |
// - nil is the zero value of pointers, maps, slices, channels and functions; |
| 27 |
// - 0 is the zero value of numbers; |
| 28 |
// - "" is the 0 value of strings; |
| 29 |
// - false is the zero value of booleans; |
| 30 |
// - zero value of structs is the struct with no fields initialized. |
| 31 |
// |
| 32 |
// Beware that: |
| 33 |
// |
| 34 |
// td.Cmp(t, AnyStruct{}, td.Zero()) // is true |
| 35 |
// td.Cmp(t, &AnyStruct{}, td.Zero()) // is false, coz pointer ≠ nil |
| 36 |
// td.Cmp(t, &AnyStruct{}, td.Ptr(td.Zero())) // is true |
| 37 |
// |
| 38 |
// See also [Empty], [Nil] and [NotZero]. |
| 39 |
func Zero() TestDeep { |
| 40 |
return &tdZero{ |
| 41 |
baseOKNil: newBaseOKNil(3), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func (z *tdZero) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) { |
| 46 |
// nil case |
| 47 |
if !got.IsValid() { |
| 48 |
return nil |
| 49 |
} |
| 50 |
return deepValueEqual(ctx, got, reflect.New(got.Type()).Elem()) |
| 51 |
} |
| 52 |
|
| 53 |
func (z *tdZero) String() string { |
| 54 |
return "Zero()" |
| 55 |
} |
| 56 |
|
| 57 |
type tdNotZero struct { |
| 58 |
baseOKNil |
| 59 |
} |
| 60 |
|
| 61 |
var _ TestDeep = &tdNotZero{} |
| 62 |
|
| 63 |
// summary(NotZero): checks that data is not zero regarding its type |
| 64 |
// input(NotZero): all |
| 65 |
|
| 66 |
// NotZero operator checks that data is not zero regarding its type. |
| 67 |
// |
| 68 |
// - nil is the zero value of pointers, maps, slices, channels and functions; |
| 69 |
// - 0 is the zero value of numbers; |
| 70 |
// - "" is the 0 value of strings; |
| 71 |
// - false is the zero value of booleans; |
| 72 |
// - zero value of structs is the struct with no fields initialized. |
| 73 |
// |
| 74 |
// Beware that: |
| 75 |
// |
| 76 |
// td.Cmp(t, AnyStruct{}, td.NotZero()) // is false |
| 77 |
// td.Cmp(t, &AnyStruct{}, td.NotZero()) // is true, coz pointer ≠ nil |
| 78 |
// td.Cmp(t, &AnyStruct{}, td.Ptr(td.NotZero())) // is false |
| 79 |
// |
| 80 |
// See also [NotEmpty], [NotNil] and [Zero]. |
| 81 |
func NotZero() TestDeep { |
| 82 |
return &tdNotZero{ |
| 83 |
baseOKNil: newBaseOKNil(3), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (z *tdNotZero) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) { |
| 88 |
if got.IsValid() && !deepValueEqualOK(got, reflect.New(got.Type()).Elem()) { |
| 89 |
return nil |
| 90 |
} |
| 91 |
if ctx.BooleanError { |
| 92 |
return ctxerr.BooleanError |
| 93 |
} |
| 94 |
return ctx.CollectError(&ctxerr.Error{ |
| 95 |
Message: "zero value", |
| 96 |
Got: got, |
| 97 |
Expected: z, |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
func (z *tdNotZero) String() string { |
| 102 |
return "NotZero()" |
| 103 |
} |