-
Notifications
You must be signed in to change notification settings - Fork 53
/
equal.go
105 lines (89 loc) · 3.19 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package testutil
import (
"fmt"
"strings"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/prototext"
"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 fmt.Sprintf("Expected\n%s\n%s\n%s",
format.IndentString(prototext.Format(actual.(proto.Message)), 1),
"to equal",
format.IndentString(prototext.Format(matcher.Expected), 1),
)
}
func (matcher *ProtoMatcher) NegatedFailureMessage(actual any) (message string) {
return fmt.Sprintf("Expected\n%s\n%s\n%s",
format.IndentString(prototext.Format(actual.(proto.Message)), 1),
"not to equal",
format.IndentString(prototext.Format(matcher.Expected), 1),
)
}
type StatusCodeMatcher struct {
Expected any
matchMsg types.GomegaMatcher
}
func MatchStatusCode(expected any, matchMessage ...types.GomegaMatcher) types.GomegaMatcher {
m := &StatusCodeMatcher{
Expected: expected,
}
if len(matchMessage) > 0 {
m.matchMsg = matchMessage[0]
}
return m
}
func (m *StatusCodeMatcher) code(value any) codes.Code {
if value == nil {
return codes.OK
}
switch value := value.(type) {
case error:
return status.Code(value)
case *status.Status:
return value.Code()
case codes.Code:
return value
case uint32:
return codes.Code(value)
default:
panic(fmt.Sprintf("MatchStatus expects a grpc status, error, or codes.Code. Got:\n%s", format.Object(value, 1)))
}
}
func (m *StatusCodeMatcher) Match(actual any) (success bool, err error) {
if actual == nil && m.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")
}
return m.code(actual) == m.code(m.Expected), nil
}
func (m *StatusCodeMatcher) FailureMessage(actual any) (message string) {
actualStatusCode := m.code(actual)
expectedStatusCode := m.code(m.Expected)
actualMsg := fmt.Sprintf("%s | %s(%d)", format.Object(actual, 1), actualStatusCode.String(), actualStatusCode)
expectedMsg := fmt.Sprintf("%s | %s(%d)", format.Object(m.Expected, 1), expectedStatusCode.String(), expectedStatusCode)
return fmt.Sprintf("Expected\n%s\nto match the status code of\n%s", actualMsg, expectedMsg)
}
func (m *StatusCodeMatcher) NegatedFailureMessage(actual any) (message string) {
msg := m.FailureMessage(actual)
return strings.Replace(msg, "to match", "not to match", 1)
}