-
Notifications
You must be signed in to change notification settings - Fork 1
/
errs_test.go
104 lines (99 loc) · 2.05 KB
/
errs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package errs
import (
"errors"
"fmt"
"testing"
)
func ExampleError() {
const err = UnauthenticatedError("an unauthenticated error")
fmt.Println(err.Kind() == KindUnauthenticated)
// Output: true
}
func TestError(t *testing.T) {
tt := []struct {
name string
err error
want Kind
}{
{
name: "const_unauthenticated",
err: Unauthenticated,
want: KindUnauthenticated,
},
{
name: "unauthenticated",
err: UnauthenticatedError("an unauthenticated error"),
want: KindUnauthenticated,
},
{
name: "const_invalid_argument",
err: InvalidArgument,
want: KindInvalidArgument,
},
{
name: "invalid_argument",
err: InvalidArgumentError("an invalid argument error"),
want: KindInvalidArgument,
},
{
name: "const_not_found",
err: NotFound,
want: KindNotFound,
},
{
name: "not_found",
err: NotFoundError("a not found error"),
want: KindNotFound,
},
{
name: "const_conflict",
err: Conflict,
want: KindConflict,
},
{
name: "conflict",
err: ConflictError("a conflict error"),
want: KindConflict,
},
{
name: "const_permission_denied",
err: PermissionDenied,
want: KindPermissionDenied,
},
{
name: "permission_denied",
err: PermissionDeniedError("a permission denied error"),
want: KindPermissionDenied,
},
{
name: "const_gone",
err: Gone,
want: KindGone,
},
{
name: "gone",
err: GoneError("a gone error"),
want: KindGone,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var e Error
if !errors.As(tc.err, &e) {
t.Errorf("errors.As(%v, &e) = false, want true", tc.err)
return
}
if got := e.Kind(); got != tc.want {
t.Errorf("e.Kind() = %v, want %v", got, tc.want)
}
})
}
}
func TestErrorIs(t *testing.T) {
if !errors.Is(UnauthenticatedError("x"), Unauthenticated) {
t.Errorf("errors.Is(UnauthenticatedError, Unauthenticated) = false, want true")
}
if errors.Is(UnauthenticatedError("x"), NotFound) {
t.Errorf("errors.Is(UnauthenticatedError, NotFound) = true, want false")
}
}