-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktrace_test.go
210 lines (192 loc) · 4 KB
/
stacktrace_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cerrors
import (
"errors"
"fmt"
"regexp"
"runtime"
"strings"
"testing"
)
var initpc = caller()
type X struct{}
// val returns a Frame pointing to itself.
func (x X) val() Frame {
return caller()
}
// ptr returns a Frame pointing to itself.
func (x *X) ptr() Frame {
return caller()
}
func TestFrameFormat(t *testing.T) {
var tests = []struct {
Frame
format string
want string
}{{
initpc,
"%s",
"stacktrace_test.go",
}, {
initpc,
"%+s",
"github.com/sloory/cerrors.init\n" +
"\t.+/cerrors/stacktrace_test.go",
}, {
0,
"%s",
"unknown",
}, {
0,
"%+s",
"unknown",
}, {
initpc,
"%v",
"stacktrace_test.go:12",
}, {
initpc,
"%+v",
"github.com/sloory/cerrors.init\n" +
"\t.+/cerrors/stacktrace_test.go:12",
}, {
0,
"%v",
"unknown:0",
}}
for i, tt := range tests {
testFormatRegexp(t, i, tt.Frame, tt.format, tt.want)
}
}
func TestStackTrace(t *testing.T) {
tests := []struct {
err error
want []string
}{{
WithStack(errors.New("ooh")), []string{
"github.com/sloory/cerrors.TestStackTrace\n" +
"\t.+/cerrors/stacktrace_test.go:74",
},
}, {
Wrap("ahh",
WithStack(
errors.New("ooh"),
),
), []string{
"github.com/sloory/cerrors.TestStackTrace\n" +
"\t.+/cerrors/stacktrace_test.go:80", // this is the stack of newWithStack, not New
},
}, {
func() error {
return WithStack(errors.New("ooh"))
}(), []string{
`github.com/sloory/cerrors.TestStackTrace.func1` +
"\n\t.+/cerrors/stacktrace_test.go:89", // this is the stack of newWithStack
"github.com/sloory/cerrors.TestStackTrace\n" +
"\t.+/cerrors/stacktrace_test.go:90", // this is the stack of newWithStack's caller
},
}}
for i, tt := range tests {
var errWithStack interface {
StackTrace() StackTrace
}
if !errors.As(tt.err, &errWithStack) {
t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err)
continue
}
st := errWithStack.StackTrace()
for j, want := range tt.want {
testFormatRegexp(t, i, st[j], "%+v", want)
}
}
}
func stackTraceTest() StackTrace {
const depth = 8
var pcs [depth]uintptr
n := runtime.Callers(1, pcs[:])
f := make([]Frame, n)
for i := 0; i < n; i++ {
f[i] = Frame(pcs[i])
}
return f
}
func TestStackTraceFormat(t *testing.T) {
tests := []struct {
StackTrace
format string
want string
}{{
nil,
"%s",
`\[\]`,
}, {
nil,
"%v",
`\[\]`,
}, {
nil,
"%+v",
"",
}, {
make(StackTrace, 0),
"%s",
`\[\]`,
}, {
make(StackTrace, 0),
"%v",
`\[\]`,
}, {
make(StackTrace, 0),
"%+v",
"",
}, {
stackTraceTest()[:2],
"%s",
`\[stacktrace_test.go stacktrace_test.go\]`,
}, {
stackTraceTest()[:2],
"%v",
`\[stacktrace_test.go:117 stacktrace_test.go:159\]`,
}, {
stackTraceTest()[:2],
"%+v",
"\n" +
"github.com/sloory/cerrors.stackTraceTest\n" +
"\t.+/cerrors/stacktrace_test.go:117\n" +
"github.com/sloory/cerrors.TestStackTraceFormat\n" +
"\t.+/cerrors/stacktrace_test.go:163",
}, {
stackTraceTest()[:2],
"%#v",
`\[\]cerrors.Frame{stacktrace_test.go:117, stacktrace_test.go:171}`,
}}
for i, tt := range tests {
testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want)
}
}
// a version of runtime.Caller that returns a Frame, not a uintptr.
func caller() Frame {
var pcs [3]uintptr
n := runtime.Callers(2, pcs[:])
frames := runtime.CallersFrames(pcs[:n])
frame, _ := frames.Next()
return Frame(frame.PC)
}
func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
t.Helper()
got := fmt.Sprintf(format, arg)
gotLines := strings.SplitN(got, "\n", -1)
wantLines := strings.SplitN(want, "\n", -1)
if len(wantLines) > len(gotLines) {
t.Errorf("test %d: wantLines(%d) > gotLines(%d):\n got: %q\nwant: %q", n+1, len(wantLines), len(gotLines), got, want)
return
}
for i, w := range wantLines {
match, err := regexp.MatchString(w, gotLines[i])
if err != nil {
t.Fatal(err)
}
if !match {
t.Errorf("test %d: line %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, i+1, format, got, want)
}
}
}