-
Notifications
You must be signed in to change notification settings - Fork 36
/
equality_specs.go
135 lines (120 loc) · 3.42 KB
/
equality_specs.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package assertions
import (
"math"
"reflect"
"time"
)
type specification interface {
assertable(a, b any) bool
passes(a, b any) bool
}
var equalitySpecs = []specification{
numericEquality{},
timeEquality{},
pointerEquality{},
deepEquality{},
equalityMethodSpecification{}, // Now that we have the timeEquality spec, this could probably be removed..
}
// deepEquality compares any two values using reflect.DeepEqual.
// https://golang.org/pkg/reflect/#DeepEqual
type deepEquality struct{}
func (deepEquality) assertable(a, b any) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}
func (deepEquality) passes(a, b any) bool {
return reflect.DeepEqual(a, b)
}
// numericEquality compares numeric values using the built-in equality
// operator (`==`). Values of differing numeric reflect.Kind are each
// converted to the type of the other and are compared with `==` in both
// directions, with one exception: two mixed integers (one signed and one
// unsigned) are always unequal in the case that the unsigned value is
// greater than math.MaxInt64. https://golang.org/pkg/reflect/#Kind
type numericEquality struct{}
func (numericEquality) assertable(a, b any) bool {
return isNumeric(a) && isNumeric(b)
}
func (numericEquality) passes(a, b any) bool {
aValue := reflect.ValueOf(a)
bValue := reflect.ValueOf(b)
if isUnsignedInteger(a) && isSignedInteger(b) && aValue.Uint() >= math.MaxInt64 {
return false
}
if isSignedInteger(a) && isUnsignedInteger(b) && bValue.Uint() >= math.MaxInt64 {
return false
}
aAsB := aValue.Convert(bValue.Type()).Interface()
bAsA := bValue.Convert(aValue.Type()).Interface()
return a == bAsA && b == aAsB
}
func isNumeric(v any) bool {
of := reflect.TypeOf(v)
if of == nil {
return false
}
_, found := numericKinds[of.Kind()]
return found
}
func isSignedInteger(v any) bool {
_, found := signedIntegerKinds[reflect.TypeOf(v).Kind()]
return found
}
var unsignedIntegerKinds = map[reflect.Kind]struct{}{
reflect.Uint: {},
reflect.Uint8: {},
reflect.Uint16: {},
reflect.Uint32: {},
reflect.Uint64: {},
reflect.Uintptr: {},
}
func isUnsignedInteger(v any) bool {
_, found := unsignedIntegerKinds[reflect.TypeOf(v).Kind()]
return found
}
var signedIntegerKinds = map[reflect.Kind]struct{}{
reflect.Int: {},
reflect.Int8: {},
reflect.Int16: {},
reflect.Int32: {},
reflect.Int64: {},
}
var numericKinds = map[reflect.Kind]struct{}{
reflect.Int: {},
reflect.Int8: {},
reflect.Int16: {},
reflect.Int32: {},
reflect.Int64: {},
reflect.Uint: {},
reflect.Uint8: {},
reflect.Uint16: {},
reflect.Uint32: {},
reflect.Uint64: {},
reflect.Float32: {},
reflect.Float64: {},
}
// timeEquality compares values both of type time.Time using their Equal method.
// https://golang.org/pkg/time/#Time.Equal
type timeEquality struct{}
func (timeEquality) assertable(a, b any) bool {
return isTime(a) && isTime(b)
}
func (timeEquality) passes(a, b any) bool {
return a.(time.Time).Equal(b.(time.Time))
}
func isTime(v any) bool {
_, ok := v.(time.Time)
return ok
}
type pointerEquality struct{}
func (pointerEquality) assertable(a, b any) bool {
return areSameType(a, b) && isKind(reflect.Func, a)
}
func (pointerEquality) passes(a, b any) bool {
return reflect.ValueOf(a).Pointer() == reflect.ValueOf(b).Pointer()
}
func areSameType(a, b any) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}
func isKind(kind reflect.Kind, a any) bool {
return reflect.ValueOf(a).Kind() == kind
}