Debian Codegraph @

stack_test.go

raw symbols SCIP
1 package errors
2
3 import (
4 "fmt"
5 "regexp"
6 "runtime"
7 "strings"
8 "testing"
9 )
10
11 var initpc = caller()
12
13 func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
14 t.Helper()
15 got := fmt.Sprintf(format, arg)
16 gotLines := strings.SplitN(got, "\n", -1)
17 wantLines := strings.SplitN(want, "\n", -1)
18
19 if len(wantLines) > len(gotLines) {
20 t.Errorf("test %d: wantLines(%d) > gotLines(%d):\n got: %q\nwant: %q", n+1, len(wantLines), len(gotLines), got, want)
21 return
22 }
23
24 for i, w := range wantLines {
25 match, err := regexp.MatchString(w, gotLines[i])
26 if err != nil {
27 t.Fatal(err)
28 }
29 if !match {
30 t.Errorf("test %d: line %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, i+1, format, got, want)
31 }
32 }
33 }
34
35 type X struct{}
36
37 // val returns a Frame pointing to itself.
38 func (x X) val() Frame {
39 return caller()
40 }
41
42 // ptr returns a Frame pointing to itself.
43 func (x *X) ptr() Frame {
44 return caller()
45 }
46
47 func TestFrameFormat(t *testing.T) {
48 var tests = []struct {
49 Frame
50 format string
51 want string
52 }{{
53 initpc,
54 "%s",
55 "stack_test.go",
56 }, {
57 initpc,
58 "%+s",
59 "github.com/go-ap/errors.init\n" +
60 "\t.+/errors/stack_test.go",
61 }, {
62 0,
63 "%s",
64 "unknown",
65 }, {
66 0,
67 "%+s",
68 "unknown",
69 }, {
70 initpc,
71 "%d",
72 "11",
73 }, {
74 0,
75 "%d",
76 "0",
77 }, {
78 initpc,
79 "%n",
80 "init",
81 }, {
82 func() Frame {
83 var x X
84 return x.ptr()
85 }(),
86 "%n",
87 `\(\*X\).ptr`,
88 }, {
89 func() Frame {
90 var x X
91 return x.val()
92 }(),
93 "%n",
94 "X.val",
95 }, {
96 0,
97 "%n",
98 "",
99 }, {
100 initpc,
101 "%v",
102 "stack_test.go:11",
103 }, {
104 initpc,
105 "%+v",
106 "github.com/go-ap/errors.init\n" +
107 "\t.+/errors/stack_test.go:11",
108 }, {
109 0,
110 "%v",
111 "unknown:0",
112 }}
113
114 for i, tt := range tests {
115 testFormatRegexp(t, i, tt.Frame, tt.format, tt.want)
116 }
117 }
118
119 func TestFuncname(t *testing.T) {
120 tests := []struct {
121 name, want string
122 }{
123 {"", ""},
124 {"runtime.main", "main"},
125 {"github.com/go-ap/errors.funcname", "funcname"},
126 {"funcname", "funcname"},
127 {"io.copyBuffer", "copyBuffer"},
128 {"main.(*R).Write", "(*R).Write"},
129 }
130
131 for _, tt := range tests {
132 got := funcname(tt.name)
133 want := tt.want
134 if got != want {
135 t.Errorf("funcname(%q): want: %q, got %q", tt.name, want, got)
136 }
137 }
138 }
139
140 func TestStackTrace(t *testing.T) {
141 tests := []struct {
142 err error
143 want []string
144 }{{
145 Newf("ooh"), []string{
146 "github.com/go-ap/errors.TestStackTrace\n" +
147 "\t.+/errors/stack_test.go:145",
148 },
149 }, {
150 wrap(Newf("ooh"), "ahh"), []string{
151 "github.com/go-ap/errors.TestStackTrace\n" +
152 "\t.+/errors/stack_test.go:150", // this is the stack of Wrap, not New
153 },
154 }, {
155 func() error { return Newf("ooh") }(), []string{
156 `github.com/go-ap/errors.TestStackTrace.func1` +
157 "\n\t.+/errors/stack_test.go:155", // this is the stack of New
158 "github.com/go-ap/errors.TestStackTrace\n" +
159 "\t.+/errors/stack_test.go:155", // this is the stack of New's caller
160 },
161 }}
162 t.Skipf(`TODO(marius): This needs some more work
163 As going one level up the stack in stack.callers() removes meaningful information from the tests`)
164 for i, tt := range tests {
165 x, ok := tt.err.(interface {
166 StackTrace() StackTrace
167 })
168 if !ok {
169 t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err)
170 continue
171 }
172 st := x.StackTrace()
173 if len(st) == 0 {
174 continue
175 }
176 for j, want := range tt.want {
177 testFormatRegexp(t, i, st[j], "%+v", want)
178 }
179 }
180 }
181
182 func stackTrace() StackTrace {
183 const depth = 8
184 var pcs [depth]uintptr
185 n := runtime.Callers(1, pcs[:])
186 var st stack = pcs[0:n]
187 return st.StackTrace()
188 }
189
190 func TestStackTraceFormat(t *testing.T) {
191 tests := []struct {
192 StackTrace
193 format string
194 want string
195 }{{
196 nil,
197 "%s",
198 `\[\]`,
199 }, {
200 nil,
201 "%v",
202 `\[\]`,
203 }, {
204 nil,
205 "%+v",
206 "",
207 }, {
208 nil,
209 "%#v",
210 `\[\]errors.Frame\(nil\)`,
211 }, {
212 make(StackTrace, 0),
213 "%s",
214 `\[\]`,
215 }, {
216 make(StackTrace, 0),
217 "%v",
218 `\[\]`,
219 }, {
220 make(StackTrace, 0),
221 "%+v",
222 "",
223 }, {
224 make(StackTrace, 0),
225 "%#v",
226 `\[\]errors.Frame{}`,
227 }, {
228 stackTrace()[:2],
229 "%s",
230 `\[stack_test.go stack_test.go\]`,
231 }, {
232 stackTrace()[:2],
233 "%v",
234 `\[stack_test.go:183 stack_test.go:230\]`,
235 }, {
236 stackTrace()[:2],
237 "%+v",
238 "\n" +
239 "github.com/go-ap/errors.stackTrace\n" +
240 "\t.+/errors/stack_test.go:183\n" +
241 "github.com/go-ap/errors.TestStackTraceFormat\n" +
242 "\t.+/errors/stack_test.go:234",
243 }, {
244 stackTrace()[:2],
245 "%#v",
246 `\[\]errors.Frame{stack_test.go:183, stack_test.go:242}`,
247 }}
248
249 t.Skipf(`TODO(marius): This needs some more work
250 As going one level up the stack in stack.callers() removes meaningful information from the tests`)
251 for i, tt := range tests {
252 testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want)
253 }
254 }
255
256 // a version of runtime.Caller that returns a Frame, not a uintptr.
257 func caller() Frame {
258 var pcs [3]uintptr
259 n := runtime.Callers(2, pcs[:])
260 frames := runtime.CallersFrames(pcs[:n])
261 frame, _ := frames.Next()
262 return Frame(frame.PC)
263 }