-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterval_test.go
More file actions
387 lines (364 loc) · 10.8 KB
/
Copy pathinterval_test.go
File metadata and controls
387 lines (364 loc) · 10.8 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package vers
import "testing"
func TestNewInterval(t *testing.T) {
i := NewInterval("1.0.0", "2.0.0", true, false)
if i.Min != "1.0.0" { //nolint:goconst
t.Errorf("Min = %q, want %q", i.Min, "1.0.0")
}
if i.Max != "2.0.0" { //nolint:goconst
t.Errorf("Max = %q, want %q", i.Max, "2.0.0")
}
if !i.MinInclusive {
t.Error("MinInclusive = false, want true")
}
if i.MaxInclusive {
t.Error("MaxInclusive = true, want false")
}
}
func TestExactInterval(t *testing.T) {
i := ExactInterval("1.2.3")
if i.Min != "1.2.3" || i.Max != "1.2.3" {
t.Errorf("ExactInterval bounds incorrect: [%s, %s]", i.Min, i.Max)
}
if !i.MinInclusive || !i.MaxInclusive {
t.Error("ExactInterval should be inclusive on both ends")
}
if !i.Contains("1.2.3") {
t.Error("ExactInterval should contain its version")
}
if i.Contains("1.2.4") {
t.Error("ExactInterval should not contain other versions")
}
}
func TestGreaterThanInterval(t *testing.T) {
tests := []struct {
name string
version string
inclusive bool
check string
want bool
}{
{">1.0.0 contains 1.0.1", "1.0.0", false, "1.0.1", true},
{">1.0.0 excludes 1.0.0", "1.0.0", false, "1.0.0", false},
{">1.0.0 excludes 0.9.9", "1.0.0", false, "0.9.9", false},
{">=1.0.0 contains 1.0.0", "1.0.0", true, "1.0.0", true},
{">=1.0.0 contains 1.0.1", "1.0.0", true, "1.0.1", true},
{">=1.0.0 excludes 0.9.9", "1.0.0", true, "0.9.9", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := GreaterThanInterval(tt.version, tt.inclusive)
got := i.Contains(tt.check)
if got != tt.want {
t.Errorf("Contains(%q) = %v, want %v", tt.check, got, tt.want)
}
})
}
}
func TestLessThanInterval(t *testing.T) {
tests := []struct {
name string
version string
inclusive bool
check string
want bool
}{
{"<2.0.0 contains 1.9.9", "2.0.0", false, "1.9.9", true},
{"<2.0.0 excludes 2.0.0", "2.0.0", false, "2.0.0", false},
{"<2.0.0 excludes 2.0.1", "2.0.0", false, "2.0.1", false},
{"<=2.0.0 contains 2.0.0", "2.0.0", true, "2.0.0", true},
{"<=2.0.0 contains 1.9.9", "2.0.0", true, "1.9.9", true},
{"<=2.0.0 excludes 2.0.1", "2.0.0", true, "2.0.1", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := LessThanInterval(tt.version, tt.inclusive)
got := i.Contains(tt.check)
if got != tt.want {
t.Errorf("Contains(%q) = %v, want %v", tt.check, got, tt.want)
}
})
}
}
func TestIntervalIsEmpty(t *testing.T) {
tests := []struct {
name string
i Interval
want bool
}{
{"empty interval", EmptyInterval(), true},
{"min > max", NewInterval("2.0.0", "1.0.0", true, true), true},
{"equal exclusive min", NewInterval("1.0.0", "1.0.0", false, true), true},
{"equal exclusive max", NewInterval("1.0.0", "1.0.0", true, false), true},
{"equal exclusive both", NewInterval("1.0.0", "1.0.0", false, false), true},
{"equal inclusive", NewInterval("1.0.0", "1.0.0", true, true), false},
{"valid range", NewInterval("1.0.0", "2.0.0", true, false), false},
{"unbounded", UnboundedInterval(), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i.IsEmpty()
if got != tt.want {
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntervalIsUnbounded(t *testing.T) {
tests := []struct {
name string
i Interval
want bool
}{
{"unbounded", UnboundedInterval(), true},
{"has min", GreaterThanInterval("1.0.0", true), false},
{"has max", LessThanInterval("2.0.0", true), false},
{"has both", NewInterval("1.0.0", "2.0.0", true, true), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i.IsUnbounded()
if got != tt.want {
t.Errorf("IsUnbounded() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntervalContains(t *testing.T) {
tests := []struct {
name string
i Interval
version string
want bool
}{
{"[1.0.0,2.0.0] contains 1.0.0", NewInterval("1.0.0", "2.0.0", true, true), "1.0.0", true},
{"[1.0.0,2.0.0] contains 1.5.0", NewInterval("1.0.0", "2.0.0", true, true), "1.5.0", true},
{"[1.0.0,2.0.0] contains 2.0.0", NewInterval("1.0.0", "2.0.0", true, true), "2.0.0", true},
{"[1.0.0,2.0.0] excludes 0.9.0", NewInterval("1.0.0", "2.0.0", true, true), "0.9.0", false},
{"[1.0.0,2.0.0] excludes 2.0.1", NewInterval("1.0.0", "2.0.0", true, true), "2.0.1", false},
{"[1.0.0,2.0.0) excludes 2.0.0", NewInterval("1.0.0", "2.0.0", true, false), "2.0.0", false},
{"(1.0.0,2.0.0] excludes 1.0.0", NewInterval("1.0.0", "2.0.0", false, true), "1.0.0", false},
{"unbounded contains anything", UnboundedInterval(), "999.999.999", true},
{"empty contains nothing", EmptyInterval(), "1.0.0", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i.Contains(tt.version)
if got != tt.want {
t.Errorf("Contains(%q) = %v, want %v", tt.version, got, tt.want)
}
})
}
}
func TestIntervalIntersect(t *testing.T) {
tests := []struct {
name string
i1 Interval
i2 Interval
expect func(Interval) bool
}{
{
"overlapping ranges",
NewInterval("1.0.0", "3.0.0", true, true),
NewInterval("2.0.0", "4.0.0", true, true),
func(r Interval) bool {
return r.Min == "2.0.0" && r.Max == "3.0.0" && r.MinInclusive && r.MaxInclusive //nolint:goconst
},
},
{
"one inside other",
NewInterval("1.0.0", "5.0.0", true, true),
NewInterval("2.0.0", "3.0.0", true, true),
func(r Interval) bool {
return r.Min == "2.0.0" && r.Max == "3.0.0"
},
},
{
"same boundary different inclusivity",
NewInterval("1.0.0", "2.0.0", true, true),
NewInterval("1.0.0", "2.0.0", false, false),
func(r Interval) bool {
return r.Min == "1.0.0" && r.Max == "2.0.0" && !r.MinInclusive && !r.MaxInclusive
},
},
{
"non-overlapping",
NewInterval("1.0.0", "2.0.0", true, true),
NewInterval("3.0.0", "4.0.0", true, true),
func(r Interval) bool {
return r.IsEmpty()
},
},
{
"unbounded with bounded",
UnboundedInterval(),
NewInterval("1.0.0", "2.0.0", true, false),
func(r Interval) bool {
return r.Min == "1.0.0" && r.Max == "2.0.0" && r.MinInclusive && !r.MaxInclusive
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.i1.Intersect(tt.i2)
if !tt.expect(result) {
t.Errorf("Intersect() = %v, didn't meet expectations", result)
}
})
}
}
func TestIntervalOverlaps(t *testing.T) {
tests := []struct {
name string
i1 Interval
i2 Interval
want bool
}{
{"overlapping", NewInterval("1.0.0", "3.0.0", true, true), NewInterval("2.0.0", "4.0.0", true, true), true},
{"touching inclusive", NewInterval("1.0.0", "2.0.0", true, true), NewInterval("2.0.0", "3.0.0", true, true), true},
{"touching exclusive", NewInterval("1.0.0", "2.0.0", true, false), NewInterval("2.0.0", "3.0.0", false, true), false},
{"non-overlapping", NewInterval("1.0.0", "2.0.0", true, true), NewInterval("3.0.0", "4.0.0", true, true), false},
{"empty", EmptyInterval(), NewInterval("1.0.0", "2.0.0", true, true), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i1.Overlaps(tt.i2)
if got != tt.want {
t.Errorf("Overlaps() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntervalAdjacent(t *testing.T) {
tests := []struct {
name string
i1 Interval
i2 Interval
want bool
}{
{"adjacent [,a] (a,]", NewInterval("1.0.0", "2.0.0", true, true), NewInterval("2.0.0", "3.0.0", false, true), true},
{"adjacent [,a) [a,]", NewInterval("1.0.0", "2.0.0", true, false), NewInterval("2.0.0", "3.0.0", true, true), true},
{"not adjacent [,a] [a,]", NewInterval("1.0.0", "2.0.0", true, true), NewInterval("2.0.0", "3.0.0", true, true), false},
{"gap between", NewInterval("1.0.0", "2.0.0", true, true), NewInterval("3.0.0", "4.0.0", true, true), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i1.Adjacent(tt.i2)
if got != tt.want {
t.Errorf("Adjacent() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntervalUnion(t *testing.T) {
tests := []struct {
name string
i1 Interval
i2 Interval
isNil bool
expect func(*Interval) bool
}{
{
"overlapping",
NewInterval("1.0.0", "3.0.0", true, true),
NewInterval("2.0.0", "4.0.0", true, true),
false,
func(r *Interval) bool {
return r.Min == "1.0.0" && r.Max == "4.0.0" && r.MinInclusive && r.MaxInclusive
},
},
{
"adjacent",
NewInterval("1.0.0", "2.0.0", true, false),
NewInterval("2.0.0", "3.0.0", true, true),
false,
func(r *Interval) bool {
return r.Min == "1.0.0" && r.Max == "3.0.0"
},
},
{
"non-overlapping non-adjacent",
NewInterval("1.0.0", "2.0.0", true, true),
NewInterval("3.0.0", "4.0.0", true, true),
true,
nil,
},
{
"same boundary different inclusivity",
NewInterval("1.0.0", "2.0.0", true, false),
NewInterval("1.0.0", "2.0.0", false, true),
false,
func(r *Interval) bool {
return r.MinInclusive && r.MaxInclusive
},
},
{
"unbounded min with bounded",
LessThanInterval("2.0.0", true),
NewInterval("1.0.0", "3.0.0", true, true),
false,
func(r *Interval) bool {
return r.Min == "" && r.Max == "3.0.0" && r.MaxInclusive
},
},
{
"bounded with unbounded max",
NewInterval("1.0.0", "3.0.0", true, true),
GreaterThanInterval("2.0.0", true),
false,
func(r *Interval) bool {
return r.Min == "1.0.0" && r.MinInclusive && r.Max == ""
},
},
{
"unbounded min with unbounded max",
LessThanInterval("2.0.0", true),
GreaterThanInterval("1.0.0", true),
false,
func(r *Interval) bool {
return r.Min == "" && r.Max == ""
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.i1.Union(tt.i2)
if tt.isNil {
if result != nil {
t.Errorf("Union() = %v, want nil", result)
}
} else {
if result == nil {
t.Error("Union() = nil, want non-nil")
} else if !tt.expect(result) {
t.Errorf("Union() = %v, didn't meet expectations", result)
}
}
})
}
}
func TestIntervalString(t *testing.T) {
tests := []struct {
name string
i Interval
want string
}{
{"empty", EmptyInterval(), "empty"},
{"unbounded", UnboundedInterval(), "(-inf,+inf)"},
{"[1.0.0,2.0.0]", NewInterval("1.0.0", "2.0.0", true, true), "[1.0.0,2.0.0]"},
{"(1.0.0,2.0.0)", NewInterval("1.0.0", "2.0.0", false, false), "(1.0.0,2.0.0)"},
{"[1.0.0,2.0.0)", NewInterval("1.0.0", "2.0.0", true, false), "[1.0.0,2.0.0)"},
{">=1.0.0", GreaterThanInterval("1.0.0", true), "[1.0.0,+inf)"},
{">1.0.0", GreaterThanInterval("1.0.0", false), "(1.0.0,+inf)"},
{"<=2.0.0", LessThanInterval("2.0.0", true), "(-inf,2.0.0]"},
{"<2.0.0", LessThanInterval("2.0.0", false), "(-inf,2.0.0)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.i.String()
if got != tt.want {
t.Errorf("String() = %q, want %q", got, tt.want)
}
})
}
}