errors/format_test.go
Dave Cheney c0c662e216 Use fmt.Formatter throughout (#40)
* Use fmt.Formatter throughout

Replace Fprintf with fmt.Formatter logic on various types. This
effectively guts Fprint to be just a recursive call down the err.Cause
chain. The next step will be to move this recursive logic into
wrapper.Format.

This change necessitates adding types for the error impls returned from
New and Errorf, and Wrap and Wrapf, respectively. The name of the latter
type is acceptable, the former is not and alternative suggestions are
encouraged.

- Remove cause.Format
- Added fmt.Formatter tests. The location is temporary, once this PR is merged and Fprint removed
they will be merged into errors_test.go
2016-06-09 16:45:08 +10:00

71 lines
1.1 KiB
Go

package errors
import (
"fmt"
"testing"
)
func TestFormat(t *testing.T) {
tests := []struct {
error
format string
want string
}{{
New("error"),
"%s",
"error",
}, {
New("error"),
"%v",
"error",
}, {
New("error"),
"%+v",
"github.com/pkg/errors/format_test.go:23: error",
}, {
Errorf("%s", "error"),
"%s",
"error",
}, {
Errorf("%s", "error"),
"%v",
"error",
}, {
Errorf("%s", "error"),
"%+v",
"github.com/pkg/errors/format_test.go:35: error",
}, {
Wrap(New("error"), "error2"),
"%s",
"error2: error",
}, {
Wrap(New("error"), "error2"),
"%v",
"error2: error",
}, {
Wrap(New("error"), "error2"),
"%+v",
"github.com/pkg/errors/format_test.go:47: error2",
}, {
Wrapf(New("error"), "error%d", 2),
"%s",
"error2: error",
}, {
Wrapf(New("error"), "error%d", 2),
"%v",
"error2: error",
}, {
Wrapf(New("error"), "error%d", 2),
"%+v",
"github.com/pkg/errors/format_test.go:59: error2",
}}
for _, tt := range tests {
got := fmt.Sprintf(tt.format, tt.error)
if got != tt.want {
t.Errorf("fmt.Sprintf(%q, err): got: %q, want: %q", tt.format, got, tt.want)
}
}
}