| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package errors
- import (
- "fmt"
- "io"
- "regexp"
- "strings"
- "testing"
- )
- func TestFormatNew(t *testing.T) {
- tests := []struct {
- error
- format string
- want string
- }{{
- New("error"),
- "%s",
- "error",
- }, {
- New("error"),
- "%v",
- "error",
- }, {
- New("error"),
- "%+v",
- "error\n" +
- "github.com/pkg/errors.TestFormatNew\n" +
- "\t.+/github.com/pkg/errors/format_test.go:25",
- }}
- for _, tt := range tests {
- testFormatRegexp(t, tt.error, tt.format, tt.want)
- }
- }
- func TestFormatErrorf(t *testing.T) {
- tests := []struct {
- error
- format string
- want string
- }{{
- Errorf("%s", "error"),
- "%s",
- "error",
- }, {
- Errorf("%s", "error"),
- "%v",
- "error",
- }, {
- Errorf("%s", "error"),
- "%+v",
- "error\n" +
- "github.com/pkg/errors.TestFormatErrorf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:51",
- }}
- for _, tt := range tests {
- testFormatRegexp(t, tt.error, tt.format, tt.want)
- }
- }
- func TestFormatWrap(t *testing.T) {
- tests := []struct {
- error
- format string
- want string
- }{{
- Wrap(New("error"), "error2"),
- "%s",
- "error2: error",
- }, {
- Wrap(New("error"), "error2"),
- "%v",
- "error2: error",
- }, {
- Wrap(New("error"), "error2"),
- "%+v",
- "error\n" +
- "github.com/pkg/errors.TestFormatWrap\n" +
- "\t.+/github.com/pkg/errors/format_test.go:77",
- }, {
- Wrap(io.EOF, "error"),
- "%s",
- "error: EOF",
- }}
- for _, tt := range tests {
- testFormatRegexp(t, tt.error, tt.format, tt.want)
- }
- }
- func TestFormatWrapf(t *testing.T) {
- tests := []struct {
- error
- format string
- want string
- }{{
- Wrapf(New("error"), "error%d", 2),
- "%s",
- "error2: error",
- }, {
- Wrap(io.EOF, "error"),
- "%v",
- "error: EOF",
- }, {
- Wrap(io.EOF, "error"),
- "%+v",
- "EOF\n" +
- "github.com/pkg/errors.TestFormatWrapf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:107: error",
- }, {
- Wrapf(New("error"), "error%d", 2),
- "%v",
- "error2: error",
- }, {
- Wrapf(New("error"), "error%d", 2),
- "%+v",
- "error\n" +
- "github.com/pkg/errors.TestFormatWrapf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:117",
- }}
- for _, tt := range tests {
- testFormatRegexp(t, tt.error, tt.format, tt.want)
- }
- }
- func testFormatRegexp(t *testing.T, arg interface{}, format, want string) {
- got := fmt.Sprintf(format, arg)
- 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)
- }
- }
- }
|