-
Notifications
You must be signed in to change notification settings - Fork 16
/
alert_test.go
132 lines (113 loc) · 3.04 KB
/
alert_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sarah
import (
"context"
"errors"
"strings"
"testing"
)
type DummyAlerter struct {
AlertFunc func(context.Context, BotType, error) error
}
func (alerter *DummyAlerter) Alert(ctx context.Context, botType BotType, err error) error {
return alerter.AlertFunc(ctx, botType, err)
}
func TestAlertErrs_appendError(t *testing.T) {
e := errors.New("foo")
errs := &alertErrs{}
errs.appendError(e)
if len(*errs) != 1 {
t.Errorf("Expected 1 error to be stored, but was %d.", len(*errs))
}
}
func TestAlertErrs_isEmpty(t *testing.T) {
errs := &alertErrs{}
if !errs.isEmpty() {
t.Error("Expected to be true, but was not.")
}
errs = &alertErrs{errors.New("foo")}
if errs.isEmpty() {
t.Error("Expected to be false, but was not.")
}
}
func TestAlertErrs_Error(t *testing.T) {
testSets := []struct {
errs []error
}{
{errs: nil},
{errs: []error{errors.New("single error string")}},
{errs: []error{errors.New("1st error string"), errors.New("2nd error string")}},
}
for i, testSet := range testSets {
var errs []string
for _, err := range testSet.errs {
errs = append(errs, err.Error())
}
expected := strings.Join(errs, "\n")
e := &alertErrs{}
*e = testSet.errs
if e.Error() != expected {
t.Errorf("Expected error is not returned on test %d: %s", i, e.Error())
}
}
}
func TestAlerters_appendAlerter(t *testing.T) {
a := &alerters{}
impl := &DummyAlerter{}
a.appendAlerter(impl)
if len(*a) != 1 {
t.Fatalf("Expected 1 Alerter to be stored, but was %d.", len(*a))
}
}
func TestAlerters_alertAll(t *testing.T) {
a := &alerters{}
err := a.alertAll(context.TODO(), "FOO", errors.New("error"))
if err != nil {
t.Errorf("Expected no error to be returned, but got %s.", err.Error())
}
wrappedErr := errors.New("panic with an error")
a = &alerters{
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
panic(wrappedErr)
},
},
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
panic("PANIC!!")
},
},
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
return wrappedErr
},
},
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
return nil
},
},
}
err = a.alertAll(context.TODO(), "FOO", errors.New("error"))
if err == nil {
t.Fatal("Expected error to be returned")
}
typed, ok := err.(*alertErrs)
if !ok {
t.Fatalf("Expected error type of *alertErrs, but was %T.", err)
}
if len(*typed) != 3 {
t.Fatalf("Expected 3 errors to be stored: %#v.", err)
}
// The first error contains an error derived from panic
if !errors.Is((*typed)[0], wrappedErr) {
t.Errorf("Expected error is not wrapped: %+v", (*typed)[0])
}
// The second error contains a string derived from panic
if !strings.HasSuffix((*typed)[1].Error(), "PANIC!!") {
t.Errorf("Expected string is not wrapped: %+v", (*typed)[1])
}
// The third error wraps a error derived from Alerter.Alert
if !errors.Is((*typed)[2], wrappedErr) {
t.Errorf("Expected error is not wrapped: %+v", (*typed)[2])
}
}