Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 011399d

Browse files
fabstudavecheney
authored andcommitted
Add WithStack and WithMessage tests
Adds testFormatCompleteCompare as additional testing func. The new function takes a string slice as "want", wherein stacktraces and non-stacktrace messages are discerned by strings.ContainsAny(want[i], "\n"). For example usage, see TestFormatWithStack & TestFormatWithMessage.
1 parent 4f8d1cf commit 011399d

File tree

3 files changed

+392
-16
lines changed

3 files changed

+392
-16
lines changed

errors_test.go

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func TestCause(t *testing.T) {
8484
}, {
8585
err: x, // return from errors.New
8686
want: x,
87+
}, {
88+
WithMessage(nil, "whoops"),
89+
nil,
90+
}, {
91+
WithMessage(io.EOF, "whoops"),
92+
io.EOF,
93+
}, {
94+
WithStack(nil),
95+
nil,
96+
}, {
97+
WithStack(io.EOF),
98+
io.EOF,
8799
}}
88100

89101
for i, tt := range tests {
@@ -137,23 +149,78 @@ func TestErrorf(t *testing.T) {
137149
}
138150
}
139151

152+
func TestWithStackNil(t *testing.T) {
153+
got := WithStack(nil)
154+
if got != nil {
155+
t.Errorf("WithStack(nil): got %#v, expected nil", got)
156+
}
157+
}
158+
159+
func TestWithStack(t *testing.T) {
160+
tests := []struct {
161+
err error
162+
want string
163+
}{
164+
{io.EOF, "EOF"},
165+
{WithStack(io.EOF), "EOF"},
166+
}
167+
168+
for _, tt := range tests {
169+
got := WithStack(tt.err).Error()
170+
if got != tt.want {
171+
t.Errorf("WithStack(%v): got: %v, want %v", tt.err, got, tt.want)
172+
}
173+
}
174+
}
175+
176+
func TestWithMessageNil(t *testing.T) {
177+
got := WithMessage(nil, "no error")
178+
if got != nil {
179+
t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)
180+
}
181+
}
182+
183+
func TestWithMessage(t *testing.T) {
184+
tests := []struct {
185+
err error
186+
message string
187+
want string
188+
}{
189+
{io.EOF, "read error", "read error: EOF"},
190+
{WithMessage(io.EOF, "read error"), "client error", "client error: read error: EOF"},
191+
}
192+
193+
for _, tt := range tests {
194+
got := WithMessage(tt.err, tt.message).Error()
195+
if got != tt.want {
196+
t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)
197+
}
198+
}
199+
200+
}
201+
140202
// errors.New, etc values are not expected to be compared by value
141203
// but the change in errors#27 made them incomparable. Assert that
142204
// various kinds of errors have a functional equality operator, even
143205
// if the result of that equality is always false.
144206
func TestErrorEquality(t *testing.T) {
145-
tests := []struct {
146-
err1, err2 error
147-
}{
148-
{io.EOF, io.EOF},
149-
{io.EOF, nil},
150-
{io.EOF, errors.New("EOF")},
151-
{io.EOF, New("EOF")},
152-
{New("EOF"), New("EOF")},
153-
{New("EOF"), Errorf("EOF")},
154-
{New("EOF"), Wrap(io.EOF, "EOF")},
207+
vals := []error{
208+
nil,
209+
io.EOF,
210+
errors.New("EOF"),
211+
New("EOF"),
212+
Errorf("EOF"),
213+
Wrap(io.EOF, "EOF"),
214+
Wrapf(io.EOF, "EOF%d", 2),
215+
WithMessage(nil, "whoops"),
216+
WithMessage(io.EOF, "whoops"),
217+
WithStack(io.EOF),
218+
WithStack(nil),
155219
}
156-
for _, tt := range tests {
157-
_ = tt.err1 == tt.err2 // mustn't panic
220+
221+
for i := 0; i < len(vals); i++ {
222+
for j := 0; j < len(vals); j++ {
223+
_ = vals[i] == vals[j] // mustn't panic
224+
}
158225
}
159226
}

example_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,59 @@ func ExampleNew_printf() {
3535
// /home/dfc/go/src/runtime/asm_amd64.s:2059
3636
}
3737

38+
func ExampleWithMessage() {
39+
cause := errors.New("whoops")
40+
err := errors.WithMessage(cause, "oh noes")
41+
fmt.Println(err)
42+
43+
// Output: oh noes: whoops
44+
}
45+
46+
func ExampleWithStack() {
47+
cause := errors.New("whoops")
48+
err := errors.WithStack(cause)
49+
fmt.Println(err)
50+
51+
// Output: whoops
52+
}
53+
54+
func ExampleWithStack_printf() {
55+
cause := errors.New("whoops")
56+
err := errors.WithStack(cause)
57+
fmt.Printf("%+v", err)
58+
59+
// Example Output:
60+
// whoops
61+
// github.com/pkg/errors_test.ExampleWithStack_printf
62+
// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:55
63+
// testing.runExample
64+
// /usr/lib/go/src/testing/example.go:114
65+
// testing.RunExamples
66+
// /usr/lib/go/src/testing/example.go:38
67+
// testing.(*M).Run
68+
// /usr/lib/go/src/testing/testing.go:744
69+
// main.main
70+
// github.com/pkg/errors/_test/_testmain.go:106
71+
// runtime.main
72+
// /usr/lib/go/src/runtime/proc.go:183
73+
// runtime.goexit
74+
// /usr/lib/go/src/runtime/asm_amd64.s:2086
75+
// github.com/pkg/errors_test.ExampleWithStack_printf
76+
// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:56
77+
// testing.runExample
78+
// /usr/lib/go/src/testing/example.go:114
79+
// testing.RunExamples
80+
// /usr/lib/go/src/testing/example.go:38
81+
// testing.(*M).Run
82+
// /usr/lib/go/src/testing/testing.go:744
83+
// main.main
84+
// github.com/pkg/errors/_test/_testmain.go:106
85+
// runtime.main
86+
// /usr/lib/go/src/runtime/proc.go:183
87+
// runtime.goexit
88+
// /usr/lib/go/src/runtime/asm_amd64.s:2086
89+
}
90+
3891
func ExampleWrap() {
3992
cause := errors.New("whoops")
4093
err := errors.Wrap(cause, "oh noes")

0 commit comments

Comments
 (0)