Commit 750ea19e authored by Francesco Banconi's avatar Francesco Banconi

Improve error testing

parent 55e64ad8
......@@ -62,7 +62,7 @@ func (c *equalsChecker) Check(got interface{}, args []interface{}, note func(key
}()
if want := args[0]; got != want {
if _, ok := got.(error); ok && want == nil {
return errors.New("provided error is not nil")
return errors.New("got non-nil error")
}
return errors.New("values are not equal")
}
......
......@@ -4,7 +4,6 @@ package quicktest_test
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
......@@ -16,10 +15,7 @@ import (
qt "github.com/frankban/quicktest"
)
var (
errBadWolf = errors.New("bad wolf")
goTime = time.Date(2012, 3, 28, 0, 0, 0, 0, time.UTC)
)
var goTime = time.Date(2012, 3, 28, 0, 0, 0, 0, time.UTC)
var (
sameInts = cmpopts.SortSlices(func(x, y int) bool {
......@@ -88,9 +84,9 @@ want:
"42"
`,
}, {
about: "Equals: error is nil",
about: "Equals: nil and nil",
checker: qt.Equals,
got: (error)(nil),
got: nil,
args: []interface{}{nil},
expectedNegateFailure: `
error:
......@@ -105,14 +101,15 @@ want:
checker: qt.Equals,
got: errBadWolf,
args: []interface{}{nil},
expectedCheckFailure: fmt.Sprintf(`
expectedCheckFailure: `
error:
provided error is not nil
got non-nil error
got:
%s
bad wolf
file:line
want:
nil
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "Equals: nil struct",
checker: qt.Equals,
......@@ -235,7 +232,7 @@ want:
},
Ints: {42},
}
`, prefixf(cmp.Diff(cmpEqualsGot, cmpEqualsWant))),
`, diff(cmpEqualsGot, cmpEqualsWant)),
}, {
about: "CmpEquals: same values with options",
checker: qt.CmpEquals(sameInts),
......@@ -267,7 +264,7 @@ got:
[]int{1, 2, 4}
want:
[]int{3, 2, 1}
`, prefixf(cmp.Diff([]int{1, 2, 4}, []int{3, 2, 1}, sameInts))),
`, diff([]int{1, 2, 4}, []int{3, 2, 1}, sameInts)),
}, {
about: "CmpEquals: structs with unexported fields not allowed",
checker: qt.CmpEquals(),
......@@ -380,7 +377,7 @@ got:
[]int{1, 2, 3}
want:
[]int{3, 2, 1}
`, prefixf(cmp.Diff([]int{1, 2, 3}, []int{3, 2, 1}))),
`, diff([]int{1, 2, 3}, []int{3, 2, 1})),
}, {
about: "DeepEquals: same times",
checker: qt.DeepEquals,
......@@ -412,7 +409,7 @@ got:
s"2012-03-29 00:00:00 +0000 UTC"
want:
s"2012-03-28 00:00:00 +0000 UTC"
`, prefixf(cmp.Diff(goTime.Add(24*time.Hour), goTime))),
`, diff(goTime.Add(24*time.Hour), goTime)),
}, {
about: "DeepEquals: not enough arguments",
checker: qt.DeepEquals,
......@@ -595,7 +592,7 @@ want:
"bad",
"wolf",
}
`, prefixf(cmp.Diff([]string{"bad", "wolf"}, []interface{}{"bad", "wolf"}))),
`, diff([]string{"bad", "wolf"}, []interface{}{"bad", "wolf"})),
}, {
about: "ContentEquals: not enough arguments",
checker: qt.ContentEquals,
......@@ -819,66 +816,71 @@ want args:
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{"bad wolf"},
expectedNegateFailure: fmt.Sprintf(`
expectedNegateFailure: `
error:
unexpected success
got error:
%s
bad wolf
file:line
regexp:
"bad wolf"
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "ErrorMatches: match",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{"bad .*"},
expectedNegateFailure: fmt.Sprintf(`
expectedNegateFailure: `
error:
unexpected success
got error:
%s
bad wolf
file:line
regexp:
"bad .*"
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "ErrorMatches: mismatch",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{"exterminate"},
expectedCheckFailure: fmt.Sprintf(`
expectedCheckFailure: `
error:
error does not match regexp
got error:
%s
bad wolf
file:line
regexp:
"exterminate"
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "ErrorMatches: empty pattern",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{""},
expectedCheckFailure: fmt.Sprintf(`
expectedCheckFailure: `
error:
error does not match regexp
got error:
%s
bad wolf
file:line
regexp:
""
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "ErrorMatches: complex pattern",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{"bad wolf|end of the universe"},
expectedNegateFailure: fmt.Sprintf(`
expectedNegateFailure: `
error:
unexpected success
got error:
%s
bad wolf
file:line
regexp:
"bad wolf|end of the universe"
`, prefixf("%+v", errBadWolf)),
`,
}, {
about: "ErrorMatches: invalid pattern",
checker: qt.ErrorMatches,
......@@ -1846,6 +1848,7 @@ func TestCheckers(t *testing.T) {
}
}
func prefixf(format string, args ...interface{}) string {
return strings.TrimSuffix(qt.Prefixf(" ", format, args...), "\n")
func diff(x, y interface{}, opts ...cmp.Option) string {
d := cmp.Diff(x, y, opts...)
return strings.TrimSuffix(qt.Prefixf(" ", "%s", d), "\n")
}
......@@ -4,6 +4,7 @@ package quicktest_test
import (
"errors"
"fmt"
"testing"
qt "github.com/frankban/quicktest"
......@@ -23,3 +24,22 @@ func TestIsBadCheck(t *testing.T) {
err = errors.New("bad wolf")
assertBool(t, qt.IsBadCheck(err), false)
}
var errBadWolf = &errTest{}
// errTest is an error type used in tests.
type errTest struct{}
// Error implements error.
func (*errTest) Error() string {
return "bad wolf"
}
// Format implements fmt.Formatter.
func (err *errTest) Format(f fmt.State, c rune) {
if !f.Flag('+') || c != 'v' {
fmt.Fprint(f, "unexpected verb for formatting the error")
}
fmt.Fprint(f, err.Error())
fmt.Fprint(f, "\n file:line")
}
......@@ -4,7 +4,6 @@ package quicktest_test
import (
"bytes"
"fmt"
"testing"
qt "github.com/frankban/quicktest"
......@@ -17,7 +16,7 @@ var formatTests = []struct {
}{{
about: "error value",
value: errBadWolf,
want: fmt.Sprintf("%+v", errBadWolf),
want: "bad wolf\n file:line",
}, {
about: "stringer",
value: bytes.NewBufferString("I am a stringer"),
......
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