Skip to content

Commit

Permalink
Merge pull request #1 from rickypai/rpai/proto_equal
Browse files Browse the repository at this point in the history
if both types are proto messages, use proto.Equal
  • Loading branch information
evie404 authored May 9, 2019
2 parents 34c6fa2 + a2b85ea commit d23661d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"unicode/utf8"

"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -58,6 +59,15 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
return expected == actual
}

expProto, ok := expected.(proto.Message)
if ok {
actProto, ok := actual.(proto.Message)
if ok {
// if both are protobuf messages, use `proto.Equal`
return proto.Equal(expProto, actProto)
}
}

exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
Expand Down
29 changes: 29 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"strings"
"testing"
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/proto/test_proto"
)

var (
Expand Down Expand Up @@ -122,6 +125,32 @@ func TestObjectsAreEqual(t *testing.T) {
if ObjectsAreEqual('x', "x") {
t.Error("objectsAreEqual should return false")
}
if !ObjectsAreEqual(
&test_proto.GoTestField{
Label: proto.String("123"),
Type: proto.String("abc"),
},
&test_proto.GoTestField{
Label: proto.String("123"),
Type: proto.String("abc"),
},
) {
t.Error("objectsAreEqual should return true")
}
if !ObjectsAreEqual(
&test_proto.GoTestField{
Label: proto.String("123"),
Type: proto.String("abc"),
XXX_sizecache: 1,
},
&test_proto.GoTestField{
Label: proto.String("123"),
Type: proto.String("abc"),
XXX_sizecache: 2,
},
) {
t.Error("objectsAreEqual should return true") // different XXX_sizecache is still equal for proto
}
if ObjectsAreEqual("x", 'x') {
t.Error("objectsAreEqual should return false")
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/stretchr/testify

require (
github.com/davecgh/go-spew v1.1.0
github.com/golang/protobuf v1.1.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/objx v0.1.0
golang.org/x/sync v0.0.0-20190412183630-56d357773e84 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84 h1:IqXQ59gzdXv58Jmm2xn0tSOR9i6HqroaOFRQ3wR/dJQ=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 comments on commit d23661d

Please sign in to comment.