-
Notifications
You must be signed in to change notification settings - Fork 3
/
matchers_test.go
270 lines (220 loc) · 5.45 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package collections
import (
"testing"
"github.com/thefuga/go-collections/internal"
)
func TestKeyEquals(t *testing.T) {
if !KeyEquals("foo")("foo", nil) {
t.Error("both keys are equal")
}
if KeyEquals("foo")("bar", nil) {
t.Error("keys are different")
}
}
func TestValueEquals(t *testing.T) {
if !ValueEquals[int]("foo")(1, "foo") {
t.Error("both values are equal")
}
if ValueEquals[int]("foo")(1, "bar") {
t.Error("values are different")
}
}
func TestValueDeepEquals(t *testing.T) {
type user struct {
name string
}
bob1 := user{"bob"}
bob2 := user{"bob"}
alice := user{"alice"}
if !ValueDeepEquals[int](bob1)(1, bob2) {
t.Error("both values are equal")
}
if ValueDeepEquals[int](bob1)(1, alice) {
t.Error("values are different")
}
}
func TestValueDiffers(t *testing.T) {
if !ValueDiffers("foo")(nil, "bar") {
t.Error("values are different")
}
if ValueDiffers("foo")(nil, "foo") {
t.Error("values are equal")
}
}
func TestAsc(t *testing.T) {
if !Asc[int]()(1, 2) {
t.Error("1 is lesser than 2")
}
if Asc[int]()(2, 1) {
t.Error("2 is greater than 1")
}
}
func TestDesc(t *testing.T) {
if !Desc[int]()(2, 1) {
t.Error("2 is greater than 1")
}
if Desc[int]()(1, 2) {
t.Error("1 is lesser than 2")
}
}
func TestAssert(t *testing.T) {
var concreteType string
defer func() {
if assertionErr := recover(); assertionErr != nil {
t.Error("Unexpected error casting value.")
}
}()
underlyingValue := "generic value"
var genericType any = underlyingValue
concreteType, _ = internal.Assert[string](genericType)
if concreteType != underlyingValue {
t.Error("Expected concreteType to have the value of underlyingValue")
}
}
func TestAssertE(t *testing.T) {
underlyingValue := "generic value"
var genericType any = underlyingValue
concreteType, assertionErr := internal.AssertE[string](genericType)
if assertionErr != nil {
t.Error("Unexpected error casting value.")
}
if concreteType != underlyingValue {
t.Error("Expected concreteType to have the value of underlyingValue")
}
zeroValue, assertionErr := internal.AssertE[int](genericType)
if zeroValue != 0 {
t.Error("Cast value should be zeroed when an invalid type is given")
}
if assertionErr.Error() != "interface conversion: interface {} is string, not int" {
t.Error("Trying to get a value with the wrong type parameter must return a type error!")
}
}
func TestFieldEquals(t *testing.T) {
u := user{Name: "Jon", Email: "[email protected]", Age: 33}
if !FieldEquals[user]("Name", u.Name)(0, u) {
t.Error("user should've matched")
}
}
func TestFieldMatch(t *testing.T) {
u := user{Name: "Jon", Email: "[email protected]", Age: 33}
if !FieldMatch[user]("Age", ValueCastGT(30))(0, u) {
t.Error("user should've matched")
}
}
func TestFieldMatchWithDifferentTypes(t *testing.T) {
u := user{Name: "Jon", Email: "[email protected]", Age: 33}
if FieldMatch[user]("Age", ValueDeepEquals[any, any]("foo"))(0, u) {
t.Error("should not match string 'foo' against Age number")
}
}
func TestNot(t *testing.T) {
matcher := ValueEquals[int](1)
notMatcher := Not(matcher)
if notMatcher(0, 1) == matcher(0, 1) {
t.Error("got the same result using Not.")
}
}
func TestAnd(t *testing.T) {
i := 10
if !And[int](ValueCastGT(9), ValueCastLT(11))(0, i) {
t.Error("10 is greater than 9 and lesser than 11")
}
}
func TestAndValue(t *testing.T) {
i := 10
if !AndValue(
11,
func(v int) Matcher[int, int] { return ValueGT[int](-v) },
func(v int) Matcher[int, int] { return ValueLT[int](v) },
)(0, i) {
t.Error("10 is greater than -11 and lesser than 11")
}
}
func TestOr(t *testing.T) {
i := 11
if !Or(ValueGT[int](9), ValueLT[int](10))(0, i) {
t.Error("11 is greater than 9 and 10")
}
}
func TestOrReturningFalse(t *testing.T) {
i := 1
if Or(ValueGT[int](1), ValueLT[int](1))(0, i) {
t.Error("1 is not less, nor greater than 1")
}
}
func TestOrValue(t *testing.T) {
i := 11
if !OrValue(
10,
func(v int) Matcher[int, int] { return ValueGT[int](v) },
func(v int) Matcher[int, int] { return ValueLT[int](2 * v) },
)(0, i) {
t.Error("11 is greater than 10 and lesser than 20")
}
}
func TestOrValueReturningFalse(t *testing.T) {
i := 1
if OrValue(
1,
func(v int) Matcher[int, int] { return ValueGT[int](1) },
func(v int) Matcher[int, int] { return ValueLT[int](1) },
)(0, i) {
t.Error("1 is not less, nor greater than 1")
}
}
func TestValueCastLT(t *testing.T) {
testCases := []struct {
matcherValue int
collectionValue int
expected bool
}{
{
matcherValue: 1,
collectionValue: 2,
expected: false,
},
{
matcherValue: 1,
collectionValue: 0,
expected: true,
},
{
matcherValue: 1,
collectionValue: -1,
expected: true,
},
}
for _, tc := range testCases {
if got := ValueCastLT(tc.matcherValue)(nil, tc.collectionValue); got != tc.expected {
t.Errorf("Expected %t, Got %t", tc.expected, got)
}
}
}
func TestValueCastGT(t *testing.T) {
testCases := []struct {
matcherValue int
collectionValue int
expected bool
}{
{
matcherValue: 1,
collectionValue: 2,
expected: true,
},
{
matcherValue: 1,
collectionValue: 0,
expected: false,
},
{
matcherValue: 1,
collectionValue: -1,
expected: false,
},
}
for _, tc := range testCases {
if got := ValueCastGT(tc.matcherValue)(nil, tc.collectionValue); got != tc.expected {
t.Errorf("Expected %t, Got %t", tc.expected, got)
}
}
}