Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Extended stacktrace output #49

Merged
merged 6 commits into from
Jun 11, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make format tests less sensitive to sourcecode prefix
  • Loading branch information
davecheney committed Jun 11, 2016
commit e172069e91f251bf78272a5b955c1fa00c87d327
43 changes: 24 additions & 19 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ package errors
import (
"fmt"
"io"
"regexp"
"strings"
"testing"
)

func testFormat(t *testing.T, err error, format, want string) {
got := fmt.Sprintf(format, err)
lines := strings.SplitN(got, "\n", -1)
for i, w := range strings.SplitN(want, "\n", -1) {
if lines[i] != w {
t.Errorf("fmt.Sprintf(%q, err): got: %q, want: %q", format, got, want)
}
}
}

func TestFormatNew(t *testing.T) {
tests := []struct {
error
Expand All @@ -35,11 +26,11 @@ func TestFormatNew(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatNew\n" +
"\t/home/dfc/src/github.com/pkg/errors/format_test.go:34",
"\t/.+/github.com/pkg/errors/format_test.go:25",
}}

for _, tt := range tests {
testFormat(t, tt.error, tt.format, tt.want)
testFormatRegexp(t, tt.error, tt.format, tt.want)
}
}

Expand All @@ -61,11 +52,11 @@ func TestFormatErrorf(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatErrorf\n" +
"\t/home/dfc/src/github.com/pkg/errors/format_test.go:60",
"\t/.+/github.com/pkg/errors/format_test.go:51",
}}

for _, tt := range tests {
testFormat(t, tt.error, tt.format, tt.want)
testFormatRegexp(t, tt.error, tt.format, tt.want)
}
}

Expand All @@ -87,15 +78,15 @@ func TestFormatWrap(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatWrap\n" +
"\t/home/dfc/src/github.com/pkg/errors/format_test.go:86",
"\t/.+/github.com/pkg/errors/format_test.go:77",
}, {
Wrap(io.EOF, "error"),
"%s",
"error: EOF",
}}

for _, tt := range tests {
testFormat(t, tt.error, tt.format, tt.want)
testFormatRegexp(t, tt.error, tt.format, tt.want)
}
}

Expand All @@ -117,7 +108,7 @@ func TestFormatWrapf(t *testing.T) {
"%+v",
"EOF\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
"\t/home/dfc/src/github.com/pkg/errors/format_test.go:116: error",
"\t/.+/github.com/pkg/errors/format_test.go:107: error",
}, {
Wrapf(New("error"), "error%d", 2),
"%v",
Expand All @@ -127,10 +118,24 @@ func TestFormatWrapf(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
"\t/home/dfc/src/github.com/pkg/errors/format_test.go:126",
"\t/.+/github.com/pkg/errors/format_test.go:117",
}}

for _, tt := range tests {
testFormat(t, tt.error, tt.format, tt.want)
testFormatRegexp(t, tt.error, tt.format, tt.want)
}
}

func testFormatRegexp(t *testing.T, err error, format, want string) {
got := fmt.Sprintf(format, err)
lines := strings.SplitN(got, "\n", -1)
for i, w := range strings.SplitN(want, "\n", -1) {
match, err := regexp.MatchString(w, lines[i])
if err != nil {
t.Fatal(err)
}
if !match {
t.Errorf("fmt.Sprintf(%q, err): got: %q, want: %q", format, got, want)
}
}
}