-
Notifications
You must be signed in to change notification settings - Fork 1
/
proto.go
78 lines (64 loc) · 2.47 KB
/
proto.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
package test
import (
"fmt"
assertions "github.com/dgottlieb/smarty-assertions"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
)
const (
success = ""
needExactValues = "This assertion requires exactly %d comparison values (you provided %d)."
shouldHaveResembled = "Expected: '%s'\nActual: '%s'\n(Should resemble)!"
shouldNotHaveResembled = "Expected '%#v'\nto NOT resemble '%#v'\n(but it did)!"
)
func need(needed int, expected []interface{}) string {
if len(expected) != needed {
return fmt.Sprintf(needExactValues, needed, len(expected))
}
return success
}
// ShouldResembleProto receives exactly two parameters and does a proto equal check.
func ShouldResembleProto(actual interface{}, expected ...interface{}) string {
if message := need(1, expected); message != success {
return message
}
if cmp.Equal(actual, expected[0], protocmp.Transform()) {
return ""
}
return fmt.Sprintf(shouldHaveResembled, actual, expected[0]) +
cmp.Diff(actual, expected[0], protocmp.Transform())
}
// ShouldNotResembleProto receives exactly two parameters and does an inverse proto equal check.
func ShouldNotResembleProto(actual interface{}, expected ...interface{}) string {
if message := need(1, expected); message != success {
return message
} else if ShouldResembleProto(actual, expected[0]) == success {
return fmt.Sprintf(shouldNotHaveResembled, actual, expected[0])
}
return success
}
// ShouldResemble receives exactly two parameters and does a deep equal check (see reflect.DeepEqual)
func ShouldResemble(actual interface{}, expected ...interface{}) string {
if message := need(1, expected); message != success {
return message
}
_, actualIsProto := actual.(proto.Message)
_, expectedIsProto := expected[0].(proto.Message)
if actualIsProto && expectedIsProto {
return ShouldResembleProto(actual, expected...)
}
return assertions.ShouldResemble(actual, expected...)
}
// ShouldNotResemble receives exactly two parameters and does an inverse deep equal check (see reflect.DeepEqual)
func ShouldNotResemble(actual interface{}, expected ...interface{}) string {
if message := need(1, expected); message != success {
return message
}
_, actualIsProto := actual.(proto.Message)
_, expectedIsProto := expected[0].(proto.Message)
if actualIsProto && expectedIsProto {
return ShouldNotResembleProto(actual, expected...)
}
return assertions.ShouldNotResemble(actual, expected...)
}