-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_test.go
62 lines (55 loc) · 1.39 KB
/
stack_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
package errors
import (
"bytes"
"errors"
"fmt"
"testing"
)
func TestWithStack(t *testing.T) {
stdErr := errors.New("error")
libErr := New("lib error")
type testcase []struct {
name string
err error
}
tests := testcase{
{name: "std error", err: stdErr},
{name: "lib error", err: libErr},
{name: "error wrapping std", err: Wrap(stdErr, "std wrap")},
{name: "error wrapping lib", err: Wrap(libErr, "lib wrap")},
{name: "error wrapping fmt", err: fmt.Errorf("wrap is:%w", stdErr)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := WithStack(tt.err)
if !errors.Is(err, tt.err) {
t.Error("Unwrap() may not be implemented.")
}
msg := tt.err.Error()
if err.Error() != msg {
t.Errorf("unmatch Error() message. out=%s, want=%s", err.Error(), msg)
}
// check implements fmt.Formatter
var buf bytes.Buffer
if _, err := buf.WriteString(fmt.Sprintf("%s\n", err)); err != nil {
t.Errorf("error unexpected. err=%s", err)
}
if _, err := buf.WriteString(fmt.Sprintf("%v", err)); err != nil {
t.Errorf("error unexpected. err=%v", err)
}
// output log
t.Logf("%v", buf.String())
})
}
tests = testcase{
{name: "args error is nil", err: nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := WithStack(tt.err)
if err != nil {
t.Errorf("WithStack returns not nil:%v", err)
}
})
}
}