Skip to content

Commit 12190f5

Browse files
authored
Merge pull request #553 from AndrewSirenko/AddProtobufMatcher
Add Protobuf Matcher utility
2 parents f07468f + 1aab2d9 commit 12190f5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/onsi/ginkgo/v2 v2.13.1
1111
github.com/onsi/gomega v1.30.0
1212
google.golang.org/grpc v1.65.0
13+
google.golang.org/protobuf v1.34.1
1314
gopkg.in/yaml.v2 v2.4.0
1415
k8s.io/klog/v2 v2.130.1
1516
)
@@ -24,6 +25,5 @@ require (
2425
golang.org/x/text v0.15.0 // indirect
2526
golang.org/x/tools v0.14.0 // indirect
2627
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
27-
google.golang.org/protobuf v1.34.1 // indirect
2828
gopkg.in/yaml.v3 v3.0.1 // indirect
2929
)

utils/protobuf_matcher.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utils
2+
3+
import (
4+
"github.com/golang/mock/gomock"
5+
"google.golang.org/protobuf/encoding/prototext"
6+
"google.golang.org/protobuf/proto"
7+
)
8+
9+
// Protobuf returns a Matcher that relies upon proto.Equal to compare Protobuf messages
10+
// Example usage with mocked request:
11+
//
12+
// example.EXPECT().ExampleRequest(Protobuf(requestMsg)).Return(responseMsg, nil).AnyTimes()
13+
func Protobuf(msg proto.Message) gomock.Matcher {
14+
return &ProtobufMatcher{msg}
15+
}
16+
17+
type ProtobufMatcher struct {
18+
msg proto.Message
19+
}
20+
21+
var _ gomock.Matcher = &ProtobufMatcher{}
22+
23+
func (p *ProtobufMatcher) Matches(x interface{}) bool {
24+
otherMsg, ok := x.(proto.Message)
25+
if !ok {
26+
return false
27+
}
28+
return proto.Equal(p.msg, otherMsg)
29+
}
30+
31+
func (p *ProtobufMatcher) String() string {
32+
return prototext.Format(p.msg)
33+
}

0 commit comments

Comments
 (0)