9 var initpc, _, _, _ = runtime.Caller(0)
11 func TestFrameLine(t *testing.T) {
12 var tests = []struct {
20 var pc, _, _, _ = runtime.Caller(0)
26 var pc, _, _, _ = runtime.Caller(1)
31 Frame(0), // invalid PC
35 for _, tt := range tests {
36 got := tt.Frame.line()
39 t.Errorf("Frame(%v): want: %v, got: %v", uintptr(tt.Frame), want, got)
46 func (x X) val() Frame {
47 var pc, _, _, _ = runtime.Caller(0)
51 func (x *X) ptr() Frame {
52 var pc, _, _, _ = runtime.Caller(0)
56 func TestFrameFormat(t *testing.T) {
57 var tests = []struct {
68 "github.com/pkg/errors.init\n" +
69 "\t.+/github.com/pkg/errors/stack_test.go",
115 "github.com/pkg/errors.init\n" +
116 "\t.+/github.com/pkg/errors/stack_test.go:9",
123 for i, tt := range tests {
124 testFormatRegexp(t, i, tt.Frame, tt.format, tt.want)
128 func TestFuncname(t *testing.T) {
133 {"runtime.main", "main"},
134 {"github.com/pkg/errors.funcname", "funcname"},
135 {"funcname", "funcname"},
136 {"io.copyBuffer", "copyBuffer"},
137 {"main.(*R).Write", "(*R).Write"},
140 for _, tt := range tests {
141 got := funcname(tt.name)
144 t.Errorf("funcname(%q): want: %q, got %q", tt.name, want, got)
149 func TestTrimGOPATH(t *testing.T) {
150 var tests = []struct {
155 "github.com/pkg/errors/stack_test.go",
158 for i, tt := range tests {
160 fn := runtime.FuncForPC(pc)
161 file, _ := fn.FileLine(pc)
162 got := trimGOPATH(fn.Name(), file)
163 testFormatRegexp(t, i, got, "%s", tt.want)
167 func TestStackTrace(t *testing.T) {
172 New("ooh"), []string{
173 "github.com/pkg/errors.TestStackTrace\n" +
174 "\t.+/github.com/pkg/errors/stack_test.go:172",
177 Wrap(New("ooh"), "ahh"), []string{
178 "github.com/pkg/errors.TestStackTrace\n" +
179 "\t.+/github.com/pkg/errors/stack_test.go:177", // this is the stack of Wrap, not New
182 Cause(Wrap(New("ooh"), "ahh")), []string{
183 "github.com/pkg/errors.TestStackTrace\n" +
184 "\t.+/github.com/pkg/errors/stack_test.go:182", // this is the stack of New
187 func() error { return New("ooh") }(), []string{
188 `github.com/pkg/errors.(func·009|TestStackTrace.func1)` +
189 "\n\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New
190 "github.com/pkg/errors.TestStackTrace\n" +
191 "\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New's caller
195 return func() error {
196 return Errorf("hello %s", fmt.Sprintf("world"))
199 `github.com/pkg/errors.(func·010|TestStackTrace.func2.1)` +
200 "\n\t.+/github.com/pkg/errors/stack_test.go:196", // this is the stack of Errorf
201 `github.com/pkg/errors.(func·011|TestStackTrace.func2)` +
202 "\n\t.+/github.com/pkg/errors/stack_test.go:197", // this is the stack of Errorf's caller
203 "github.com/pkg/errors.TestStackTrace\n" +
204 "\t.+/github.com/pkg/errors/stack_test.go:198", // this is the stack of Errorf's caller's caller
207 for i, tt := range tests {
208 x, ok := tt.err.(interface {
209 StackTrace() StackTrace
212 t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err)
216 for j, want := range tt.want {
217 testFormatRegexp(t, i, st[j], "%+v", want)
222 func stackTrace() StackTrace {
224 var pcs [depth]uintptr
225 n := runtime.Callers(1, pcs[:])
226 var st stack = pcs[0:n]
227 return st.StackTrace()
230 func TestStackTraceFormat(t *testing.T) {
250 `\[\]errors.Frame\(nil\)`,
266 `\[\]errors.Frame{}`,
270 `\[stack_test.go stack_test.go\]`,
274 `\[stack_test.go:225 stack_test.go:272\]`,
279 "github.com/pkg/errors.stackTrace\n" +
280 "\t.+/github.com/pkg/errors/stack_test.go:225\n" +
281 "github.com/pkg/errors.TestStackTraceFormat\n" +
282 "\t.+/github.com/pkg/errors/stack_test.go:276",
286 `\[\]errors.Frame{stack_test.go:225, stack_test.go:284}`,
289 for i, tt := range tests {
290 testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want)