Skip to content

Commit

Permalink
Merge pull request #555 from AndrewSirenko/AddProtobufMatcher
Browse files Browse the repository at this point in the history
Cherry-pick #553: Add Protobuf Matcher utility
  • Loading branch information
k8s-ci-robot authored Aug 30, 2024
2 parents f07468f + 1aab2d9 commit c20397f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/klog/v2 v2.130.1
)
Expand All @@ -24,6 +25,5 @@ require (
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
33 changes: 33 additions & 0 deletions utils/protobuf_matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"github.com/golang/mock/gomock"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)

// Protobuf returns a Matcher that relies upon proto.Equal to compare Protobuf messages
// Example usage with mocked request:
//
// example.EXPECT().ExampleRequest(Protobuf(requestMsg)).Return(responseMsg, nil).AnyTimes()
func Protobuf(msg proto.Message) gomock.Matcher {
return &ProtobufMatcher{msg}
}

type ProtobufMatcher struct {
msg proto.Message
}

var _ gomock.Matcher = &ProtobufMatcher{}

func (p *ProtobufMatcher) Matches(x interface{}) bool {
otherMsg, ok := x.(proto.Message)
if !ok {
return false
}
return proto.Equal(p.msg, otherMsg)
}

func (p *ProtobufMatcher) String() string {
return prototext.Format(p.msg)
}

0 comments on commit c20397f

Please sign in to comment.