Skip to content

Commit

Permalink
Remove deprecated Stack() []uintptr interface (pkg#45)
Browse files Browse the repository at this point in the history
* Remove deprecated Stack() []uintptr interface

* move TestStack to TestStacktrace
  • Loading branch information
davecheney committed Jun 9, 2016
1 parent 89edb63 commit 5776abf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
55 changes: 0 additions & 55 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,61 +197,6 @@ func TestErrorf(t *testing.T) {
}
}

func TestStack(t *testing.T) {
type fileline struct {
file string
line int
}
tests := []struct {
err error
want []fileline
}{{
New("ooh"), []fileline{
{"github.com/pkg/errors/errors_test.go", 209},
},
}, {
Wrap(New("ooh"), "ahh"), []fileline{
{"github.com/pkg/errors/errors_test.go", 213}, // this is the stack of Wrap, not New
},
}, {
Cause(Wrap(New("ooh"), "ahh")), []fileline{
{"github.com/pkg/errors/errors_test.go", 217}, // this is the stack of New
},
}, {
func() error { return New("ooh") }(), []fileline{
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New's caller
},
}, {
Cause(func() error {
return func() error {
return Errorf("hello %s", fmt.Sprintf("world"))
}()
}()), []fileline{
{"github.com/pkg/errors/errors_test.go", 228}, // this is the stack of Errorf
{"github.com/pkg/errors/errors_test.go", 229}, // this is the stack of Errorf's caller
{"github.com/pkg/errors/errors_test.go", 230}, // this is the stack of Errorf's caller's caller
},
}}
for _, tt := range tests {
x, ok := tt.err.(interface {
Stack() []uintptr
})
if !ok {
t.Errorf("expected %#v to implement Stack()", tt.err)
continue
}
st := x.Stack()
for i, want := range tt.want {
frame := Frame(st[i])
file, line := fmt.Sprintf("%+s", frame), frame.line()
if file != want.file || line != want.line {
t.Errorf("frame %d: expected %s:%d, got %s:%d", i, want.file, want.line, file, line)
}
}
}
}

// errors.New, etc values are not expected to be compared by value
// but the change in errors#27 made them incomparable. Assert that
// various kinds of errors have a functional equality operator, even
Expand Down
3 changes: 0 additions & 3 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func (f Frame) Format(s fmt.State, verb rune) {
// stack represents a stack of program counters.
type stack []uintptr

// Deprecated: use Stacktrace()
func (s *stack) Stack() []uintptr { return *s }

func (s *stack) Stacktrace() []Frame {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
Expand Down
55 changes: 55 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,58 @@ func TestTrimGOPATH(t *testing.T) {
}
}
}

func TestStacktrace(t *testing.T) {
type fileline struct {
file string
line int
}
tests := []struct {
err error
want []fileline
}{{
New("ooh"), []fileline{
{"github.com/pkg/errors/stack_test.go", 181},
},
}, {
Wrap(New("ooh"), "ahh"), []fileline{
{"github.com/pkg/errors/stack_test.go", 185}, // this is the stack of Wrap, not New
},
}, {
Cause(Wrap(New("ooh"), "ahh")), []fileline{
{"github.com/pkg/errors/stack_test.go", 189}, // this is the stack of New
},
}, {
func() error { return New("ooh") }(), []fileline{
{"github.com/pkg/errors/stack_test.go", 193}, // this is the stack of New
{"github.com/pkg/errors/stack_test.go", 193}, // this is the stack of New's caller
},
}, {
Cause(func() error {
return func() error {
return Errorf("hello %s", fmt.Sprintf("world"))
}()
}()), []fileline{
{"github.com/pkg/errors/stack_test.go", 200}, // this is the stack of Errorf
{"github.com/pkg/errors/stack_test.go", 201}, // this is the stack of Errorf's caller
{"github.com/pkg/errors/stack_test.go", 202}, // this is the stack of Errorf's caller's caller
},
}}
for _, tt := range tests {
x, ok := tt.err.(interface {
Stacktrace() []Frame
})
if !ok {
t.Errorf("expected %#v to implement Stacktrace() []Frame", tt.err)
continue
}
st := x.Stacktrace()
for i, want := range tt.want {
frame := st[i]
file, line := fmt.Sprintf("%+s", frame), frame.line()
if file != want.file || line != want.line {
t.Errorf("frame %d: expected %s:%d, got %s:%d", i, want.file, want.line, file, line)
}
}
}
}

0 comments on commit 5776abf

Please sign in to comment.