Added DeepEquals and Equals now matches the behavior of ==.
diff --git a/checkers.go b/checkers.go
index 5ffc449..70d5b68 100644
--- a/checkers.go
+++ b/checkers.go
@@ -152,21 +152,49 @@
*CheckerInfo
}
-// The Equals checker verifies that the obtained value is deep-equal to
-// the expected value. The check will work correctly even when facing
-// arrays, interfaces, and values of different types (which always fail
-// the test).
+// The Equals checker verifies that the obtained value is equal to
+// the expected value, according to usual Go semantics for ==.
//
// For example:
//
// c.Assert(value, Equals, 42)
-// c.Assert(array, Equals, []string{"hi", "there"})
//
var Equals Checker = &equalsChecker{
&CheckerInfo{Name: "Equals", Params: []string{"obtained", "expected"}},
}
func (checker *equalsChecker) Check(params []interface{}, names []string) (result bool, error string) {
+ defer func() {
+ if v := recover(); v != nil {
+ result = false
+ error = fmt.Sprint(v)
+ }
+ }()
+ return params[0] == params[1], ""
+}
+
+// -----------------------------------------------------------------------
+// DeepEquals checker.
+
+type deepEqualsChecker struct {
+ *CheckerInfo
+}
+
+// The DeepEquals checker verifies that the obtained value is deep-equal to
+// the expected value. The check will work correctly even when facing
+// slices, interfaces, and values of different types (which always fail
+// the test).
+//
+// For example:
+//
+// c.Assert(value, DeepEquals, 42)
+// c.Assert(array, DeepEquals, []string{"hi", "there"})
+//
+var DeepEquals Checker = &deepEqualsChecker{
+ &CheckerInfo{Name: "DeepEquals", Params: []string{"obtained", "expected"}},
+}
+
+func (checker *deepEqualsChecker) Check(params []interface{}, names []string) (result bool, error string) {
return reflect.DeepEqual(params[0], params[1]), ""
}
diff --git a/checkers_test.go b/checkers_test.go
index e802f2f..c1bf711 100644
--- a/checkers_test.go
+++ b/checkers_test.go
@@ -96,19 +96,44 @@
// With nil.
testCheck(c, gocheck.Equals, false, "", 42, nil)
- // Arrays
- testCheck(c, gocheck.Equals, true, "", []byte{1, 2}, []byte{1, 2})
- testCheck(c, gocheck.Equals, false, "", []byte{1, 2}, []byte{1, 3})
+ // Slices
+ testCheck(c, gocheck.Equals, false, "runtime error: comparing uncomparable type []uint8", []byte{1, 2}, []byte{1, 2})
// Struct values
testCheck(c, gocheck.Equals, true, "", simpleStruct{1}, simpleStruct{1})
testCheck(c, gocheck.Equals, false, "", simpleStruct{1}, simpleStruct{2})
// Struct pointers
- testCheck(c, gocheck.Equals, true, "", &simpleStruct{1}, &simpleStruct{1})
+ testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{1})
testCheck(c, gocheck.Equals, false, "", &simpleStruct{1}, &simpleStruct{2})
}
+func (s *CheckersS) TestDeepEquals(c *gocheck.C) {
+ testInfo(c, gocheck.DeepEquals, "DeepEquals", []string{"obtained", "expected"})
+
+ // The simplest.
+ testCheck(c, gocheck.DeepEquals, true, "", 42, 42)
+ testCheck(c, gocheck.DeepEquals, false, "", 42, 43)
+
+ // Different native types.
+ testCheck(c, gocheck.DeepEquals, false, "", int32(42), int64(42))
+
+ // With nil.
+ testCheck(c, gocheck.DeepEquals, false, "", 42, nil)
+
+ // Slices
+ testCheck(c, gocheck.DeepEquals, true, "", []byte{1, 2}, []byte{1, 2})
+ testCheck(c, gocheck.DeepEquals, false, "", []byte{1, 2}, []byte{1, 3})
+
+ // Struct values
+ testCheck(c, gocheck.DeepEquals, true, "", simpleStruct{1}, simpleStruct{1})
+ testCheck(c, gocheck.DeepEquals, false, "", simpleStruct{1}, simpleStruct{2})
+
+ // Struct pointers
+ testCheck(c, gocheck.DeepEquals, true, "", &simpleStruct{1}, &simpleStruct{1})
+ testCheck(c, gocheck.DeepEquals, false, "", &simpleStruct{1}, &simpleStruct{2})
+}
+
func (s *CheckersS) TestErrorMatches(c *gocheck.C) {
testInfo(c, gocheck.ErrorMatches, "ErrorMatches", []string{"value", "regex"})