-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from AndrewSirenko/AddProtobufMatcher
Cherry-pick #553: Add Protobuf Matcher utility
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |