-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstack_test.go
164 lines (154 loc) · 3.73 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
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
package errors
import (
"fmt"
"regexp"
"runtime"
"testing"
)
func TestStackTrace(t *testing.T) {
tests := []struct {
err error
want string
}{
{
New("ooh"),
"github.com/morrisxyang/errors.TestStackTrace" +
"\n\t.+errors/stack_test.go:16",
},
{
Wrap(New("ooh"), "ahh"),
"github.com/morrisxyang/errors.TestStackTrace" +
"\n\t.+errors/stack_test.go:21", // this is the stack of Wrap, not New
},
{
Cause(Wrap(New("ooh"), "ahh")),
"github.com/morrisxyang/errors.TestStackTrace" +
"\n\t.+errors/stack_test.go:26", // this is the stack of New
},
{
func() error { return New("ooh") }(),
`github.com/morrisxyang/errors.TestStackTrace.func1` +
"\n\t.+errors/stack_test.go:31" + "\n" + // this is the stack of New
"github.com/morrisxyang/errors.TestStackTrace" +
"\n\t.+errors/stack_test.go:31", // this is the stack of New's caller
},
{
Cause(func() error {
return func() error {
return Errorf("hello %s", fmt.Sprintf("world: %s", "ooh"))
}()
}()),
`github.com/morrisxyang/errors.TestStackTrace.func2.1` +
"\n\t.+errors/stack_test.go:40" + "\n" + // this is the stack of Errorf
`github.com/morrisxyang/errors.TestStackTrace.func2` +
"\n\t.+errors/stack_test.go:41" + "\n" + // this is the stack of Errorf's caller
"github.com/morrisxyang/errors.TestStackTrace" +
"\n\t.+errors/stack_test.go:42", // this is the stack of Errorf's caller's caller
},
}
for i, tt := range tests {
x, ok := tt.err.(interface {
StackTrace() StackTrace
})
if !ok {
t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err)
continue
}
st := x.StackTrace()
testFormatRegexp(t, i, st, "%+v", tt.want)
}
}
var initCallers = func() *StackTrace {
// maxDepth 记录的栈深度
const maxDepth = 64
var pcs [maxDepth]uintptr
n := runtime.Callers(2, pcs[:])
var stack *runtime.Frames
cfg := GetCfg()
if cfg.StackDepth > 0 && cfg.StackDepth < n {
stack = runtime.CallersFrames(pcs[0:cfg.StackDepth])
} else {
// 转换为 errors.StackTrace
stack = runtime.CallersFrames(pcs[0:n])
}
return &StackTrace{*stack}
}()
func TestStackTraceFormat(t *testing.T) {
tests := []struct {
*StackTrace
format string
want string
}{
{
&StackTrace{},
"%s",
`\[\]`,
},
{
&StackTrace{},
"%q",
`unsupported format: %!q, use %s: \[\]`,
},
{
&StackTrace{},
"%v",
`\[\]`,
},
{
&StackTrace{},
"%+v",
"",
},
{
&StackTrace{},
"%#v",
`\[\]`,
},
{
initCallers,
"%s",
`\[github.com/morrisxyang/errors.init runtime.doInit runtime.doInit runtime.main runtime.goexit\]`,
},
{
initCallers,
"%q",
`unsupported format: %!q, use %s: \[github.com/morrisxyang/errors.init runtime.doInit runtime.doInit ` +
`runtime.main runtime.goexit\]`,
},
{
initCallers,
"%v",
`\[github.com/morrisxyang/errors.init runtime.doInit runtime.doInit runtime.main runtime.goexit\]`,
},
{
initCallers,
"%+v",
`\ngithub.com/morrisxyang/errors.init\n\t.+errors/stack_test.go:81\nruntime.doInit\n\t.*`,
},
{
initCallers,
"%#v",
`\[github.com/morrisxyang/errors.init runtime.doInit runtime.doInit runtime.main runtime.goexit\]`,
},
{
(*StackTrace)(nil),
"%v",
`<nil>`,
},
}
for i, tt := range tests {
testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want)
}
}
func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
t.Helper()
got := fmt.Sprintf(format, arg)
match, err := regexp.MatchString(want, got)
if err != nil {
t.Fatal(err)
}
if !match {
t.Errorf("failed test %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, format, got, want)
}
t.Logf("success test %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, format, got, want)
}