-
Notifications
You must be signed in to change notification settings - Fork 4
/
range_test.go
639 lines (534 loc) · 14.9 KB
/
range_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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Copyright 2014 The Semver Package Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package semver
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func hasLowerBound(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
if s := ShouldResemble(a.lower, aVersion[0]); s != "" {
return s
}
return ShouldBeTrue(a.equalsLower)
}
func isLeftClosedBy(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
if s := ShouldResemble(a.lower, aVersion[0]); s != "" {
return s
}
return ShouldBeFalse(a.equalsLower)
}
func hasUpperBound(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
if s := ShouldResemble(a.upper, aVersion[0]); s != "" {
return s
}
return ShouldBeTrue(a.equalsUpper)
}
func isRightClosedBy(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
if s := ShouldResemble(a.upper, aVersion[0]); s != "" {
return s
}
return ShouldBeFalse(a.equalsUpper)
}
func testIfResembles(actual, expected Range) {
So(actual.lower, ShouldResemble, expected.lower)
So(actual.equalsLower, ShouldEqual, expected.equalsLower)
So(actual.upper, ShouldResemble, expected.upper)
So(actual.equalsUpper, ShouldEqual, expected.equalsUpper)
}
func shouldContain(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
for _, version := range aVersion {
v := version.(string)
ver, err := NewVersion([]byte(v))
if err != nil {
return err.Error()
}
if s := ShouldBeTrue(a.Contains(ver)); s != "" {
return v + " is not in Range"
}
}
return ""
}
func shouldNotContain(aRange interface{}, aVersion ...interface{}) string {
a := aRange.(Range)
for _, version := range aVersion {
v := version.(string)
ver, err := NewVersion([]byte(v))
if err != nil {
return err.Error()
}
if s := ShouldBeFalse(a.Contains(ver)); s != "" {
return v + " is in Range"
}
}
return ""
}
func TestRangeConstruction(t *testing.T) {
Convey("version 1.2.3 should be part of…", t, func() {
ver := MustParse("1.2.3")
Convey("specific Range 1.2.3", func() {
verRange, err := NewRange([]byte("1.2.3"))
So(err, ShouldBeNil)
if err != nil {
return
}
So(verRange.lower, ShouldResemble, ver)
})
})
v100 := MustParse("1.0.0")
v120 := MustParse("1.2.0")
v123 := MustParse("1.2.3")
v130 := MustParse("1.3.0")
v200 := MustParse("2.0.0")
Convey("Range >=1.2.3 <=1.3.0…", t, func() {
verRange, err := NewRange([]byte(">=1.2.3 <=1.3.0"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.3", func() {
So(verRange, hasLowerBound, v123)
})
Convey("has upper bound <=1.3.0", func() {
So(verRange, hasUpperBound, v130)
})
})
Convey("Range >1.2.3 <1.3.0…", t, func() {
verRange, err := NewRange([]byte(">1.2.3 <1.3.0"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >1.2.3", func() {
So(verRange, isLeftClosedBy, v123)
})
Convey("has upper bound <1.3.0", func() {
So(verRange, isRightClosedBy, v130)
})
})
Convey("Range 1.2.3 - 1.3.0…", t, func() {
verRange, err := NewRange([]byte("1.2.3 - 1.3.0"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.3", func() {
So(verRange, hasLowerBound, v123)
})
Convey("has upper bound <=1.3.0", func() {
So(verRange, hasUpperBound, v130)
})
})
Convey("Range ~1.2.3 equals: >=1.2.3 <1.3.0", t, func() {
verRange, err := NewRange([]byte("~1.2.3"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.3", func() {
So(verRange, hasLowerBound, v123)
})
Convey("has upper bound <1.3.0", func() {
So(verRange, isRightClosedBy, v130)
})
})
Convey("Range ^1.2.3 equals: >=1.2.3 <2.0.0", t, func() {
verRange, err := NewRange([]byte("^1.2.3"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.3", func() {
So(verRange, hasLowerBound, v123)
})
Convey("has upper bound <2.0.0", func() {
So(verRange, isRightClosedBy, v200)
})
})
Convey("Range ~1.2 equals: >=1.2.0 <1.3.0", t, func() {
verRange, err := NewRange([]byte("~1.2"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.0", func() {
So(verRange, hasLowerBound, v120)
})
Convey("has upper bound <1.3.0", func() {
So(verRange, isRightClosedBy, v130)
})
})
Convey("Range ^1.2 equals: >=1.2.0 <2.0.0", t, func() {
verRange, err := NewRange([]byte("^1.2"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.2.0", func() {
So(verRange, hasLowerBound, v120)
})
Convey("has upper bound <2.0.0", func() {
So(verRange, isRightClosedBy, v200)
})
})
Convey("Ranges ^1 and ~1 equal: >=1.0.0 <2.0.0", t, func() {
r1, err := NewRange([]byte("^1"))
So(err, ShouldBeNil)
if err != nil {
return
}
r2, err := NewRange([]byte("^1"))
So(err, ShouldBeNil)
if err != nil {
return
}
Convey("has lower bound >=1.0.0", func() {
So(r1, hasLowerBound, v100)
So(r2, hasLowerBound, v100)
})
Convey("has upper bound <2.0.0", func() {
So(r1, isRightClosedBy, v200)
So(r2, isRightClosedBy, v200)
})
})
Convey("Given .x and .* notations", t, func() {
refRange, _ := NewRange([]byte("^1"))
Convey("1.x equals ^1", func() {
r1, err := NewRange([]byte("1.x"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
Convey("1.* equals ^1", func() {
r1, err := NewRange([]byte("1.*"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
Convey("1 equals ^1", func() {
r1, err := NewRange([]byte("1"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
smallRange, _ := NewRange([]byte("~1.2"))
Convey("1.2.x equals ~1.2", func() {
r1, err := NewRange([]byte("1.2.x"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, smallRange)
})
Convey("1.2.* equals ~1.2", func() {
r1, err := NewRange([]byte("1.2.*"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, smallRange)
})
Convey("1.2 equals ~1.2", func() {
r1, err := NewRange([]byte("1.2"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, smallRange)
})
})
Convey("Notations for 'any'", t, func() {
refRange := Range{}
Convey("'x'", func() {
r1, err := NewRange([]byte("x"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
Convey("'*'", func() {
r1, err := NewRange([]byte("*"))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
Convey("'' (empty string)", func() {
r1, err := NewRange([]byte(""))
So(err, ShouldBeNil)
if err != nil {
return
}
testIfResembles(r1, refRange)
})
})
// now come fringe cases
Convey("Range ^0.1.3 and ~0.1.3 equal: >=0.1.3 <0.2.0", t, func() {
r1, err := NewRange([]byte("^0.1.3"))
So(err, ShouldBeNil)
if err != nil {
return
}
r2, err := NewRange([]byte("~0.1.3"))
So(err, ShouldBeNil)
if err != nil {
return
}
v013 := MustParse("0.1.3")
v020 := MustParse("0.2.0")
Convey("have lower bound >=0.1.3", func() {
So(r1, hasLowerBound, v013)
So(r2, hasLowerBound, v013)
})
Convey("have upper bound <0.2.0", func() {
So(r1, isRightClosedBy, v020)
So(r2, isRightClosedBy, v020)
})
})
Convey("Range ^0.0.2 and ~0.0.2 are 0.2.0", t, func() {
r1, err := NewRange([]byte("^0.0.2"))
So(err, ShouldBeNil)
if err != nil {
return
}
r2, err := NewRange([]byte("~0.0.2"))
So(err, ShouldBeNil)
if err != nil {
return
}
r002, _ := NewRange([]byte("0.0.2"))
Convey("have the same bounds as 0.2.0", func() {
testIfResembles(r1, r002)
testIfResembles(r2, r002)
})
})
Convey("Reject invalid ranges such as", t, func() {
for _, s := range []string{
" - 1.0", "- 1.0",
"1 - X", "X - 1",
"1 - 5.6.7.8.9", "1.2.3.4.5 - 1",
} {
Convey(s, func() {
_, err := NewRange([]byte(s))
So(err, ShouldNotBeNil)
})
}
})
}
func TestSingleBound(t *testing.T) {
Convey("Given a specific version as Range…", t, func() {
Convey("1.2.3…", func() {
verRange, _ := NewRange([]byte("1.2.3"))
Convey("reject Version 1.2.4", func() {
So(verRange, shouldNotContain, "1.2.4")
})
Convey("accept Version 1.2.3", func() {
So(verRange, shouldContain, "1.2.3")
})
Convey("accept Version 1.2.3+build2014 (ignore build)", func() {
So(verRange, shouldContain, "1.2.3+build2014")
})
Convey("accept Version 1.2.3-p1 (patch levels are reasonably equal)", func() {
So(verRange, shouldContain, "1.2.3-p1")
})
Convey("reject pre-releases like 1.2.3-alpha", func() {
So(verRange, shouldNotContain, "1.2.3-alpha")
})
})
Convey("1.2.3-alpha20…", func() {
verRange, _ := NewRange([]byte("1.2.3-alpha20"))
Convey("accept Version 1.2.3-alpha20", func() {
So(verRange, shouldContain, "1.2.3-alpha20")
})
Convey("reject Version 1.2.3-alpha5", func() {
So(verRange, shouldNotContain, "1.2.3-alpha5")
})
Convey("reject Version 1.2.3-beta", func() {
So(verRange, shouldNotContain, "1.2.3-beta")
})
Convey("reject Version 1.2.3", func() {
So(verRange, shouldNotContain, "1.2.3")
})
})
})
Convey("Given the lower bound >1.2.3", t, func() {
verRange, _ := NewRange([]byte(">1.2.3"))
So(verRange.GetUpperBoundary(), ShouldBeNil)
Convey("reject Version 1.2.3", func() {
So(verRange, shouldNotContain, "1.2.3")
})
Convey("reject Version 1.2.3-p1 (ignore release/pre-release)", func() {
So(verRange, shouldNotContain, "1.2.3-p1")
})
Convey("reject Version 1.2.3+build2014 (ignore build)", func() {
So(verRange, shouldNotContain, "1.2.3+build2014")
})
Convey("accept Versions…", func() {
Convey("1.2.4", func() {
So(verRange, shouldContain, "1.2.4")
})
Convey("1.3.0", func() {
So(verRange, shouldContain, "1.3.0")
})
Convey("2.0.0", func() {
So(verRange, shouldContain, "2.0.0")
})
})
})
Convey("A lower bound >=1.2.3…", t, func() {
verRange, _ := NewRange([]byte(">=1.2.3"))
Convey("should contain Version 1.2.3", func() {
So(verRange, shouldContain, "1.2.3")
})
Convey("should contain Version 1.2.3-p1", func() {
So(verRange, shouldContain, "1.2.3-p1")
})
Convey("but NOT contain 1.2.3-rc (exclude pre-release)", func() {
So(verRange, shouldNotContain, "1.2.3-rc")
})
})
Convey("Over-specific lower bounds >=1.2.3-alpha4…", t, func() {
verRange, _ := NewRange([]byte(">=1.2.3-alpha4"))
Convey("should contain Version 1.2.4", func() {
So(verRange, shouldContain, "1.2.4")
})
Convey("should contain Version 1.2.3-alpha4", func() {
So(verRange, shouldContain, "1.2.3-alpha4")
})
Convey("but NOT contain 1.2.3-alpha", func() {
So(verRange, shouldNotContain, "1.2.3-alpha")
})
})
Convey("Upper bounds such as <1.2.3…", t, func() {
verRange, _ := NewRange([]byte("<1.2.3"))
So(verRange.GetLowerBoundary(), ShouldBeNil)
Convey("reject Version 1.2.3", func() {
So(verRange, shouldNotContain, "1.2.3")
})
Convey("reject Version 1.2.3-alpha3 (ignore release/pre-release)", func() {
So(verRange, shouldNotContain, "1.2.3-alpha3")
})
Convey("reject Version 1.2.3+build2014 (ignore build)", func() {
So(verRange, shouldNotContain, "1.2.3+build2014")
})
Convey("accept Versions…", func() {
Convey("1.2.0", func() {
So(verRange, shouldContain, "1.2.0")
})
Convey("1.1.0", func() {
So(verRange, shouldContain, "1.1.0")
})
Convey("1.0.0", func() {
So(verRange, shouldContain, "1.0.0")
})
})
})
Convey("An over-specific upper bound <1.2.3-beta20…", t, func() {
verRange, _ := NewRange([]byte("<1.2.3-beta20"))
Convey("reject Version 1.2.3-beta20", func() {
So(verRange, shouldNotContain, "1.2.3-beta20")
})
Convey("reject Version 1.2.3-beta20-alpha3 (ignore release specifier)", func() {
So(verRange, shouldNotContain, "1.2.3-beta20-alpha3")
})
Convey("reject Version 1.2.3-beta20+build2014 (ignore build)", func() {
So(verRange, shouldNotContain, "1.2.3-beta20+build2014")
})
Convey("accept Versions…", func() {
Convey("1.2.3-beta19", func() {
So(verRange, shouldContain, "1.2.3-beta19")
})
Convey("1.2.3-alpha", func() {
So(verRange, shouldContain, "1.2.3-alpha")
})
Convey("1.2.0", func() {
So(verRange, shouldContain, "1.2.0")
})
})
})
Convey("An upper bound with equality <=1.2.3…", t, func() {
verRange, _ := NewRange([]byte("<=1.2.3"))
Convey("should contain Version 1.2.3", func() {
So(verRange, shouldContain, "1.2.3")
})
Convey("should contain Version 1.2.3-p1", func() {
So(verRange, shouldContain, "1.2.3-p1")
})
Convey("and, that's new, contain 1.2.3-rc (INCLUDE pre-release)", func() {
So(verRange, shouldContain, "1.2.3-rc")
})
})
}
func TestSatisfies(t *testing.T) {
Convey("Convenience function 'Satisfies'", t, func() {
Convey("works with valid input", func() {
t, _ := Satisfies("1.2.3", "^1.2.2")
So(t, ShouldBeTrue)
t, _ = Satisfies("1.2.3-pre2", "^1.2.3-pre1")
So(t, ShouldBeTrue)
})
Convey("yields an error on invalid Version", func() {
t, err := Satisfies("1.2.3.4.5.6", "^1.2.2")
So(t, ShouldBeFalse)
So(err, ShouldNotBeNil)
})
Convey("yields an error on invalid Range", func() {
t, err := Satisfies("1.2.3", "^1.2.2/1.2.5")
So(t, ShouldBeFalse)
So(err, ShouldNotBeNil)
})
})
Convey("Range.IsSatisfiedBy…", t, func() {
Convey("rejects pre-releases", func() {
t, _ := Satisfies("1.2.3-pre1", "^1.2.2")
So(t, ShouldBeFalse)
t, _ = Satisfies("1.2.4-pre1", "^1.2.2-pre1")
So(t, ShouldBeFalse)
t, _ = Satisfies("1.2.3-pre1", "<1.2.3")
So(t, ShouldBeFalse)
})
Convey("accepts pre-releases for a pre-release upper bound with the same prefix", func() {
t, _ := Satisfies("1.2.3-pre1", "<1.2.3-pre1")
So(t, ShouldBeFalse)
t, _ = Satisfies("1.2.3-pre1", "<1.2.3-pre2")
So(t, ShouldBeTrue)
t, _ = Satisfies("1.2.3-pre1", "<=1.2.3-pre1")
So(t, ShouldBeTrue)
})
Convey("accepts pre-releases for a pre-release lower bound with the same prefix", func() {
t, _ := Satisfies("1.2.3-pre1", ">1.2.3-pre1")
So(t, ShouldBeFalse)
t, _ = Satisfies("1.2.3-pre2", ">1.2.3-pre1")
So(t, ShouldBeTrue)
t, _ = Satisfies("1.2.3-pre1", ">=1.2.3-pre1")
So(t, ShouldBeTrue)
})
})
Convey("Test the examples found in README file.", t, func() {
v := MustParse("1.2.3-beta")
r, _ := NewRange([]byte("~1.2"))
So(r.Contains(v), ShouldBeTrue)
So(r.IsSatisfiedBy(v), ShouldBeFalse)
})
}
var benchR, benchRErr = NewRange([]byte(">=1.2.3 <=1.3.0"))
func BenchmarkSemverNewRange(b *testing.B) {
var r, e = NewRange([]byte(">=1.2.3 <=1.3.0"))
for n := 0; n < b.N; n++ {
r, e = NewRange([]byte(">=1.2.3 <=1.3.0"))
}
benchR, benchRErr = r, e
}