Debian Codegraph @

gcerrors/errors_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package gcerrors
16
17 import (
18 "context"
19 "io"
20 "testing"
21
22 "gocloud.dev/internal/gcerr"
23 )
24
25 type wrappedErr struct {
26 err error
27 }
28
29 func (w wrappedErr) Error() string { return "wrapped" }
30
31 func (w wrappedErr) Unwrap() error { return w.err }
32
33 func TestCode(t *testing.T) {
34 for _, test := range []struct {
35 in error
36 want ErrorCode
37 }{
38 {nil, OK},
39 {gcerr.New(AlreadyExists, nil, 1, ""), AlreadyExists},
40 {wrappedErr{gcerr.New(PermissionDenied, nil, 1, "")}, PermissionDenied},
41 {context.Canceled, Canceled},
42 {context.DeadlineExceeded, DeadlineExceeded},
43 {wrappedErr{context.Canceled}, Canceled},
44 {wrappedErr{context.DeadlineExceeded}, DeadlineExceeded},
45 {io.EOF, Unknown},
46 } {
47 got := Code(test.in)
48 if got != test.want {
49 t.Errorf("%v: got %s, want %s", test.in, got, test.want)
50 }
51 }
52 }