Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
244 lines (219 loc) · 6.08 KB
/
Copy patherrors_test.go
File metadata and controls
244 lines (219 loc) · 6.08 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-License-Identifier: MIT
package iec61850
import (
"errors"
"fmt"
"strings"
"testing"
)
func TestSentinelErrors(t *testing.T) {
sentinels := []error{
ErrInvalidReference,
ErrInvalidFunctionalConstraint,
ErrNotFound,
ErrTypeMismatch,
ErrUnsupportedService,
ErrSubscriptionClosed,
ErrSCLParse,
ErrModelMismatch,
ErrUnsupportedCDC,
ErrReportDecode,
ErrDatasetDecode,
ErrClosed,
ErrInvalidArgument,
ErrProtocol,
ErrControlFailed,
ErrSelectFailed,
ErrOperateFailed,
ErrCancelFailed,
ErrNotControllable,
}
for _, sentinel := range sentinels {
t.Run(sentinel.Error(), func(t *testing.T) {
wrapped := fmt.Errorf("outer: %w", sentinel)
if !errors.Is(wrapped, sentinel) {
t.Errorf("errors.Is failed for wrapped %v", sentinel)
}
})
}
}
func TestReferenceError_Unwrap(t *testing.T) {
err := &ReferenceError{Input: "bad/ref", Reason: "test reason"}
if !errors.Is(err, ErrInvalidReference) {
t.Error("ReferenceError should wrap ErrInvalidReference")
}
var refErr *ReferenceError
if !errors.As(err, &refErr) {
t.Error("errors.As should extract *ReferenceError")
}
if refErr.Input != "bad/ref" {
t.Errorf("Input = %q, want %q", refErr.Input, "bad/ref")
}
}
func TestReferenceError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("inner error")
err := &ReferenceError{Input: "ref", Reason: "reason", Wrapped: inner}
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestDecodeError_Unwrap(t *testing.T) {
err := &DecodeError{Ref: "LD/LN.DO", Type: "Quality", Message: "bad bits"}
if !errors.Is(err, ErrTypeMismatch) {
t.Error("DecodeError should wrap ErrTypeMismatch")
}
if _, ok := errors.AsType[*DecodeError](err); !ok {
t.Error("errors.As should extract *DecodeError")
}
}
func TestModelError_Unwrap(t *testing.T) {
err := &ModelError{Ref: "LD/LN", Message: "missing child"}
if !errors.Is(err, ErrModelMismatch) {
t.Error("ModelError should wrap ErrModelMismatch")
}
}
func TestReportError_Unwrap(t *testing.T) {
err := &ReportError{RCBRef: "LD/LN.RP.rcb01", Message: "decode failed"}
if !errors.Is(err, ErrReportDecode) {
t.Error("ReportError should wrap ErrReportDecode")
}
}
func TestSCLParseError_Unwrap(t *testing.T) {
err := &SCLParseError{File: "test.icd", Line: 42, Message: "bad XML"}
if !errors.Is(err, ErrSCLParse) {
t.Error("SCLParseError should wrap ErrSCLParse")
}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestSCLParseError_NoFile(t *testing.T) {
err := &SCLParseError{Message: "bad XML"}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestSCLParseError_FileNoLine(t *testing.T) {
err := &SCLParseError{File: "test.icd", Message: "bad XML"}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestSCLParseError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("xml syntax error")
err := &SCLParseError{File: "test.icd", Line: 10, Message: "bad element", Wrapped: inner}
s := err.Error()
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
if !containsSubstring(s, "xml syntax error") {
t.Errorf("Error() = %q, should contain wrapped error text", s)
}
}
func TestDecodeError_NoRef(t *testing.T) {
err := &DecodeError{Type: "Timestamp", Message: "bad format"}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestDecodeError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("inner error")
err := &DecodeError{Ref: "LD/LN.DO", Type: "Quality", Message: "bad bits", Wrapped: inner}
s := err.Error()
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
if s == "" {
t.Error("empty error string")
}
if !containsSubstring(s, "inner error") {
t.Errorf("Error() = %q, should contain wrapped error text", s)
}
}
func TestModelError_NoRef(t *testing.T) {
err := &ModelError{Message: "general error"}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestModelError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("inner model issue")
err := &ModelError{Ref: "LD/LN", Message: "mismatch", Wrapped: inner}
s := err.Error()
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
if !containsSubstring(s, "inner model issue") {
t.Errorf("Error() = %q, should contain wrapped error text", s)
}
}
func TestReportError_NoRCB(t *testing.T) {
err := &ReportError{Message: "general report error"}
s := err.Error()
if s == "" {
t.Error("empty error string")
}
}
func TestReportError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("decode failed")
err := &ReportError{RCBRef: "LD/LN.RP.rcb01", Message: "corrupt", Wrapped: inner}
s := err.Error()
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
if !containsSubstring(s, "decode failed") {
t.Errorf("Error() = %q, should contain wrapped error text", s)
}
}
func TestControlError_UnwrapByOperation(t *testing.T) {
tests := []struct {
op string
sentinel error
}{
{"select", ErrSelectFailed},
{"operate", ErrOperateFailed},
{"cancel", ErrCancelFailed},
{"unknown", ErrControlFailed},
}
for _, tt := range tests {
t.Run(tt.op, func(t *testing.T) {
err := &ControlError{Ref: "LD/LN.DO", Operation: tt.op}
if !errors.Is(err, tt.sentinel) {
t.Errorf("ControlError{Operation: %q} should unwrap to %v", tt.op, tt.sentinel)
}
})
}
}
func TestControlError_WithAddCause(t *testing.T) {
err := &ControlError{
Ref: "LD/LN.DO",
Operation: "operate",
AddCause: AddCauseBlockedByInterlocking,
}
s := err.Error()
if !containsSubstring(s, "blocked-by-interlocking") {
t.Errorf("Error() = %q, should contain addCause text", s)
}
}
func TestControlError_WithWrapped(t *testing.T) {
inner := fmt.Errorf("write rejected")
err := &ControlError{Ref: "LD/LN.DO", Operation: "operate", Wrapped: inner}
if !errors.Is(err, inner) {
t.Error("should unwrap to inner error")
}
if !errors.Is(err, ErrOperateFailed) {
t.Error("should also match ErrOperateFailed even when wrapped")
}
}
func containsSubstring(s, sub string) bool {
return strings.Contains(s, sub)
}