| 1 |
// Copyright (c) 2022, 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 |
"errors" |
| 11 |
"fmt" |
| 12 |
"reflect" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 16 |
"github.com/maxatome/go-testdeep/internal/types" |
| 17 |
) |
| 18 |
|
| 19 |
type tdErrorIs struct { |
| 20 |
tdSmugglerBase |
| 21 |
typeBehind reflect.Type |
| 22 |
} |
| 23 |
|
| 24 |
var _ TestDeep = &tdErrorIs{} |
| 25 |
|
| 26 |
func errorToRawString(err error) types.RawString { |
| 27 |
if err == nil { |
| 28 |
return "nil" |
| 29 |
} |
| 30 |
return types.RawString(fmt.Sprintf("(%[1]T) %[1]q", err)) |
| 31 |
} |
| 32 |
|
| 33 |
// summary(ErrorIs): checks the data is an error and matches a wrapped error |
| 34 |
// input(ErrorIs): if(error) |
| 35 |
|
| 36 |
// ErrorIs is a smuggler operator. It reports whether any error in an |
| 37 |
// error's chain matches expectedError. |
| 38 |
// |
| 39 |
// _, err := os.Open("/unknown/file") |
| 40 |
// td.Cmp(t, err, os.ErrNotExist) // fails |
| 41 |
// td.Cmp(t, err, td.ErrorIs(os.ErrNotExist)) // succeeds |
| 42 |
// |
| 43 |
// err1 := fmt.Errorf("failure1") |
| 44 |
// err2 := fmt.Errorf("failure2: %w", err1) |
| 45 |
// err3 := fmt.Errorf("failure3: %w", err2) |
| 46 |
// err := fmt.Errorf("failure4: %w", err3) |
| 47 |
// td.Cmp(t, err, td.ErrorIs(err)) // succeeds |
| 48 |
// td.Cmp(t, err, td.ErrorIs(err1)) // succeeds |
| 49 |
// td.Cmp(t, err1, td.ErrorIs(err)) // fails |
| 50 |
// |
| 51 |
// var cerr myError |
| 52 |
// td.Cmp(t, err, td.ErrorIs(td.Catch(&cerr, td.String("my error...")))) |
| 53 |
// |
| 54 |
// td.Cmp(t, err, td.ErrorIs(td.All( |
| 55 |
// td.Isa(myError{}), |
| 56 |
// td.String("my error..."), |
| 57 |
// ))) |
| 58 |
// |
| 59 |
// Behind the scene it uses [errors.Is] function if expectedError is |
| 60 |
// an error and [errors.As] function if expectedError is a [TestDeep] |
| 61 |
// operator. |
| 62 |
// |
| 63 |
// Note that like [errors.Is], expectedError can be nil: in this case |
| 64 |
// the comparison succeeds only when got is nil too. |
| 65 |
// |
| 66 |
// See also [CmpError] and [CmpNoError]. |
| 67 |
func ErrorIs(expectedError any) TestDeep { |
| 68 |
e := tdErrorIs{ |
| 69 |
tdSmugglerBase: newSmugglerBase(expectedError), |
| 70 |
} |
| 71 |
|
| 72 |
switch expErr := expectedError.(type) { |
| 73 |
case nil: |
| 74 |
case error: |
| 75 |
e.expectedValue = reflect.ValueOf(expectedError) |
| 76 |
case TestDeep: |
| 77 |
e.typeBehind = expErr.TypeBehind() |
| 78 |
if e.typeBehind == nil { |
| 79 |
e.typeBehind = types.Interface |
| 80 |
break |
| 81 |
} |
| 82 |
if !e.typeBehind.Implements(types.Error) && |
| 83 |
e.typeBehind.Kind() != reflect.Interface { |
| 84 |
e.err = ctxerr.OpBad("ErrorIs", |
| 85 |
"ErrorIs(%[1]s): type %[2]s behind %[1]s operator is not an interface or does not implement error", |
| 86 |
expErr.GetLocation().Func, e.typeBehind) |
| 87 |
} |
| 88 |
default: |
| 89 |
e.err = ctxerr.OpBadUsage("ErrorIs", |
| 90 |
"(error|TESTDEEP_OPERATOR)", expectedError, 1, false) |
| 91 |
} |
| 92 |
|
| 93 |
return &e |
| 94 |
} |
| 95 |
|
| 96 |
func (e *tdErrorIs) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 97 |
if e.err != nil { |
| 98 |
return ctx.CollectError(e.err) |
| 99 |
} |
| 100 |
|
| 101 |
// nil case |
| 102 |
if !got.IsValid() { |
| 103 |
// Special case |
| 104 |
if !e.expectedValue.IsValid() { |
| 105 |
return nil |
| 106 |
} |
| 107 |
if ctx.BooleanError { |
| 108 |
return ctxerr.BooleanError |
| 109 |
} |
| 110 |
return ctx.CollectError(&ctxerr.Error{ |
| 111 |
Message: "nil value", |
| 112 |
Got: types.RawString("nil"), |
| 113 |
Expected: types.RawString("anything implementing error interface"), |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
gotIf, ok := dark.GetInterface(got, true) |
| 118 |
if !ok { |
| 119 |
return ctx.CollectError(ctx.CannotCompareError()) |
| 120 |
} |
| 121 |
|
| 122 |
gotErr, ok := gotIf.(error) |
| 123 |
if !ok { |
| 124 |
if ctx.BooleanError { |
| 125 |
return ctxerr.BooleanError |
| 126 |
} |
| 127 |
return ctx.CollectError(&ctxerr.Error{ |
| 128 |
Message: got.Type().String() + " does not implement error interface", |
| 129 |
Got: gotIf, |
| 130 |
Expected: types.RawString("anything implementing error interface"), |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
if e.isTestDeeper { |
| 135 |
target := reflect.New(e.typeBehind) |
| 136 |
|
| 137 |
if !errors.As(gotErr, target.Interface()) { |
| 138 |
if ctx.BooleanError { |
| 139 |
return ctxerr.BooleanError |
| 140 |
} |
| 141 |
return ctx.CollectError(&ctxerr.Error{ |
| 142 |
Message: "type is not found in err's tree", |
| 143 |
Got: gotIf, |
| 144 |
Expected: types.RawString(e.typeBehind.String()), |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
return deepValueEqual(ctx.AddCustomLevel(S(".ErrorIs(%s)", e.typeBehind)), |
| 149 |
target.Elem(), e.expectedValue) |
| 150 |
} |
| 151 |
|
| 152 |
var expErr error |
| 153 |
if e.expectedValue.IsValid() { |
| 154 |
expErr = e.expectedValue.Interface().(error) |
| 155 |
if errors.Is(gotErr, expErr) { |
| 156 |
return nil |
| 157 |
} |
| 158 |
if ctx.BooleanError { |
| 159 |
return ctxerr.BooleanError |
| 160 |
} |
| 161 |
return ctx.CollectError(&ctxerr.Error{ |
| 162 |
Message: "is not found in err's tree", |
| 163 |
Got: errorToRawString(gotErr), |
| 164 |
Expected: errorToRawString(expErr), |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
if ctx.BooleanError { |
| 169 |
return ctxerr.BooleanError |
| 170 |
} |
| 171 |
return ctx.CollectError(&ctxerr.Error{ |
| 172 |
Message: "is not nil", |
| 173 |
Got: errorToRawString(gotErr), |
| 174 |
Expected: errorToRawString(expErr), |
| 175 |
}) |
| 176 |
} |
| 177 |
|
| 178 |
func (e *tdErrorIs) String() string { |
| 179 |
if e.err != nil { |
| 180 |
return e.stringError() |
| 181 |
} |
| 182 |
if e.isTestDeeper { |
| 183 |
return "ErrorIs(" + e.expectedValue.Interface().(TestDeep).String() + ")" |
| 184 |
} |
| 185 |
if !e.expectedValue.IsValid() { |
| 186 |
return "ErrorIs(nil)" |
| 187 |
} |
| 188 |
return "ErrorIs(" + e.expectedValue.Interface().(error).Error() + ")" |
| 189 |
} |
| 190 |
|
| 191 |
func (e *tdErrorIs) HandleInvalid() bool { |
| 192 |
return true |
| 193 |
} |