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
|
package errors
import (
"errors"
"strings"
"testing"
)
func TestWrap(t *testing.T) {
tests := []struct {
name string
err error
msg string
expected string
}{
{
name: "wrap with message",
err: ErrFileNotFound,
msg: "opening config file",
expected: "opening config file: file not found",
},
{
name: "wrap nil error",
err: nil,
msg: "should return nil",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Wrap(tt.err, tt.msg)
if tt.err == nil && result != nil {
t.Errorf("expected nil, got %v", result)
}
if tt.err != nil && result.Error() != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result.Error())
}
})
}
}
func TestWrapf(t *testing.T) {
err := Wrapf(ErrConnectionFailed, "connecting to %s:%d", "localhost", 2222)
expected := "connecting to localhost:2222: connection failed"
if err.Error() != expected {
t.Errorf("expected %q, got %q", expected, err.Error())
}
}
func TestIs(t *testing.T) {
wrapped := Wrap(ErrPermissionDenied, "accessing /etc/passwd")
if !Is(wrapped, ErrPermissionDenied) {
t.Error("expected Is to return true for wrapped error")
}
if Is(wrapped, ErrFileNotFound) {
t.Error("expected Is to return false for different error")
}
}
func TestMultiError(t *testing.T) {
multi := NewMultiError()
// Test empty multi-error
if multi.HasErrors() {
t.Error("new MultiError should not have errors")
}
if multi.ErrorOrNil() != nil {
t.Error("ErrorOrNil should return nil for empty MultiError")
}
// Add errors
multi.Add(ErrConnectionFailed)
multi.Add(nil) // Should be ignored
multi.Add(ErrTimeout)
if !multi.HasErrors() {
t.Error("MultiError should have errors after adding")
}
if len(multi.Errors()) != 2 {
t.Errorf("expected 2 errors, got %d", len(multi.Errors()))
}
// Test error message
errMsg := multi.Error()
if !strings.Contains(errMsg, "multiple errors occurred") {
t.Errorf("unexpected error message: %s", errMsg)
}
// Test single error
single := NewMultiError()
single.Add(ErrInvalidArgument)
if single.Error() != "invalid argument" {
t.Errorf("single error message incorrect: %s", single.Error())
}
}
func TestErrorUnwrapping(t *testing.T) {
base := errors.New("base error")
wrapped := Wrap(base, "context")
unwrapped := Unwrap(wrapped)
if unwrapped != base {
t.Error("Unwrap did not return base error")
}
}
|