Unverified Commit 16fea45c authored by Francesco Banconi's avatar Francesco Banconi Committed by GitHub

Merge pull request #37 from frankban/format-improvements

Improve formatting of errors values, stringers and strings
parents 10c956aa fc534f6e
......@@ -78,6 +78,19 @@ got:
want:
"47"
`,
}, {
about: "Equals: different strings with quotes",
checker: qt.Equals,
got: `string "foo"`,
args: []interface{}{`string "bar"`},
expectedCheckFailure: strings.Replace(`
error:
values are not equal
got:
~string "foo"~
want:
~string "bar"~
`, "~", "`", -1),
}, {
about: "Equals: different types",
checker: qt.Equals,
......@@ -118,6 +131,36 @@ got:
want:
nil
`,
}, {
about: "Equals: error is not nil: not formatted",
checker: qt.Equals,
got: &errTest{
msg: "bad wolf",
},
args: []interface{}{nil},
expectedCheckFailure: `
error:
got non-nil error
got:
e"bad wolf"
want:
nil
`,
}, {
about: "Equals: error is not nil: not formatted and with quotes",
checker: qt.Equals,
got: &errTest{
msg: `failure: "bad wolf"`,
},
args: []interface{}{nil},
expectedCheckFailure: strings.Replace(`
error:
got non-nil error
got:
e~failure: "bad wolf"~
want:
nil
`, "~", "`", -1),
}, {
about: "Equals: nil struct",
checker: qt.Equals,
......@@ -1486,7 +1529,7 @@ want args:
error:
unexpected success
arg:
bad check: bad wolf
e"bad check: bad wolf"
predicate function:
func(error) bool {...}
`,
......
......@@ -25,14 +25,20 @@ func TestIsBadCheck(t *testing.T) {
assertBool(t, qt.IsBadCheck(err), false)
}
var errBadWolf = &errTest{}
var errBadWolf = &errTest{
msg: "bad wolf",
formatted: true,
}
// errTest is an error type used in tests.
type errTest struct{}
type errTest struct {
msg string
formatted bool
}
// Error implements error.
func (*errTest) Error() string {
return "bad wolf"
func (err *errTest) Error() string {
return err.msg
}
// Format implements fmt.Formatter.
......@@ -41,5 +47,7 @@ func (err *errTest) Format(f fmt.State, c rune) {
fmt.Fprint(f, "unexpected verb for formatting the error")
}
fmt.Fprint(f, err.Error())
fmt.Fprint(f, "\n file:line")
if err.formatted {
fmt.Fprint(f, "\n file:line")
}
}
......@@ -4,6 +4,8 @@ package quicktest
import (
"fmt"
"strconv"
"strings"
"github.com/kr/pretty"
)
......@@ -13,12 +15,32 @@ import (
func Format(v interface{}) string {
switch v := v.(type) {
case error:
return fmt.Sprintf("%+v", v)
return formatErr(v)
case fmt.Stringer:
return fmt.Sprintf("s%q", v)
return "s" + quoteString(v.String())
case string:
return quoteString(v)
}
// The pretty.Sprint equivalent does not quote string values.
return fmt.Sprintf("%# v", pretty.Formatter(v))
}
func formatErr(err error) string {
s := fmt.Sprintf("%+v", err)
if s != err.Error() {
// The error has formatted itself with additional information.
// Leave that as is.
return s
}
return "e" + quoteString(s)
}
func quoteString(s string) string {
// TODO think more about what to do about multi-line strings.
if strings.Contains(s, `"`) && !strings.Contains(s, "\n") && strconv.CanBackquote(s) {
return "`" + s + "`"
}
return strconv.Quote(s)
}
type formatFunc func(interface{}) string
......@@ -17,14 +17,57 @@ var formatTests = []struct {
about: "error value",
value: errBadWolf,
want: "bad wolf\n file:line",
}, {
about: "error value: not formatted",
value: &errTest{
msg: "exterminate!",
},
want: `e"exterminate!"`,
}, {
about: "error value: with quotes",
value: &errTest{
msg: `cannot open "/no/such/file"`,
},
want: "e`cannot open \"/no/such/file\"`",
}, {
about: "error value: multi-line",
value: &errTest{
msg: `err:
"these are the voyages"`,
},
want: `e"err:\n\"these are the voyages\""`,
}, {
about: "error value: with backquotes",
value: &errTest{
msg: "cannot `open` \"file\"",
},
want: `e"cannot ` + "`open`" + ` \"file\""`,
}, {
about: "stringer",
value: bytes.NewBufferString("I am a stringer"),
want: `s"I am a stringer"`,
}, {
about: "stringer: with quotes",
value: bytes.NewBufferString(`I say "hello"`),
want: "s`I say \"hello\"`",
}, {
about: "string",
value: "these are the voyages",
want: `"these are the voyages"`,
}, {
about: "string: with quotes",
value: `here is a quote: "`,
want: "`here is a quote: \"`",
}, {
about: "string: multi-line",
value: `foo
"bar"
`,
want: `"foo\n\"bar\"\n"`,
}, {
about: "string: with backquotes",
value: `"` + "`",
want: `"\"` + "`\"",
}, {
about: "slice",
value: []int{1, 2, 3},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment