-
Notifications
You must be signed in to change notification settings - Fork 2
/
matchers.go
145 lines (110 loc) · 3 KB
/
matchers.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
136
137
138
139
140
141
142
143
144
145
package spy
import (
"reflect"
)
type Matcher interface {
//Match matches provided value against conditions and returns true or false
// this method should not panic
Match(actual interface{}) bool
}
type stringMatcher struct {
value string
}
func (m stringMatcher) Match(a interface{}) bool {
v, ok := a.(string)
return ok && m.value == v
}
//String returns a matcher which match against provided string value
func String(s string) Matcher {
return stringMatcher{s}
}
type boolMatcher struct {
value bool
}
func (m boolMatcher) Match(a interface{}) bool {
v, ok := a.(bool)
return ok && m.value == v
}
//Bool returns a matcher which can match against provided bool value
func Bool(b bool) Matcher {
return boolMatcher{b}
}
type anythingMatcher struct{}
func (m anythingMatcher) Match(a interface{}) bool { return true }
//Anything returns a matcher which matches anything
func Anything() Matcher {
return anythingMatcher{}
}
type nilMatcher struct{}
func (m nilMatcher) Match(a interface{}) bool { return a == nil }
//Nil returns a matcher which matches nil
func Nil() Matcher { return nilMatcher{} }
type notNilMatcher struct{}
func (m notNilMatcher) Match(a interface{}) bool { return a != nil }
//NotNil returns a matcher which matches anything not nil
func NotNil() Matcher { return notNilMatcher{} }
type intMatcher struct {
value int
}
func (m intMatcher) Match(v interface{}) bool {
v, ok := v.(int)
return ok && m.value == v
}
//Int returns a matcher which can match against provided int value
func Int(i int) Matcher { return intMatcher{i} }
type int64Matcher struct {
value int64
}
func (m int64Matcher) Match(v interface{}) bool {
v, ok := v.(int64)
return ok && m.value == v
}
//Int64 returns a matcher which can match against provided int64 value
func Int64(i int64) Matcher { return int64Matcher{i} }
type uint64Matcher struct {
value uint64
}
func (m uint64Matcher) Match(v interface{}) bool {
v, ok := v.(uint64)
return ok && m.value == v
}
//Uint64 returns a matcher which can match against provided uint64 value
func Uint64(i uint64) Matcher { return uint64Matcher{i} }
type typeMatcher struct {
value string
}
//Type returns a Matcher which matches the type against the string argument.
// Example Type("*int")
// Will match a pointer to int
func (m typeMatcher) Match(v interface{}) bool {
if v == nil {
return false
}
return reflect.TypeOf(v).String() == m.value
}
func Type(t string) Matcher {
return typeMatcher{t}
}
type customMatcher struct {
value func(v interface{}) bool
}
func (m customMatcher) Match(v interface{}) bool {
return m.value(v)
}
//Custom wrapps a custom func with Matcher interface
func Custom(f func(v interface{}) bool) Matcher {
return customMatcher{f}
}
type exactMatcher struct {
value interface{}
}
func (m exactMatcher) Match(v interface{}) bool {
if v == nil {
return m.value == v
}
return reflect.DeepEqual(m.value, v)
}
//Exact only matches the same value. Uses reflect.DeepEqual
func Exact(v interface{}) Matcher {
return exactMatcher{v}
}