-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_error_test.go
147 lines (117 loc) · 3.25 KB
/
shared_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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Copyright (c) 2022, Xerra Earth Observation Institute
* All rights reserved. Use is subject to License terms.
* See LICENSE.TXT in the root directory of this source tree.
*/
package sharederror_test
import (
"fmt"
"testing"
"github.com/starboard-nz/sharederror"
)
func TestNewSharedError(t *testing.T) {
var sharedErr interface{} = sharederror.NewSharedError()
_, ok := (sharedErr).(error)
if !ok {
t.Errorf("expected shared error to implement error")
}
}
func TestSharedErrorTriggered(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
t.Log("initial")
if sharedErr.Triggered() {
t.Errorf("expected shared error not to be triggered")
}
t.Log("store error")
sharedErr.Store(fmt.Errorf("some error"))
if !sharedErr.Triggered() {
t.Errorf("expected shared error to be triggered")
}
}
func TestSharedErrorErr(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
t.Log("initial")
if err := sharedErr.Err(); err != nil {
t.Errorf("expected error to be nil, got %v", err)
}
t.Log("store error")
sharedErr.Store(fmt.Errorf("some error"))
if err := sharedErr.Err(); err == nil {
t.Errorf("expected error after store, got nil")
}
}
func TestSharedErrorErrors(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
t.Log("initial")
if errs := sharedErr.Errors(); len(errs) > 0 {
t.Errorf("expected no errors, got %d", len(errs))
}
n := 30
t.Log("store errors")
for i := 0; i < n; i++ {
sharedErr.Store(fmt.Errorf("some error"))
}
t.Log("after errors")
if errs := sharedErr.Errors(); len(errs) != n {
t.Errorf("expected %d stored errors, got %d", n, len(errs))
}
}
type targetError struct{}
func (e *targetError) Error() string {
return "some error"
}
func TestSharedErrorIsAny(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
t.Log("initial")
if sharedErr.IsAny(&targetError{}) {
t.Errorf("expected no match on target error, got match")
}
t.Log("store other error")
sharedErr.Store(fmt.Errorf("some other error"))
if sharedErr.IsAny(&targetError{}) {
t.Errorf("expected no match on target error, got match")
}
t.Log("store target error")
sharedErr.Store(&targetError{})
if !sharedErr.IsAny(&targetError{}) {
t.Errorf("expected match on target error, got no matches")
}
}
func TestSharedErrorIsAll(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
t.Log("initial")
if sharedErr.IsAll(&targetError{}) {
t.Errorf("expected no match on target error, got match")
}
n := 30
t.Log("store target errors")
for i := 0; i < n; i++ {
sharedErr.Store(&targetError{})
}
t.Log("is all target errors")
if !sharedErr.IsAll(&targetError{}) {
t.Errorf("expected all errors match on target error, got no matches")
}
t.Log("store other error")
sharedErr.Store(fmt.Errorf("some other error"))
if sharedErr.IsAll(&targetError{}) {
t.Errorf("expected not all errors match on target error, got all matches")
}
}
func TestReset(t *testing.T) {
var sharedErr = sharederror.NewSharedError()
err := sharedErr.Reset()
if err != nil {
t.Errorf("incorrect result")
}
t.Log("store error")
sharedErr.Store(fmt.Errorf("some error"))
err = sharedErr.Reset()
if err == nil {
t.Errorf("incorrect result")
}
err = sharedErr.Reset()
if err != nil {
t.Errorf("incorrect result")
}
}