-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
194 lines (158 loc) · 4.3 KB
/
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
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
package errgo
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes"
)
type ErrTestError string
func (e ErrTestError) Error() string {
return string(e)
}
const (
ErrTest = ErrTestError("test")
TestMsg = "test message"
FileName = "error_test.go"
ExpectedErrText = "InvalidArgument (3): test message"
)
type ErrorTestSuite struct {
suite.Suite
}
func Test_ErrorTestSuite(t *testing.T) {
suite.Run(t, new(ErrorTestSuite))
}
func (s *ErrorTestSuite) TestNew() {
r := s.Require()
_, _, line := getStackInfo(0)
err := Wrap(ErrTest, TestMsg, codes.InvalidArgument)
r.Equal(TestMsg, err.Message)
r.Equal(codes.InvalidArgument, err.Code)
r.Equal(FileName, err.File)
r.Equal("(*ErrorTestSuite).TestNew()", err.Function)
r.Equal(line+1, err.Line)
}
func (s *ErrorTestSuite) TestNew_genericFunc() {
r := s.Require()
line, err := genericFunc("test")
errgoError := err.(*Error)
r.Equal(TestMsg, errgoError.Message)
r.Equal(codes.InvalidArgument, errgoError.Code)
r.Equal(FileName, errgoError.File)
r.Equal("genericFunc[...]()", errgoError.Function)
r.Equal(line, errgoError.Line)
r.Equal(ExpectedErrText, err.Error())
}
func (s *ErrorTestSuite) TestError() {
r := s.Require()
err := Wrap(ErrTest, "test message", codes.InvalidArgument)
r.Equal(ExpectedErrText, err.Error())
}
func (s *ErrorTestSuite) TestUnwrap() {
r := s.Require()
_, _, line := getStackInfo(0)
err := Wrap(ErrTest, TestMsg, codes.InvalidArgument)
errgoError := Unwrap(err)
r.NotNil(errgoError)
r.Equal(TestMsg, errgoError.Message)
r.Equal(codes.InvalidArgument, errgoError.Code)
r.Equal(FileName, errgoError.File)
r.Equal("(*ErrorTestSuite).TestUnwrap()", errgoError.Function)
r.Equal(line+1, errgoError.Line)
errgoError = Unwrap(nil)
r.Nil(errgoError)
errgoError = Unwrap(ErrTest)
r.Nil(errgoError)
}
func (s *ErrorTestSuite) TestUnwrapAll() {
r := s.Require()
err := Wrap(ErrTest, TestMsg, codes.InvalidArgument)
err = Wrap(err, "nested error", codes.Unauthenticated)
err = Wrap(err, "another nested error", codes.NotFound)
errors := UnwrapAll(err)
r.Len(errors, 3)
}
func (s *ErrorTestSuite) TestGetRoot() {
r := s.Require()
_, _, line := getStackInfo(0)
err := Wrap(ErrTest, TestMsg, codes.InvalidArgument)
err = Wrap(err, "nested error", codes.Unauthenticated)
err = Wrap(err, "another nested error", codes.NotFound)
rootErr := GetRoot(err)
r.Equal(TestMsg, rootErr.Message)
r.Equal(codes.InvalidArgument, rootErr.Code)
r.Equal(FileName, rootErr.File)
r.Equal("(*ErrorTestSuite).TestGetRoot()", rootErr.Function)
r.Equal(line+1, rootErr.Line)
}
func (s *ErrorTestSuite) TestContainsError() {
r := s.Require()
// Test with nil errors
{
left := error(nil)
right := error(nil)
r.False(ContainsError(left, right))
}
// Test with equal errors
{
err := errors.New("test error")
left := err
right := err
r.True(ContainsError(left, right))
}
// Test with different errors
{
left := errors.New("error 1")
right := errors.New("error 2")
r.False(ContainsError(left, right))
}
// Test with wrapped errors
{
inner := ErrTest
left := Wrap(inner, "outer error", codes.AlreadyExists)
right := inner
r.True(ContainsError(left, right))
}
}
func (s *ErrorTestSuite) TestContainsCode() {
r := s.Require()
// Test with nil error
{
var err error
code := codes.NotFound
r.False(ContainsCode(err, code))
}
// Test with matching code
{
err := errors.New("test error")
code := codes.NotFound
wrapper := Wrap(err, "wrapped error", code)
r.True(ContainsCode(wrapper, code))
}
// Test with non-matching code
{
err := errors.New("test error")
code := codes.Unknown
wrapper := Wrap(err, "wrapped error", codes.NotFound)
r.False(ContainsCode(wrapper, code))
}
// Test with string
{
code := codes.DeadlineExceeded
err := errors.New(code.String())
wrapper := Wrap(err, "wrapped error", codes.AlreadyExists)
str := fmt.Sprint(Wrap(wrapper, "another wrapped error", codes.NotFound).JSON())
fmt.Println(str)
r.True(ContainsCode(wrapper, code))
}
// Test with unwrapped error
{
code := codes.DeadlineExceeded
err := errors.New(code.String())
r.True(ContainsCode(err, code))
}
}
func genericFunc[T any](_ T) (int, error) {
_, _, line := getStackInfo(0)
return line + 1, Wrap(ErrTest, TestMsg, codes.InvalidArgument)
}