-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinerrors_test.go
55 lines (47 loc) · 1.09 KB
/
joinerrors_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
package rendezvous
import (
"context"
"errors"
"regexp"
"testing"
)
type myError struct{}
func (myError) Error() string {
return "myError"
}
func TestJoinErrors(t *testing.T) {
t.Parallel()
if !errors.Is(joinErrors(context.Canceled), context.Canceled) {
t.Error("errors.Is failure")
}
if errors.Is(joinErrors(context.Canceled), myError{}) {
t.Error("errors.Is failure")
}
var me myError
if !errors.As(joinErrors(context.Canceled, myError{}), &me) {
t.Error("errors.As failure")
}
if errors.As(joinErrors(context.Canceled), &me) {
t.Error("errors.As failure")
}
err := joinErrors(myError{}, myError{})
if err == nil {
t.Fatal("nil unexpected")
}
var errs []error
if err := err.(interface{ Unwrap() []error }); err == nil {
t.Fatal("Unwrap() []error expected")
} else {
errs = err.Unwrap()
}
if len(errs) != 2 {
t.Fatal("len 2 expected")
}
_ = errs[0].(myError)
_ = errs[1].(myError)
str := err.Error()
re := regexp.MustCompile(regexp.QuoteMeta(myError{}.Error()))
if len(re.FindAllString(str, -1)) != 2 {
t.Fatal("2 occurences of (myError{}).Error() expected")
}
}