-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstacktrace_test.go
More file actions
152 lines (145 loc) · 4.22 KB
/
Copy pathstacktrace_test.go
File metadata and controls
152 lines (145 loc) · 4.22 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
143
144
145
146
147
148
149
150
151
152
package xerrors
import (
"errors"
"fmt"
"io"
"reflect"
"regexp"
"testing"
)
func TestWithStackTrace(t *testing.T) {
tests := []struct {
err error
want string
}{
{err: Message("foo"), want: "foo"},
{err: io.EOF, want: "EOF"},
{err: nil, want: ""},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
err := WithStackTrace(tt.err, 0)
if tt.err == nil {
if err != nil {
t.Errorf("WithStackTrace(nil): must return nil")
}
} else {
if got := err.Error(); got != tt.want {
t.Errorf("WithStackTrace(%#v).Error(): got: %q, want: %q", tt.err, got, tt.want)
}
if len(StackTrace(err)) == 0 {
t.Errorf("WithStackTrace(%#v): returned error must contain a stack trace", tt.err)
}
if tt.err != nil {
if !errors.Is(err, tt.err) {
t.Errorf("errors.Is(WithStackTrace(%#v), err): must return true", tt.err)
}
if !errors.As(err, reflect.New(reflect.TypeOf(tt.err)).Interface()) {
t.Errorf("errors.As(WithStackTrace(%#v), err): must return true", tt.err)
}
}
}
})
}
}
func TestWithStackTraceFormat(t *testing.T) {
tests := []struct {
format string
err error
skip int
want string
regexp bool
}{
{format: "%s", err: Message(""), want: ``},
{format: "%s", err: New("foo"), want: `foo`},
{format: "%s", err: io.EOF, want: `EOF`},
{format: "%v", err: New("foo"), want: `foo`},
{format: "%q", err: Message("foo"), want: `"foo"`},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
var err error
func() {
// We are running this in a closure to test stack trace frames
// skipping.
err = WithStackTrace(tt.err, tt.skip)
}()
if got := fmt.Sprintf(tt.format, err); got != tt.want {
t.Errorf("fmt.Sprtinf(%q, WithStackTrace(%q)): got: %q, want: %q", tt.format, tt.err, got, tt.want)
}
})
}
}
func TestCallers_Empty(t *testing.T) {
var empty Callers
if got := empty.Frames(); len(got) != 0 {
t.Errorf("Callers(nil).Frames(): got: %#v, want: no frames", got)
}
if got := empty.String(); got != "" {
t.Errorf("Callers(nil).String(): got: %q, want: %q", got, "")
}
if got := StackTrace(io.EOF).Frames(); len(got) != 0 {
t.Errorf("StackTrace(io.EOF).Frames(): got: %#v, want: no frames", got)
}
if got := Sprint(WithStackTrace(io.EOF, 1000)); got != "Error: EOF\n" {
t.Errorf("Sprint(WithStackTrace(io.EOF, 1000)): got: %q, want: %q", got, "Error: EOF\n")
}
}
func TestCallers_FramesLength(t *testing.T) {
for _, frame := range callers(0).Frames() {
if frame.Function == "" {
t.Errorf("Callers.Frames(): must not return empty frames")
}
}
}
func TestFrameFormat(t *testing.T) {
frame := Frame{
File: "file",
Line: 42,
Function: "package/function",
}
tests := []struct {
format string
want string
regexp bool
}{
{format: "%s", want: "function (file:42)"},
{format: "%f", want: "file"},
{format: "%d", want: "42"},
{format: "%n", want: "function"},
{format: "%+n", want: "package/function"},
{format: "%+n", want: "package/function"},
{format: "%v", want: "function (file:42)"},
{format: "%+v", want: "{File:file Line:42 Function:package/function}"},
{format: "%#v", want: "xerrors._Frame{File:\"file\", Line:42, Function:\"package/function\"}"},
{format: "%q", want: "\"function (file:42)\""},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
if got := fmt.Sprintf(tt.format, frame); got != tt.want {
t.Errorf("fmt.Sprtinf(%q, %#v): got: %q, want: %q", tt.format, frame, got, tt.want)
}
})
}
}
func TestCallersFormat(t *testing.T) {
callers := callers(0)
tests := []struct {
format string
want string
}{
{format: "%s", want: `^at .*(\nat .*)+\n$`},
{format: "%v", want: `^at .*(\nat .*)+\n$`},
{format: "%+v", want: `\[([0-9 ])+\]`},
{format: "%#v", want: `^xerrors\._Callers\{(0x[a-f0-9]+, )*(0x[a-f0-9]+)\}$`},
{format: "%q", want: `^"at .*(\\nat .*)+\\n"$`},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := fmt.Sprintf(tt.format, callers)
if match, _ := regexp.MatchString(tt.want, got); !match {
t.Errorf("fmt.Sprtinf(%q, callers(0)): %q does not match %q", tt.format, got, tt.want)
}
})
}
}