| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
) |
| 9 |
|
| 10 |
// ErrorAs checks that the error is or wraps a specific error type. If so, it |
| 11 |
// assigns it to the provided pointer. This is analogous to calling errors.As. |
| 12 |
// |
| 13 |
// For instance: |
| 14 |
// |
| 15 |
// // Checking for a specific error type |
| 16 |
// c.Assert(err, qt.ErrorAs, new(*os.PathError)) |
| 17 |
// |
| 18 |
// // Checking fields on a specific error type |
| 19 |
// var pathError *os.PathError |
| 20 |
// if c.Check(err, qt.ErrorAs, &pathError) { |
| 21 |
// c.Assert(pathError.Path, qt.Equals, "some_path") |
| 22 |
// } |
| 23 |
var ErrorAs Checker = &errorAsChecker{ |
| 24 |
argNames: []string{"got", "as"}, |
| 25 |
} |
| 26 |
|
| 27 |
type errorAsChecker struct { |
| 28 |
argNames |
| 29 |
} |
| 30 |
|
| 31 |
// Check implements Checker.Check by checking that got is an error whose error |
| 32 |
// chain matches args[0] and assigning it to args[0]. |
| 33 |
func (c *errorAsChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) (err error) { |
| 34 |
if err := checkFirstArgIsError(got, note); err != nil { |
| 35 |
return err |
| 36 |
} |
| 37 |
|
| 38 |
gotErr := got.(error) |
| 39 |
defer func() { |
| 40 |
// A panic is raised when the target is not a pointer to an interface |
| 41 |
// or error. |
| 42 |
if r := recover(); r != nil { |
| 43 |
err = BadCheckf("%s", r) |
| 44 |
} |
| 45 |
}() |
| 46 |
as := args[0] |
| 47 |
if errors.As(gotErr, as) { |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
note("error", Unquoted("wanted type is not found in error chain")) |
| 52 |
note("got", gotErr) |
| 53 |
note("as", Unquoted(fmt.Sprintf("%T", as))) |
| 54 |
return ErrSilent |
| 55 |
} |
| 56 |
|
| 57 |
// ErrorIs checks that the error is or wraps a specific error value. This is |
| 58 |
// analogous to calling errors.Is. |
| 59 |
// |
| 60 |
// For instance: |
| 61 |
// |
| 62 |
// c.Assert(err, qt.ErrorIs, os.ErrNotExist) |
| 63 |
var ErrorIs Checker = &errorIsChecker{ |
| 64 |
argNames: []string{"got", "want"}, |
| 65 |
} |
| 66 |
|
| 67 |
type errorIsChecker struct { |
| 68 |
argNames |
| 69 |
} |
| 70 |
|
| 71 |
// Check implements Checker.Check by checking that got is an error whose error |
| 72 |
// chain matches args[0]. |
| 73 |
func (c *errorIsChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) error { |
| 74 |
if got == nil && args[0] == nil { |
| 75 |
return nil |
| 76 |
} |
| 77 |
if err := checkFirstArgIsError(got, note); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
gotErr := got.(error) |
| 82 |
wantErr, ok := args[0].(error) |
| 83 |
if !ok && args[0] != nil { |
| 84 |
note("want", args[0]) |
| 85 |
return BadCheckf("second argument is not an error") |
| 86 |
} |
| 87 |
|
| 88 |
if !errors.Is(gotErr, wantErr) { |
| 89 |
return errors.New("wanted error is not found in error chain") |
| 90 |
} |
| 91 |
return nil |
| 92 |
} |