-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompatibility_pkg_errors_test.go
More file actions
142 lines (112 loc) · 3.73 KB
/
compatibility_pkg_errors_test.go
File metadata and controls
142 lines (112 loc) · 3.73 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
package errors
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// testCauseError implements the Cause() interface for testing
type testCauseError struct {
cause error
msg string
}
func (e *testCauseError) Error() string {
return e.msg
}
func (e *testCauseError) Cause() error {
return e.cause
}
func TestCause(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := Cause(nil)
assert.Nil(t, result)
})
t.Run("error without cause", func(t *testing.T) {
err := New("test error")
result := Cause(err)
assert.Equal(t, err, result)
})
t.Run("error with cause interface", func(t *testing.T) {
innerErr := New("inner error")
outerErr := &testCauseError{msg: "outer error", cause: innerErr}
result := Cause(outerErr)
assert.Equal(t, innerErr, result)
})
}
func TestWithMessage(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := WithMessage(nil, "message")
assert.Nil(t, result)
})
t.Run("wrap error with message", func(t *testing.T) {
originalErr := New("original error")
wrappedErr := WithMessage(originalErr, "additional context")
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "additional context")
assert.Contains(t, wrappedErr.Error(), "original error")
// Should be able to unwrap to get original error
assert.True(t, Is(wrappedErr, originalErr))
})
}
func TestWithMessagef(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := WithMessagef(nil, "formatted %s", "message")
assert.Nil(t, result)
})
t.Run("wrap error with formatted message", func(t *testing.T) {
originalErr := New("original error")
wrappedErr := WithMessagef(originalErr, "context %d: %s", 42, "additional info")
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "context 42: additional info")
assert.Contains(t, wrappedErr.Error(), "original error")
// Should be able to unwrap to get original error
assert.True(t, Is(wrappedErr, originalErr))
})
}
func TestWithStack(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := WithStack(nil)
assert.Nil(t, result)
})
t.Run("add stack to error", func(t *testing.T) {
originalErr := fmt.Errorf("standard error")
stackErr := WithStack(originalErr)
assert.NotNil(t, stackErr)
// The WithStack implementation uses Join which adds formatting, so we check the content
assert.Contains(t, stackErr.Error(), originalErr.Error())
// Should be able to unwrap to get original error
assert.True(t, Is(stackErr, originalErr))
// Should have stack trace when formatted with %+v
stackStr := fmt.Sprintf("%+v", stackErr)
assert.NotEmpty(t, stackStr)
})
}
func TestWrap(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := Wrap(nil, "message")
assert.Nil(t, result)
})
t.Run("wrap error with message", func(t *testing.T) {
originalErr := fmt.Errorf("original error")
wrappedErr := Wrap(originalErr, "wrapper message")
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "wrapper message")
assert.Contains(t, wrappedErr.Error(), "original error")
// Should be able to unwrap to get original error
assert.True(t, Is(wrappedErr, originalErr))
})
}
func TestWrapf(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
result := Wrapf(nil, "formatted %s", "message")
assert.Nil(t, result)
})
t.Run("wrap error with formatted message", func(t *testing.T) {
originalErr := fmt.Errorf("original error")
wrappedErr := Wrapf(originalErr, "wrapper %d: %s", 123, "context")
assert.NotNil(t, wrappedErr)
assert.Contains(t, wrappedErr.Error(), "wrapper 123: context")
assert.Contains(t, wrappedErr.Error(), "original error")
// Should be able to unwrap to get original error
assert.True(t, Is(wrappedErr, originalErr))
})
}