-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
479 lines (412 loc) · 13.3 KB
/
Copy patherrors_test.go
File metadata and controls
479 lines (412 loc) · 13.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// errors_test.go: Tests for the go-errors AGILira library
//
// Copyright (c) 2025 AGILira - A. Giordano
// Series: an AGLIra library
// SPDX-License-Identifier: MPL-2.0
package errors
import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
)
const (
TestCodeValidation ErrorCode = "VALIDATION_ERROR"
TestCodeDatabase ErrorCode = "DATABASE_ERROR"
)
func TestNew(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
// Use a table-driven approach to reduce cyclomatic complexity
tests := []struct {
field string
expected interface{}
actual interface{}
}{
{"Code", TestCodeValidation, err.Code},
{"Message", "Validation failed", err.Message},
{"Severity", SeverityError, err.Severity},
}
for _, tt := range tests {
if tt.actual != tt.expected {
t.Errorf("Field %s: expected %v, got %v", tt.field, tt.expected, tt.actual)
}
}
// Test special cases separately for clarity
if err.Timestamp.IsZero() {
t.Error("Expected timestamp to be set")
}
if err.Context == nil {
t.Error("Expected context to be initialized")
}
}
func TestNewWithField(t *testing.T) {
err := NewWithField(TestCodeValidation, "Field is empty", "username", "")
if err.Field != "username" {
t.Errorf("Expected field 'username', got '%s'", err.Field)
}
if err.Value != "" {
t.Errorf("Expected empty value, got '%s'", err.Value)
}
}
func TestNewWithContext(t *testing.T) {
ctx := map[string]interface{}{"user_id": "123"}
err := NewWithContext(TestCodeDatabase, "DB error", ctx)
if err.Context["user_id"] != "123" {
t.Errorf("Expected user_id '123', got '%v'", err.Context["user_id"])
}
}
func TestWrapAndRootCause(t *testing.T) {
orig := errors.New("original error")
wrapped := Wrap(orig, TestCodeDatabase, "DB failed")
if wrapped.Cause != orig {
t.Error("Expected cause to be original error")
}
if RootCause(wrapped) != orig {
t.Error("RootCause did not return the original error")
}
}
func TestErrorString(t *testing.T) {
err := NewWithField(TestCodeValidation, "Invalid format", "email", "bad")
str := err.Error()
if !strings.Contains(str, string(TestCodeValidation)) || !strings.Contains(str, "Invalid format") {
t.Error("Error string does not contain code or message")
}
}
func TestUnwrap(t *testing.T) {
orig := errors.New("original error")
wrapped := Wrap(orig, TestCodeDatabase, "DB failed")
if wrapped.Unwrap() != orig {
t.Error("Expected unwrapped error to be original error")
}
}
func TestHasCode(t *testing.T) {
err := Wrap(New(TestCodeValidation, "Validation failed"), TestCodeDatabase, "DB failed")
if !HasCode(err, TestCodeDatabase) {
t.Error("HasCode did not find the code in the chain")
}
if HasCode(err, "NON_EXISTENT") {
t.Error("HasCode returned true for a non-existent code")
}
}
func TestIsAndAsCompatibility(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
if !errors.Is(err, &Error{Code: TestCodeValidation}) {
t.Error("errors.Is did not match error code")
}
var target *Error
if !errors.As(err, &target) {
t.Error("errors.As did not match *Error type")
}
}
func TestStacktrace(t *testing.T) {
err := Wrap(errors.New("fail"), TestCodeValidation, "with stack")
if err.Stack == nil || len(err.Stack.Frames) == 0 {
t.Error("Expected stacktrace to be captured")
}
str := err.Stack.String()
if str == "" {
t.Error("Stacktrace string should not be empty")
}
}
func TestMarshalJSON(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
data, errMarshal := json.Marshal(err)
if errMarshal != nil {
t.Errorf("MarshalJSON failed: %v", errMarshal)
}
if !strings.Contains(string(data), "Validation failed") {
t.Error("JSON does not contain error message")
}
}
func TestInterfaces(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
// Test ErrorCoder interface
if err.ErrorCode() != TestCodeValidation {
t.Error("ErrorCoder interface not working")
}
err.Retryable = true
// Test Retryable interface
if !err.IsRetryable() {
t.Error("Retryable interface not working")
}
err = err.WithUserMessage("User message")
// Test UserMessager interface
if err.UserMessage() != "User message" {
t.Error("UserMessager interface not working")
}
}
func TestIsWithNilTarget(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
if err.Is(nil) {
t.Error("Is should return false for nil target")
}
}
func TestIsWithNonErrorTarget(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
nonErrorTarget := errors.New("standard error")
if err.Is(nonErrorTarget) {
t.Error("Is should return false for non-Error target")
}
}
func TestAsFunction(t *testing.T) {
orig := errors.New("original error")
wrapped := Wrap(orig, TestCodeDatabase, "DB failed")
// Test that As can find the original error in the cause chain
var origTarget error
if !wrapped.As(&origTarget) {
t.Error("As should succeed for error target")
}
if origTarget != orig {
t.Error("As should set target to the original error")
}
}
func TestAsWithNilTarget(t *testing.T) {
err := New(TestCodeValidation, "Validation failed")
if err.As(nil) {
t.Error("As should return false for nil target")
}
}
func TestMarshalJSONWithNilStack(t *testing.T) {
// Create error without stack trace (should be nil by default)
err := New(TestCodeValidation, "Validation failed")
// Verify stack is actually nil
if err.Stack != nil {
t.Fatal("Stack should be nil for errors created with New()")
}
// Test JSON marshaling and validation in helper function to reduce complexity
validateJSONStack(t, err, "Stack should be empty string when nil")
}
// Helper function to reduce cyclomatic complexity in JSON tests
func validateJSONStack(t *testing.T, err *Error, expectedMsg string) {
data, errMarshal := json.Marshal(err)
if errMarshal != nil {
t.Errorf("MarshalJSON failed: %v", errMarshal)
return
}
var result map[string]interface{}
if errUnmarshal := json.Unmarshal(data, &result); errUnmarshal != nil {
t.Errorf("Failed to unmarshal: %v", errUnmarshal)
return
}
if stack, exists := result["stack"]; exists && stack != "" {
t.Error(expectedMsg)
}
}
func TestMarshalJSONWithEmptyStack(t *testing.T) {
// Create error with empty stack trace
err := New(TestCodeValidation, "Validation failed")
err.Stack = &Stacktrace{Frames: []uintptr{}} // Empty stack
// Use helper function to reduce complexity
validateJSONStack(t, err, "Stack should be empty string when stack has no frames")
}
func TestStacktraceStringWithNilStacktrace(t *testing.T) {
var stack *Stacktrace
result := stack.String()
if result != "" {
t.Errorf("Expected empty string for nil stacktrace, got: %s", result)
}
}
func TestStacktraceStringWithEmptyFrames(t *testing.T) {
stack := &Stacktrace{Frames: []uintptr{}}
result := stack.String()
if result != "" {
t.Errorf("Expected empty string for empty frames, got: %s", result)
}
}
func TestUserMessageWithEmptyUserMsg(t *testing.T) {
err := New(TestCodeValidation, "Technical message")
err.UserMsg = "" // Ensure UserMsg is empty
result := err.UserMessage()
if result != "Technical message" {
t.Errorf("Expected technical message when UserMsg is empty, got: %s", result)
}
}
func TestUserMessageWithEmptyBoth(t *testing.T) {
err := New(TestCodeValidation, "")
err.UserMsg = ""
result := err.UserMessage()
if result != "" {
t.Errorf("Expected empty string when both messages are empty, got: %s", result)
}
}
func TestWithContext(t *testing.T) {
err := New(TestCodeValidation, "Test error")
// Test adding context to error with nil context
err.Context = nil
err = err.WithContext("key1", "value1")
if err.Context == nil {
t.Error("Expected context to be initialized")
}
if err.Context["key1"] != "value1" {
t.Errorf("Expected context key1 to be 'value1', got '%v'", err.Context["key1"])
}
// Test adding more context
err = err.WithContext("key2", "value2")
if err.Context["key2"] != "value2" {
t.Errorf("Expected context key2 to be 'value2', got '%v'", err.Context["key2"])
}
// Test updating existing context
err = err.WithContext("key1", "updated_value")
if err.Context["key1"] != "updated_value" {
t.Errorf("Expected context key1 to be 'updated_value', got '%v'", err.Context["key1"])
}
}
func TestAsRetryable(t *testing.T) {
err := New(TestCodeValidation, "Test error")
// Test marking error as retryable
err = err.AsRetryable()
if !err.Retryable {
t.Error("Expected error to be marked as retryable")
}
// Test that it returns the same error instance for chaining
if err.AsRetryable() != err {
t.Error("Expected AsRetryable to return the same error instance")
}
}
func TestWithSeverity(t *testing.T) {
err := New(TestCodeValidation, "Test error")
// Test setting severity
err = err.WithSeverity("warning")
if err.Severity != "warning" {
t.Errorf("Expected severity to be 'warning', got '%s'", err.Severity)
}
// Test setting different severity
err = err.WithSeverity("critical")
if err.Severity != "critical" {
t.Errorf("Expected severity to be 'critical', got '%s'", err.Severity)
}
// Test that it returns the same error instance for chaining
if err.WithSeverity("info") != err {
t.Error("Expected WithSeverity to return the same error instance")
}
}
func TestMethodChaining(t *testing.T) {
err := New(TestCodeValidation, "Test").
WithUserMessage("User friendly message").
WithContext("key", "value").
AsRetryable().
WithSeverity("warning")
if err.UserMsg != "User friendly message" {
t.Error("Method chaining failed for UserMessage")
}
if err.Context["key"] != "value" {
t.Error("Method chaining failed for Context")
}
if !err.Retryable {
t.Error("Method chaining failed for Retryable")
}
if err.Severity != "warning" {
t.Error("Method chaining failed for Severity")
}
}
func TestSeverityConstants(t *testing.T) {
// Test that severity constants are properly defined
if SeverityCritical != "critical" {
t.Errorf("Expected SeverityCritical to be 'critical', got '%s'", SeverityCritical)
}
if SeverityError != "error" {
t.Errorf("Expected SeverityError to be 'error', got '%s'", SeverityError)
}
if SeverityWarning != "warning" {
t.Errorf("Expected SeverityWarning to be 'warning', got '%s'", SeverityWarning)
}
if SeverityInfo != "info" {
t.Errorf("Expected SeverityInfo to be 'info', got '%s'", SeverityInfo)
}
}
func TestSeverityHelperMethods(t *testing.T) {
err := New(TestCodeValidation, "Test message")
// Test critical severity helper (return value intentionally ignored to test in-place modification)
_ = err.WithCriticalSeverity()
if err.Severity != SeverityCritical {
t.Errorf("Expected severity '%s', got '%s'", SeverityCritical, err.Severity)
}
// Test warning severity helper (return value intentionally ignored to test in-place modification)
_ = err.WithWarningSeverity()
if err.Severity != SeverityWarning {
t.Errorf("Expected severity '%s', got '%s'", SeverityWarning, err.Severity)
}
// Test info severity helper (return value intentionally ignored to test in-place modification)
_ = err.WithInfoSeverity()
if err.Severity != SeverityInfo {
t.Errorf("Expected severity '%s', got '%s'", SeverityInfo, err.Severity)
}
}
func TestErrorCodeValidation(t *testing.T) {
// Table-driven tests to reduce cyclomatic complexity
testCases := []struct {
name string
input ErrorCode
expectedCode ErrorCode
}{
{"empty ErrorCode", "", DefaultErrorCode},
{"whitespace-only ErrorCode", " \t\n ", DefaultErrorCode},
{"valid ErrorCode", "VALID_CODE", "VALID_CODE"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := New(tc.input, "Test message")
if err.Code != tc.expectedCode {
t.Errorf("Expected code '%s', got '%s'", tc.expectedCode, err.Code)
}
})
}
// Test constructor functions with empty codes
testConstructorValidation(t)
}
// Helper function to test constructor validation
func testConstructorValidation(t *testing.T) {
constructors := []struct {
name string
fn func() ErrorCode
}{
{"NewWithField", func() ErrorCode {
return NewWithField("", "Test message", "field", "value").Code
}},
{"NewWithContext", func() ErrorCode {
ctx := map[string]interface{}{"key": "value"}
return NewWithContext("", "Test message", ctx).Code
}},
{"Wrap", func() ErrorCode {
originalErr := fmt.Errorf("original error")
return Wrap(originalErr, "", "Wrapped message").Code
}},
}
for _, constructor := range constructors {
t.Run(constructor.name, func(t *testing.T) {
code := constructor.fn()
if code != DefaultErrorCode {
t.Errorf("Expected code '%s' for empty input in %s, got '%s'",
DefaultErrorCode, constructor.name, code)
}
})
}
}
func TestStacktraceOptimizations(t *testing.T) {
// Test stacktrace capture with normal depth
stack := CaptureStacktrace(0)
if stack == nil {
t.Fatal("Expected non-nil stacktrace")
}
if len(stack.Frames) == 0 {
t.Error("Expected frames in stacktrace")
}
// Test String() method optimizations
stackStr := stack.String()
if len(stackStr) == 0 {
t.Error("Expected non-empty stacktrace string")
}
// Test direct access to optimization paths by calling recursive function
recursiveStack := testRecursiveStackCapture(0, 20) // Create deeper stack
if recursiveStack == nil {
t.Fatal("Expected non-nil recursive stacktrace")
}
}
// Helper function to create deeper call stacks for testing
func testRecursiveStackCapture(depth, target int) *Stacktrace {
if depth >= target {
return CaptureStacktrace(0)
}
return testRecursiveStackCapture(depth+1, target)
}