-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
78 lines (72 loc) · 1.68 KB
/
errors_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
package dtls
import (
"errors"
"fmt"
"net"
"testing"
"golang.org/x/xerrors"
)
func TestErrorUnwrap(t *testing.T) {
errExample := errors.New("an example error")
cases := []struct {
err error
errUnwrapped []error
}{
{
&FatalError{errExample},
[]error{errExample},
},
{
&TemporaryError{errExample},
[]error{errExample},
},
{
&InternalError{errExample},
[]error{errExample},
},
{
&TimeoutError{errExample},
[]error{errExample},
},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprintf("%T", c.err), func(t *testing.T) {
err := c.err
for _, unwrapped := range c.errUnwrapped {
e := xerrors.Unwrap(err)
if e != unwrapped {
t.Errorf("Unwrapped error is expected to be '%v', got '%v'", unwrapped, e)
}
}
})
}
}
func TestErrorNetError(t *testing.T) {
errExample := errors.New("an example error")
cases := []struct {
err error
str string
timeout, temporary bool
}{
{&FatalError{errExample}, "dtls fatal: an example error", false, false},
{&TemporaryError{errExample}, "dtls temporary: an example error", false, true},
{&InternalError{errExample}, "dtls internal: an example error", false, false},
{&TimeoutError{errExample}, "dtls timeout: an example error", true, true},
}
for _, c := range cases {
c := c
t.Run(fmt.Sprintf("%T", c.err), func(t *testing.T) {
ne, ok := c.err.(net.Error)
if !ok {
t.Fatalf("%T doesn't implement net.Error", c.err)
}
if ne.Timeout() != c.timeout {
t.Errorf("%T.Timeout() should be %v", c.err, c.timeout)
}
if ne.Temporary() != c.temporary {
t.Errorf("%T.Temporary() should be %v", c.err, c.temporary)
}
})
}
}