forked from nuclio/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
185 lines (144 loc) · 4.42 KB
/
errors_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
package errors
import (
"bytes"
"errors"
"fmt"
"path"
"strings"
"testing"
"github.com/stretchr/testify/suite"
)
type ErrorsTestSuite struct {
suite.Suite
}
func (suite *ErrorsTestSuite) TestNew() {
var err error // Make sure we conform to Error interface
message := "hello"
err = New(message)
suite.Require().Equal(message, err.Error())
}
func (suite *ErrorsTestSuite) TestErrorf() {
var err error // Make sure we conform to Error interface
format := "bad math: %d != %q"
args := []interface{}{1, "2"}
err = Errorf(format, args...)
suite.Require().Equal(fmt.Sprintf(format, args...), err.Error())
}
func (suite *ErrorsTestSuite) TestWrap() {
cause := fmt.Errorf("first error")
message := "second error"
err := Wrap(cause, message)
suite.Require().Equal(cause, Cause(err))
suite.Require().Equal(message, err.Error())
}
func (suite *ErrorsTestSuite) TestCauseNoWrap() {
err := errors.New("The cause")
suite.Require().Equal(err, Cause(err))
}
func (suite *ErrorsTestSuite) TestWrapf() {
cause := fmt.Errorf("first error")
format := "bad math: %d != %q"
args := []interface{}{1, "2"}
err := Wrapf(cause, format, args...)
suite.Require().Equal(cause, Cause(err))
suite.Require().Equal(fmt.Sprintf(format, args...), err.Error())
}
func (suite *ErrorsTestSuite) TestFormat_s() {
cause := fmt.Errorf("first error")
message := "second error"
err := Wrap(cause, message)
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s", err)
suite.Require().Equal(message, buf.String())
}
func (suite *ErrorsTestSuite) TestFormat_q() {
cause := fmt.Errorf("first error")
message := "second error"
err := Wrap(cause, message)
var buf bytes.Buffer
fmt.Fprintf(&buf, "%q", err)
suite.Require().Equal(fmt.Sprintf("%q", message), buf.String())
}
func genError() error {
e1 := New("e1")
e2 := Wrap(e1, "e2")
e3 := Wrap(e2, "e3")
return e3
}
func (suite *ErrorsTestSuite) TestFormat_v() {
err := genError()
var buf bytes.Buffer
fmt.Fprintf(&buf, "%+v", err)
for _, err := range GetErrorStack(err, -1) {
errObj := err.(*Error)
fileName, lineNumber := errObj.LineInfo()
lineInfo := fmt.Sprintf("%s:%d", path.Base(fileName), lineNumber)
suite.Require().True(strings.Contains(buf.String(), lineInfo))
suite.Require().True(strings.Contains(buf.String(), err.Error()))
}
}
func (suite *ErrorsTestSuite) TestGetErrorStack() {
total := 10
var messages []string
for i := 0; i < total; i++ {
messages = append(messages, fmt.Sprintf("message #%d", i))
}
err := Errorf(messages[0])
for _, message := range messages[1:] {
err = Wrap(err, message)
}
// Check partial
size := 4
messageStack := GetErrorStack(err, size)
suite.Require().Equal(size, len(messageStack))
suite.Require().Equal(messageStack[0].Error(), messages[0])
// Check too much
messageStack = GetErrorStack(err, total+200)
suite.Require().Equal(total, len(messageStack))
// Check regular error
message := "hello there"
stdErr := fmt.Errorf(message)
messageStack = GetErrorStack(stdErr, 7)
suite.Require().Equal(1, len(messageStack))
suite.Require().Equal(messageStack[0].Error(), message)
}
func (suite *ErrorsTestSuite) TestReverse() {
e1, e2, e3, e4 := New("1"), New("2"), New("3"), New("4")
errors := []error{}
reverse(errors)
errors = []error{e1, e2, e3, e4}
reverse(errors)
suite.Require().Equal([]error{e4, e3, e2, e1}, errors)
errors = []error{e1, e2, e3}
reverse(errors)
suite.Require().Equal([]error{e3, e2, e1}, errors)
errors = []error{e1}
reverse(errors)
suite.Require().Equal([]error{e1}, errors)
}
func (suite *ErrorsTestSuite) TestPrintErrorStack() {
err := genError()
var buf bytes.Buffer
PrintErrorStack(&buf, err, -1)
for _, err := range GetErrorStack(err, -1) {
errObj := err.(*Error)
fileName, lineNumber := errObj.LineInfo()
lineInfo := fmt.Sprintf("%s:%d", path.Base(fileName), lineNumber)
suite.Require().True(strings.Contains(buf.String(), lineInfo))
suite.Require().True(strings.Contains(buf.String(), err.Error()))
}
depth := 2
buf.Reset()
PrintErrorStack(&buf, err, depth)
for _, err := range GetErrorStack(err, depth) {
errObj := err.(*Error)
fileName, lineNumber := errObj.LineInfo()
lineInfo := fmt.Sprintf("%s:%d", path.Base(fileName), lineNumber)
suite.Require().True(strings.Contains(buf.String(), lineInfo))
suite.Require().True(strings.Contains(buf.String(), err.Error()))
}
suite.Require().False(strings.Contains(buf.String(), "e3"))
}
func TestErrors(t *testing.T) {
suite.Run(t, new(ErrorsTestSuite))
}