Debian Codegraph @

internal/test/checkers.go

raw
1 package test
2
3 import (
4 "errors"
5 "fmt"
6
7 check "gopkg.in/check.v1"
8 )
9
10 // This check.Checker implementation exists because there's no implementation
11 // in the library that compares errors using `errors.Is`. If / when the check
12 // library fixes https://github.com/go-check/check/issues/139, this code can
13 // likely be removed and replaced with the library implementation.
14 //
15 // Added in Go 1.13 [https://go.dev/blog/go1.13-errors] `errors.Is` is the
16 // best mechanism to use to compare errors that might be wrapped in other
17 // errors.
18 type errorIsChecker struct {
19 *check.CheckerInfo
20 }
21
22 var ErrorIs check.Checker = errorIsChecker{
23 &check.CheckerInfo{
24 Name: "ErrorIs",
25 Params: []string{"obtained", "expected"},
26 },
27 }
28
29 func (e errorIsChecker) Check(params []interface{}, names []string) (bool, string) {
30 obtained, ok := params[0].(error)
31 if !ok {
32 return false, "obtained is not an error"
33 }
34 expected, ok := params[1].(error)
35 if !ok {
36 return false, "expected is not an error"
37 }
38
39 if !errors.Is(obtained, expected) {
40 return false, fmt.Sprintf("obtained: %+v expected: %+v", obtained, expected)
41 }
42 return true, ""
43 }