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