···11-package test
22-33-import (
44- "errors"
55- "fmt"
66-77- check "gopkg.in/check.v1"
88-)
99-1010-// This check.Checker implementation exists because there's no implementation
1111-// in the library that compares errors using `errors.Is`. If / when the check
1212-// library fixes https://github.com/go-check/check/issues/139, this code can
1313-// likely be removed and replaced with the library implementation.
1414-//
1515-// Added in Go 1.13 [https://go.dev/blog/go1.13-errors] `errors.Is` is the
1616-// best mechanism to use to compare errors that might be wrapped in other
1717-// errors.
1818-type errorIsChecker struct {
1919- *check.CheckerInfo
2020-}
2121-2222-var ErrorIs check.Checker = errorIsChecker{
2323- &check.CheckerInfo{
2424- Name: "ErrorIs",
2525- Params: []string{"obtained", "expected"},
2626- },
2727-}
2828-2929-func (e errorIsChecker) Check(params []interface{}, names []string) (bool, string) {
3030- obtained, ok := params[0].(error)
3131- if !ok {
3232- return false, "obtained is not an error"
3333- }
3434- expected, ok := params[1].(error)
3535- if !ok {
3636- return false, "expected is not an error"
3737- }
3838-3939- if !errors.Is(obtained, expected) {
4040- return false, fmt.Sprintf("obtained: %+v expected: %+v", obtained, expected)
4141- }
4242- return true, ""
4343-}