-
Notifications
You must be signed in to change notification settings - Fork 2
/
matchers_test.go
188 lines (158 loc) · 4.8 KB
/
matchers_test.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package spy
import (
. "github.com/onsi/gomega"
"testing"
)
var trueMatcher, falseMatcher, strMatcher, iMatcher, i64Matcher, ui64Matcher, anyMatcher, nullMatcher, notNullMatcher, tMatcher, custMatcher Matcher
func init() {
trueMatcher = Bool(true)
falseMatcher = Bool(false)
strMatcher = String("Foo")
iMatcher = Int(1)
i64Matcher = Int64(1)
ui64Matcher = Uint64(1)
anyMatcher = Anything()
nullMatcher = Nil()
notNullMatcher = NotNil()
tMatcher = Type("*int")
custMatcher = Custom(func(a interface{}) bool {
v, ok := a.(string)
return ok && len(v) > 4
})
}
func TestMatchesFoo(t *testing.T) {
RegisterTestingT(t)
Expect(strMatcher.Match("Foo")).To(BeTrue(), "failed to match 'Foo' with 'Foo'")
}
func Test_does_not_panic_when_matched_against_non_string(t *testing.T) {
RegisterTestingT(t)
Expect(func() {
strMatcher.Match(2)
}).ShouldNot(Panic(), "String matcher should not panic")
}
func Test_does_not_match_bar(t *testing.T) {
RegisterTestingT(t)
Expect(strMatcher.Match("bar")).To(BeFalse())
}
func Test_does_not_panic_when_matched_against_non_bool(t *testing.T) {
Expect(func() {
trueMatcher.Match("a")
falseMatcher.Match("a")
}).ShouldNot(Panic())
}
func Test_bool_matches(t *testing.T) {
RegisterTestingT(t)
Expect(trueMatcher.Match(true)).To(BeTrue())
Expect(falseMatcher.Match(false))
}
func Test_bool_does__not_match(t *testing.T) {
RegisterTestingT(t)
Expect(trueMatcher.Match(false)).To(BeFalse())
Expect(falseMatcher.Match(true)).To(BeFalse())
}
func Test_does_not_panic_when_matched_against_non_int(t *testing.T) {
RegisterTestingT(t)
Expect(func() {
Expect(iMatcher.Match(uint(1))).To(BeFalse())
}).ShouldNot(Panic())
}
func Test_int_matches_1(t *testing.T) {
RegisterTestingT(t)
Expect(iMatcher.Match(1)).To(BeTrue())
}
func Test_does_not_match_2(t *testing.T) {
RegisterTestingT(t)
Expect(iMatcher.Match(2)).To(BeFalse())
}
func Test_does_not_match_uint_or_int32_or_int64(t *testing.T) {
RegisterTestingT(t)
Expect(iMatcher.Match(uint(1))).To(BeFalse())
Expect(iMatcher.Match(int64(1))).To(BeFalse())
Expect(iMatcher.Match(int32(1))).To(BeFalse())
}
func Test_does_not_panic_when_matched_against_non_int64(t *testing.T) {
RegisterTestingT(t)
Expect(func() {
Expect(i64Matcher.Match(uint(1))).To(BeFalse())
}).ShouldNot(Panic())
}
func Test_matches1(t *testing.T) {
RegisterTestingT(t)
Expect(i64Matcher.Match(int64(1))).To(BeTrue())
}
func Test_does_not_matches2(t *testing.T) {
RegisterTestingT(t)
Expect(i64Matcher.Match(int64(2))).To(BeFalse())
}
func Test_does_not_matches_uint_int_int(t *testing.T) {
RegisterTestingT(t)
Expect(i64Matcher.Match(uint(1))).To(BeFalse())
Expect(i64Matcher.Match(int(1))).To(BeFalse())
Expect(i64Matcher.Match(int32(1))).To(BeFalse())
}
func Test_does_not_panic_when_matchedagainst_no_uint64(t *testing.T) {
RegisterTestingT(t)
Expect(func() {
Expect(ui64Matcher.Match(int(1))).To(BeFalse())
}).ShouldNot(Panic())
}
func Test_matches_uint1(t *testing.T) {
RegisterTestingT(t)
Expect(ui64Matcher.Match(uint64(1))).To(BeTrue())
}
func Test_does_not_matches_uint2(t *testing.T) {
RegisterTestingT(t)
Expect(ui64Matcher.Match(uint64(2))).To(BeFalse())
}
func Test_does_not_match_uint_int32_int(t *testing.T) {
RegisterTestingT(t)
Expect(ui64Matcher.Match(uint(1))).To(BeFalse())
Expect(ui64Matcher.Match(int(1))).To(BeFalse())
Expect(ui64Matcher.Match(int32(1))).To(BeFalse())
}
func Test_matches_anything(t *testing.T) {
RegisterTestingT(t)
Expect(anyMatcher.Match(nil)).To(BeTrue())
Expect(anyMatcher.Match("1")).To(BeTrue())
Expect(anyMatcher.Match(1)).To(BeTrue())
Expect(anyMatcher.Match(String("test"))).To(BeTrue())
}
func Test_matches_nil(t *testing.T) {
RegisterTestingT(t)
Expect(nullMatcher.Match(nil)).To(BeTrue())
}
func Test_does_not_matches_non_nil(t *testing.T) {
RegisterTestingT(t)
Expect(nullMatcher.Match("value")).To(BeFalse())
Expect(nullMatcher.Match(10)).To(BeFalse())
}
func Test_match_not_nil(t *testing.T) {
RegisterTestingT(t)
Expect(notNullMatcher.Match("value")).To(BeTrue())
Expect(notNullMatcher.Match(10)).To(BeTrue())
}
func Test_does_not_match_nnil(t *testing.T) {
RegisterTestingT(t)
Expect(notNullMatcher.Match(nil)).To(BeFalse())
}
func Test_match_pointer_int(t *testing.T) {
RegisterTestingT(t)
var i int
Expect(tMatcher.Match(&i)).To(BeTrue())
}
func Test_does_not_match_no_pointer_int(t *testing.T) {
RegisterTestingT(t)
Expect(tMatcher.Match(1)).To(BeFalse())
Expect(tMatcher.Match("1")).To(BeFalse())
Expect(tMatcher.Match(nil)).To(BeFalse())
}
func Test_match_foobar(t *testing.T) {
RegisterTestingT(t)
Expect(custMatcher.Match("foobar")).To(BeTrue())
}
func Test_does_not_match_non_string_or_string_lesthan_4_len(t *testing.T) {
RegisterTestingT(t)
Expect(custMatcher.Match(1)).To(BeFalse())
Expect(custMatcher.Match("1")).To(BeFalse())
Expect(custMatcher.Match(nil)).To(BeFalse())
}