-
Notifications
You must be signed in to change notification settings - Fork 53
/
equal.go
37 lines (30 loc) · 1.11 KB
/
equal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package testutil
import (
"fmt"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
"google.golang.org/protobuf/proto"
)
func ProtoEqual(expected proto.Message) types.GomegaMatcher {
return &ProtoMatcher{
Expected: expected,
}
}
type ProtoMatcher struct {
Expected proto.Message
}
func (matcher *ProtoMatcher) Match(actual any) (success bool, err error) {
if actual == nil && matcher.Expected == nil {
return false, fmt.Errorf("Refusing to compare <nil> to <nil>.\nBe explicit and use BeNil() instead. This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.")
}
if _, ok := actual.(proto.Message); !ok {
return false, fmt.Errorf("ProtoMatcher expects a proto.Message. Got:\n%s", format.Object(actual, 1))
}
return proto.Equal(actual.(proto.Message), matcher.Expected), nil
}
func (matcher *ProtoMatcher) FailureMessage(actual any) (message string) {
return format.Message(actual, "to equal", matcher.Expected)
}
func (matcher *ProtoMatcher) NegatedFailureMessage(actual any) (message string) {
return format.Message(actual, "not to equal", matcher.Expected)
}