| 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_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
"github.com/maxatome/go-testdeep/td" |
| 17 |
) |
| 18 |
|
| 19 |
type errorIsSimpleErr string |
| 20 |
|
| 21 |
func (e errorIsSimpleErr) Error() string { |
| 22 |
return string(e) |
| 23 |
} |
| 24 |
|
| 25 |
type errorIsWrappedErr struct { |
| 26 |
s string |
| 27 |
err error |
| 28 |
} |
| 29 |
|
| 30 |
func (e errorIsWrappedErr) Error() string { |
| 31 |
if e.err != nil { |
| 32 |
return e.s + ": " + e.err.Error() |
| 33 |
} |
| 34 |
return e.s + ": nil" |
| 35 |
} |
| 36 |
|
| 37 |
func (e errorIsWrappedErr) Unwrap() error { |
| 38 |
return e.err |
| 39 |
} |
| 40 |
|
| 41 |
var _ = []error{errorIsSimpleErr(""), errorIsWrappedErr{}} |
| 42 |
|
| 43 |
func TestErrorIs(t *testing.T) { |
| 44 |
insideErr1 := errorIsSimpleErr("failure1") |
| 45 |
insideErr2 := errorIsWrappedErr{"failure2", insideErr1} |
| 46 |
insideErr3 := errorIsWrappedErr{"failure3", insideErr2} |
| 47 |
err := errorIsWrappedErr{"failure4", insideErr3} |
| 48 |
|
| 49 |
checkOK(t, err, td.ErrorIs(err)) |
| 50 |
checkOK(t, err, td.ErrorIs(insideErr3)) |
| 51 |
checkOK(t, err, td.ErrorIs(insideErr2)) |
| 52 |
checkOK(t, err, td.ErrorIs(insideErr1)) |
| 53 |
checkOK(t, nil, td.ErrorIs(nil)) |
| 54 |
|
| 55 |
checkOK(t, err, td.ErrorIs(td.All( |
| 56 |
td.Isa(errorIsSimpleErr("")), |
| 57 |
td.String("failure1"), |
| 58 |
))) |
| 59 |
|
| 60 |
// many errorIsWrappedErr in the err's tree, so only the first |
| 61 |
// encountered matches |
| 62 |
checkOK(t, err, td.ErrorIs(td.All( |
| 63 |
td.Isa(errorIsWrappedErr{}), |
| 64 |
td.HasPrefix("failure4"), |
| 65 |
))) |
| 66 |
|
| 67 |
// HasPrefix().TypeBehind() always returns nil |
| 68 |
// so errors.As() is called with &any, so the toplevel error matches |
| 69 |
checkOK(t, err, td.ErrorIs(td.HasPrefix("failure4"))) |
| 70 |
|
| 71 |
var errNil error |
| 72 |
checkOK(t, &errNil, td.Ptr(td.ErrorIs(nil))) |
| 73 |
|
| 74 |
var inside errorIsSimpleErr |
| 75 |
checkOK(t, err, td.ErrorIs(td.Catch(&inside, td.String("failure1")))) |
| 76 |
test.EqualStr(t, string(inside), "failure1") |
| 77 |
|
| 78 |
checkError(t, nil, td.ErrorIs(insideErr1), |
| 79 |
expectedError{ |
| 80 |
Path: mustBe("DATA"), |
| 81 |
Message: mustBe("nil value"), |
| 82 |
Got: mustBe("nil"), |
| 83 |
Expected: mustBe("anything implementing error interface"), |
| 84 |
}) |
| 85 |
|
| 86 |
checkError(t, 45, td.ErrorIs(insideErr1), |
| 87 |
expectedError{ |
| 88 |
Path: mustBe("DATA"), |
| 89 |
Message: mustBe("int does not implement error interface"), |
| 90 |
Got: mustBe("45"), |
| 91 |
Expected: mustBe("anything implementing error interface"), |
| 92 |
}) |
| 93 |
|
| 94 |
checkError(t, 45, td.ErrorIs(fmt.Errorf("another")), |
| 95 |
expectedError{ |
| 96 |
Path: mustBe("DATA"), |
| 97 |
Message: mustBe("int does not implement error interface"), |
| 98 |
Got: mustBe("45"), |
| 99 |
Expected: mustBe("anything implementing error interface"), |
| 100 |
}) |
| 101 |
|
| 102 |
checkError(t, err, td.ErrorIs(fmt.Errorf("another")), |
| 103 |
expectedError{ |
| 104 |
Path: mustBe("DATA"), |
| 105 |
Message: mustBe("is not found in err's tree"), |
| 106 |
Got: mustBe(`(td_test.errorIsWrappedErr) "failure4: failure3: failure2: failure1"`), |
| 107 |
Expected: mustBe(`(*errors.errorString) "another"`), |
| 108 |
}) |
| 109 |
|
| 110 |
checkError(t, err, td.ErrorIs(td.String("nonono")), |
| 111 |
expectedError{ |
| 112 |
Path: mustBe("DATA.ErrorIs(interface {})"), |
| 113 |
Message: mustBe("does not match"), |
| 114 |
Got: mustBe(`"failure4: failure3: failure2: failure1"`), |
| 115 |
Expected: mustBe(`"nonono"`), |
| 116 |
}) |
| 117 |
|
| 118 |
checkError(t, err, td.ErrorIs(td.Isa(fmt.Errorf("another"))), |
| 119 |
expectedError{ |
| 120 |
Path: mustBe("DATA"), |
| 121 |
Message: mustBe("type is not found in err's tree"), |
| 122 |
Got: mustBe(`(td_test.errorIsWrappedErr) failure4: failure3: failure2: failure1`), |
| 123 |
Expected: mustBe(`*errors.errorString`), |
| 124 |
}) |
| 125 |
|
| 126 |
checkError(t, err, td.ErrorIs(td.Smuggle(io.ReadAll, td.String("xx"))), |
| 127 |
expectedError{ |
| 128 |
Path: mustBe("DATA"), |
| 129 |
Message: mustBe("type is not found in err's tree"), |
| 130 |
Got: mustBe(`(td_test.errorIsWrappedErr) failure4: failure3: failure2: failure1`), |
| 131 |
Expected: mustBe(`io.Reader`), |
| 132 |
}) |
| 133 |
|
| 134 |
checkError(t, err, td.ErrorIs(nil), |
| 135 |
expectedError{ |
| 136 |
Path: mustBe("DATA"), |
| 137 |
Message: mustBe("is not nil"), |
| 138 |
Got: mustBe(`(td_test.errorIsWrappedErr) "failure4: failure3: failure2: failure1"`), |
| 139 |
Expected: mustBe(`nil`), |
| 140 |
}) |
| 141 |
|
| 142 |
// As errors.Is, it does not match |
| 143 |
checkError(t, errorIsWrappedErr{"failure", nil}, td.ErrorIs(nil), |
| 144 |
expectedError{ |
| 145 |
Path: mustBe("DATA"), |
| 146 |
Message: mustBe("is not nil"), |
| 147 |
Got: mustBe(`(td_test.errorIsWrappedErr) "failure: nil"`), |
| 148 |
Expected: mustBe(`nil`), |
| 149 |
}) |
| 150 |
|
| 151 |
checkError(t, err, td.ErrorIs(td.Gt(0)), |
| 152 |
expectedError{ |
| 153 |
Path: mustBe("DATA"), |
| 154 |
Message: mustBe("bad usage of ErrorIs operator"), |
| 155 |
Summary: mustBe(`ErrorIs(Gt): type int behind Gt operator is not an interface or does not implement error`), |
| 156 |
}) |
| 157 |
|
| 158 |
type private struct{ err error } |
| 159 |
got := private{err: err} |
| 160 |
for _, expErr := range []error{err, insideErr3} { |
| 161 |
expected := td.Struct(private{}, td.StructFields{"err": td.ErrorIs(expErr)}) |
| 162 |
if dark.UnsafeDisabled { |
| 163 |
checkError(t, got, expected, |
| 164 |
expectedError{ |
| 165 |
Message: mustBe("cannot compare"), |
| 166 |
Path: mustBe("DATA.err"), |
| 167 |
Summary: mustBe("unexported field that cannot be overridden"), |
| 168 |
}) |
| 169 |
} else { |
| 170 |
checkOK(t, got, expected) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if !dark.UnsafeDisabled { |
| 175 |
got = private{} |
| 176 |
checkOK(t, got, td.Struct(private{}, td.StructFields{"err": td.ErrorIs(nil)})) |
| 177 |
} |
| 178 |
|
| 179 |
// |
| 180 |
// String |
| 181 |
test.EqualStr(t, td.ErrorIs(insideErr1).String(), "ErrorIs(failure1)") |
| 182 |
test.EqualStr(t, td.ErrorIs(nil).String(), "ErrorIs(nil)") |
| 183 |
test.EqualStr(t, td.ErrorIs(td.HasPrefix("pipo")).String(), |
| 184 |
`ErrorIs(HasPrefix("pipo"))`) |
| 185 |
test.EqualStr(t, td.ErrorIs(12).String(), "ErrorIs(<ERROR>)") |
| 186 |
} |
| 187 |
|
| 188 |
func TestErrorIsTypeBehind(t *testing.T) { |
| 189 |
equalTypes(t, td.ErrorIs(fmt.Errorf("another")), nil) |
| 190 |
} |