From 5776abf3b94c5694cfb2a5a834486c3fb12fe75d Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Fri, 10 Jun 2016 00:48:07 +1000 Subject: [PATCH] Remove deprecated Stack() []uintptr interface (#45) * Remove deprecated Stack() []uintptr interface * move TestStack to TestStacktrace --- errors_test.go | 55 -------------------------------------------------- stack.go | 3 --- stack_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 58 deletions(-) diff --git a/errors_test.go b/errors_test.go index d3780f1..30796d8 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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 diff --git a/stack.go b/stack.go index 882e051..4794a2f 100644 --- a/stack.go +++ b/stack.go @@ -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++ { diff --git a/stack_test.go b/stack_test.go index 36dbe6d..1272c95 100644 --- a/stack_test.go +++ b/stack_test.go @@ -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) + } + } + } +}