-
Notifications
You must be signed in to change notification settings - Fork 39
/
error_test.go
86 lines (75 loc) · 1.84 KB
/
error_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
package eventsource_test
import (
"io"
"testing"
"fmt"
"github.com/altairsix/eventsource"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestNewError(t *testing.T) {
err := eventsource.NewError(io.EOF, "code", "hello %v", "world")
assert.NotNil(t, err)
v, ok := err.(eventsource.Error)
assert.True(t, ok)
assert.Equal(t, io.EOF, v.Cause())
assert.Equal(t, "code", v.Code())
assert.Equal(t, "hello world", v.Message())
assert.Equal(t, "[code] hello world - EOF", v.Error())
s, ok := err.(fmt.Stringer)
assert.True(t, ok)
assert.Equal(t, v.Error(), s.String())
}
func TestIsNotFound(t *testing.T) {
testCases := map[string]struct {
Err error
IsNotFound bool
}{
"nil": {
Err: nil,
IsNotFound: false,
},
"eventsource.Error": {
Err: eventsource.NewError(nil, eventsource.ErrAggregateNotFound, "not found"),
IsNotFound: true,
},
"nested eventsource.Error": {
Err: eventsource.NewError(
eventsource.NewError(nil, eventsource.ErrAggregateNotFound, "not found"),
eventsource.ErrUnboundEventType,
"not found",
),
IsNotFound: true,
},
}
for label, tc := range testCases {
t.Run(label, func(t *testing.T) {
assert.Equal(t, tc.IsNotFound, eventsource.IsNotFound(tc.Err))
})
}
}
func TestErrHasCode(t *testing.T) {
code := "code"
testCases := map[string]struct {
Err error
ErrHasCode bool
}{
"simple": {
Err: eventsource.NewError(nil, code, "blah"),
ErrHasCode: true,
},
"nope": {
Err: errors.New("blah"),
ErrHasCode: false,
},
"nested": {
Err: eventsource.NewError(eventsource.NewError(nil, code, "blah"), "blah", "blah"),
ErrHasCode: true,
},
}
for label, tc := range testCases {
t.Run(label, func(t *testing.T) {
assert.Equal(t, tc.ErrHasCode, eventsource.ErrHasCode(tc.Err, code))
})
}
}