This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
constraint_test.go
903 lines (759 loc) · 27 KB
/
constraint_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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
package gps
import (
"fmt"
"testing"
)
// gu - helper func for stringifying what we assume is a VersionPair (otherwise
// will panic), but is given as a Constraint
func gu(v Constraint) string {
return fmt.Sprintf("%q at rev %q", v, v.(PairedVersion).Underlying())
}
func TestBranchConstraintOps(t *testing.T) {
v1 := NewBranch("master").(branchVersion)
v2 := NewBranch("test").(branchVersion)
if !v1.MatchesAny(any) {
t.Errorf("Branches should always match the any constraint")
}
if v1.Intersect(any) != v1 {
t.Errorf("Branches should always return self when intersecting the any constraint, but got %s", v1.Intersect(any))
}
if v1.MatchesAny(none) {
t.Errorf("Branches should never match the none constraint")
}
if v1.Intersect(none) != none {
t.Errorf("Branches should always return none when intersecting the none constraint, but got %s", v1.Intersect(none))
}
if v1.Matches(v2) {
t.Errorf("%s should not match %s", v1, v2)
}
if v1.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v1, v2)
}
if v1.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v1, v2)
}
// Add rev to one
snuffster := Revision("snuffleupagus")
v3 := v1.Is(snuffster).(versionPair)
if v2.Matches(v3) {
t.Errorf("%s should not match %s", v2, gu(v3))
}
if v3.Matches(v2) {
t.Errorf("%s should not match %s", gu(v3), v2)
}
if v2.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v3.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v2.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v2, gu(v3))
}
if v3.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), v2)
}
// Add different rev to the other
v4 := v2.Is(Revision("cookie monster")).(versionPair)
if v4.Matches(v3) {
t.Errorf("%s should not match %s", gu(v4), gu(v3))
}
if v3.Matches(v4) {
t.Errorf("%s should not match %s", gu(v3), gu(v4))
}
if v4.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v3.MatchesAny(v4) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v4.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v4), gu(v3))
}
if v3.Intersect(v4) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), gu(v4))
}
// Now add same rev to different branches
// TODO(sdboyer) this might not actually be a good idea, when you consider the
// semantics of floating versions...matching on an underlying rev might be
// nice in the short term, but it's probably shit most of the time
v5 := v2.Is(Revision("snuffleupagus")).(versionPair)
if !v5.Matches(v3) {
t.Errorf("%s should match %s", gu(v5), gu(v3))
}
if !v3.Matches(v5) {
t.Errorf("%s should match %s", gu(v3), gu(v5))
}
if !v5.MatchesAny(v3) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if !v3.MatchesAny(v5) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if v5.Intersect(v3) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v5), gu(v3))
}
if v3.Intersect(v5) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v3), gu(v5))
}
// Set up for cross-type constraint ops
cookie := Revision("cookie monster")
o1 := NewVersion("master").(plainVersion)
o2 := NewVersion("1.0.0").(semVersion)
o3 := o1.Is(cookie).(versionPair)
o4 := o2.Is(cookie).(versionPair)
v6 := v1.Is(cookie).(versionPair)
if v1.Matches(o1) {
t.Errorf("%s (branch) should not match %s (version) across types", v1, o1)
}
if v1.MatchesAny(o1) {
t.Errorf("%s (branch) should not allow any matches when combined with %s (version)", v1, o1)
}
if v1.Intersect(o1) != none {
t.Errorf("Intersection of %s (branch) with %s (version) should result in empty set", v1, o1)
}
if v1.Matches(o2) {
t.Errorf("%s (branch) should not match %s (semver) across types", v1, o2)
}
if v1.MatchesAny(o2) {
t.Errorf("%s (branch) should not allow any matches when combined with %s (semver)", v1, o2)
}
if v1.Intersect(o2) != none {
t.Errorf("Intersection of %s (branch) with %s (semver) should result in empty set", v1, o2)
}
if v1.Matches(o3) {
t.Errorf("%s (branch) should not match %s (version) across types", v1, gu(o3))
}
if v1.MatchesAny(o3) {
t.Errorf("%s (branch) should not allow any matches when combined with %s (version)", v1, gu(o3))
}
if v1.Intersect(o3) != none {
t.Errorf("Intersection of %s (branch) with %s (version) should result in empty set", v1, gu(o3))
}
if v1.Matches(o4) {
t.Errorf("%s (branch) should not match %s (semver) across types", v1, gu(o4))
}
if v1.MatchesAny(o4) {
t.Errorf("%s (branch) should not allow any matches when combined with %s (semver)", v1, gu(o4))
}
if v1.Intersect(o4) != none {
t.Errorf("Intersection of %s (branch) with %s (semver) should result in empty set", v1, gu(o4))
}
if !v6.Matches(o3) {
t.Errorf("%s (branch) should match %s (version) across types due to shared rev", gu(v6), gu(o3))
}
if !v6.MatchesAny(o3) {
t.Errorf("%s (branch) should allow some matches when combined with %s (version) across types due to shared rev", gu(v6), gu(o3))
}
if v6.Intersect(o3) != cookie {
t.Errorf("Intersection of %s (branch) with %s (version) should return shared underlying rev", gu(v6), gu(o3))
}
if !v6.Matches(o4) {
t.Errorf("%s (branch) should match %s (version) across types due to shared rev", gu(v6), gu(o4))
}
if !v6.MatchesAny(o4) {
t.Errorf("%s (branch) should allow some matches when combined with %s (version) across types due to shared rev", gu(v6), gu(o4))
}
if v6.Intersect(o4) != cookie {
t.Errorf("Intersection of %s (branch) with %s (version) should return shared underlying rev", gu(v6), gu(o4))
}
}
func TestVersionConstraintOps(t *testing.T) {
v1 := NewVersion("ab123").(plainVersion)
v2 := NewVersion("b2a13").(plainVersion)
if !v1.MatchesAny(any) {
t.Errorf("Versions should always match the any constraint")
}
if v1.Intersect(any) != v1 {
t.Errorf("Versions should always return self when intersecting the any constraint, but got %s", v1.Intersect(any))
}
if v1.MatchesAny(none) {
t.Errorf("Versions should never match the none constraint")
}
if v1.Intersect(none) != none {
t.Errorf("Versions should always return none when intersecting the none constraint, but got %s", v1.Intersect(none))
}
if v1.Matches(v2) {
t.Errorf("%s should not match %s", v1, v2)
}
if v1.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v1, v2)
}
if v1.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v1, v2)
}
// Add rev to one
snuffster := Revision("snuffleupagus")
v3 := v1.Is(snuffster).(versionPair)
if v2.Matches(v3) {
t.Errorf("%s should not match %s", v2, gu(v3))
}
if v3.Matches(v2) {
t.Errorf("%s should not match %s", gu(v3), v2)
}
if v2.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v3.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v2.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v2, gu(v3))
}
if v3.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), v2)
}
// Add different rev to the other
v4 := v2.Is(Revision("cookie monster")).(versionPair)
if v4.Matches(v3) {
t.Errorf("%s should not match %s", gu(v4), gu(v3))
}
if v3.Matches(v4) {
t.Errorf("%s should not match %s", gu(v3), gu(v4))
}
if v4.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v3.MatchesAny(v4) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v4.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v4), gu(v3))
}
if v3.Intersect(v4) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), gu(v4))
}
// Now add same rev to different versions, and things should line up
v5 := v2.Is(Revision("snuffleupagus")).(versionPair)
if !v5.Matches(v3) {
t.Errorf("%s should match %s", gu(v5), gu(v3))
}
if !v3.Matches(v5) {
t.Errorf("%s should match %s", gu(v3), gu(v5))
}
if !v5.MatchesAny(v3) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if !v3.MatchesAny(v5) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if v5.Intersect(v3) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v5), gu(v3))
}
if v3.Intersect(v5) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v3), gu(v5))
}
// Set up for cross-type constraint ops
cookie := Revision("cookie monster")
o1 := NewBranch("master").(branchVersion)
o2 := NewVersion("1.0.0").(semVersion)
o3 := o1.Is(cookie).(versionPair)
o4 := o2.Is(cookie).(versionPair)
v6 := v1.Is(cookie).(versionPair)
if v1.Matches(o1) {
t.Errorf("%s (version) should not match %s (branch) across types", v1, o1)
}
if v1.MatchesAny(o1) {
t.Errorf("%s (version) should not allow any matches when combined with %s (branch)", v1, o1)
}
if v1.Intersect(o1) != none {
t.Errorf("Intersection of %s (version) with %s (branch) should result in empty set", v1, o1)
}
if v1.Matches(o2) {
t.Errorf("%s (version) should not match %s (semver) across types", v1, o2)
}
if v1.MatchesAny(o2) {
t.Errorf("%s (version) should not allow any matches when combined with %s (semver)", v1, o2)
}
if v1.Intersect(o2) != none {
t.Errorf("Intersection of %s (version) with %s (semver) should result in empty set", v1, o2)
}
if v1.Matches(o3) {
t.Errorf("%s (version) should not match %s (branch) across types", v1, gu(o3))
}
if v1.MatchesAny(o3) {
t.Errorf("%s (version) should not allow any matches when combined with %s (branch)", v1, gu(o3))
}
if v1.Intersect(o3) != none {
t.Errorf("Intersection of %s (version) with %s (branch) should result in empty set", v1, gu(o3))
}
if v1.Matches(o4) {
t.Errorf("%s (version) should not match %s (semver) across types", v1, gu(o4))
}
if v1.MatchesAny(o4) {
t.Errorf("%s (version) should not allow any matches when combined with %s (semver)", v1, gu(o4))
}
if v1.Intersect(o4) != none {
t.Errorf("Intersection of %s (version) with %s (semver) should result in empty set", v1, gu(o4))
}
if !v6.Matches(o3) {
t.Errorf("%s (version) should match %s (branch) across types due to shared rev", gu(v6), gu(o3))
}
if !v6.MatchesAny(o3) {
t.Errorf("%s (version) should allow some matches when combined with %s (branch) across types due to shared rev", gu(v6), gu(o3))
}
if v6.Intersect(o3) != cookie {
t.Errorf("Intersection of %s (version) with %s (branch) should return shared underlying rev", gu(v6), gu(o3))
}
if !v6.Matches(o4) {
t.Errorf("%s (version) should match %s (branch) across types due to shared rev", gu(v6), gu(o4))
}
if !v6.MatchesAny(o4) {
t.Errorf("%s (version) should allow some matches when combined with %s (branch) across types due to shared rev", gu(v6), gu(o4))
}
if v6.Intersect(o4) != cookie {
t.Errorf("Intersection of %s (version) with %s (branch) should return shared underlying rev", gu(v6), gu(o4))
}
}
func TestSemverVersionConstraintOps(t *testing.T) {
v1 := NewVersion("1.0.0").(semVersion)
v2 := NewVersion("2.0.0").(semVersion)
if !v1.MatchesAny(any) {
t.Errorf("Semvers should always match the any constraint")
}
if v1.Intersect(any) != v1 {
t.Errorf("Semvers should always return self when intersecting the any constraint, but got %s", v1.Intersect(any))
}
if v1.MatchesAny(none) {
t.Errorf("Semvers should never match the none constraint")
}
if v1.Intersect(none) != none {
t.Errorf("Semvers should always return none when intersecting the none constraint, but got %s", v1.Intersect(none))
}
if v1.Matches(v2) {
t.Errorf("%s should not match %s", v1, v2)
}
if v1.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v1, v2)
}
if v1.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v1, v2)
}
// Add rev to one
snuffster := Revision("snuffleupagus")
v3 := v1.Is(snuffster).(versionPair)
if v2.Matches(v3) {
t.Errorf("%s should not match %s", v2, gu(v3))
}
if v3.Matches(v2) {
t.Errorf("%s should not match %s", gu(v3), v2)
}
if v2.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v3.MatchesAny(v2) {
t.Errorf("%s should not allow any matches when combined with %s", v2, gu(v3))
}
if v2.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", v2, gu(v3))
}
if v3.Intersect(v2) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), v2)
}
// Add different rev to the other
v4 := v2.Is(Revision("cookie monster")).(versionPair)
if v4.Matches(v3) {
t.Errorf("%s should not match %s", gu(v4), gu(v3))
}
if v3.Matches(v4) {
t.Errorf("%s should not match %s", gu(v3), gu(v4))
}
if v4.MatchesAny(v3) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v3.MatchesAny(v4) {
t.Errorf("%s should not allow any matches when combined with %s", gu(v4), gu(v3))
}
if v4.Intersect(v3) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v4), gu(v3))
}
if v3.Intersect(v4) != none {
t.Errorf("Intersection of %s with %s should result in empty set", gu(v3), gu(v4))
}
// Now add same rev to different versions, and things should line up
v5 := v2.Is(Revision("snuffleupagus")).(versionPair)
if !v5.Matches(v3) {
t.Errorf("%s should match %s", gu(v5), gu(v3))
}
if !v3.Matches(v5) {
t.Errorf("%s should match %s", gu(v3), gu(v5))
}
if !v5.MatchesAny(v3) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if !v3.MatchesAny(v5) {
t.Errorf("%s should allow some matches when combined with %s", gu(v5), gu(v3))
}
if v5.Intersect(v3) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v5), gu(v3))
}
if v3.Intersect(v5) != snuffster {
t.Errorf("Intersection of %s with %s should return underlying rev", gu(v3), gu(v5))
}
// Set up for cross-type constraint ops
cookie := Revision("cookie monster")
o1 := NewBranch("master").(branchVersion)
o2 := NewVersion("ab123").(plainVersion)
o3 := o1.Is(cookie).(versionPair)
o4 := o2.Is(cookie).(versionPair)
v6 := v1.Is(cookie).(versionPair)
if v1.Matches(o1) {
t.Errorf("%s (semver) should not match %s (branch) across types", v1, o1)
}
if v1.MatchesAny(o1) {
t.Errorf("%s (semver) should not allow any matches when combined with %s (branch)", v1, o1)
}
if v1.Intersect(o1) != none {
t.Errorf("Intersection of %s (semver) with %s (branch) should result in empty set", v1, o1)
}
if v1.Matches(o2) {
t.Errorf("%s (semver) should not match %s (version) across types", v1, o2)
}
if v1.MatchesAny(o2) {
t.Errorf("%s (semver) should not allow any matches when combined with %s (version)", v1, o2)
}
if v1.Intersect(o2) != none {
t.Errorf("Intersection of %s (semver) with %s (version) should result in empty set", v1, o2)
}
if v1.Matches(o3) {
t.Errorf("%s (semver) should not match %s (branch) across types", v1, gu(o3))
}
if v1.MatchesAny(o3) {
t.Errorf("%s (semver) should not allow any matches when combined with %s (branch)", v1, gu(o3))
}
if v1.Intersect(o3) != none {
t.Errorf("Intersection of %s (semver) with %s (branch) should result in empty set", v1, gu(o3))
}
if v1.Matches(o4) {
t.Errorf("%s (semver) should not match %s (version) across types", v1, gu(o4))
}
if v1.MatchesAny(o4) {
t.Errorf("%s (semver) should not allow any matches when combined with %s (version)", v1, gu(o4))
}
if v1.Intersect(o4) != none {
t.Errorf("Intersection of %s (semver) with %s (version) should result in empty set", v1, gu(o4))
}
if !v6.Matches(o3) {
t.Errorf("%s (semver) should match %s (branch) across types due to shared rev", gu(v6), gu(o3))
}
if !v6.MatchesAny(o3) {
t.Errorf("%s (semver) should allow some matches when combined with %s (branch) across types due to shared rev", gu(v6), gu(o3))
}
if v6.Intersect(o3) != cookie {
t.Errorf("Intersection of %s (semver) with %s (branch) should return shared underlying rev", gu(v6), gu(o3))
}
if !v6.Matches(o4) {
t.Errorf("%s (semver) should match %s (branch) across types due to shared rev", gu(v6), gu(o4))
}
if !v6.MatchesAny(o4) {
t.Errorf("%s (semver) should allow some matches when combined with %s (branch) across types due to shared rev", gu(v6), gu(o4))
}
if v6.Intersect(o4) != cookie {
t.Errorf("Intersection of %s (semver) with %s (branch) should return shared underlying rev", gu(v6), gu(o4))
}
// Regression check - make sure that semVersion -> semverConstraint works
// the same as verified in the other test
c1, _ := NewSemverConstraint("=1.0.0")
if !v1.MatchesAny(c1) {
t.Errorf("%s (semver) should allow some matches - itself - when combined with an equivalent semverConstraint", gu(v1))
}
if v1.Intersect(c1) != v1 {
t.Errorf("Intersection of %s (semver) with equivalent semver constraint should return self, got %s", gu(v1), v1.Intersect(c1))
}
if !v6.MatchesAny(c1) {
t.Errorf("%s (semver pair) should allow some matches - itself - when combined with an equivalent semverConstraint", gu(v6))
}
if v6.Intersect(c1) != v6 {
t.Errorf("Intersection of %s (semver pair) with equivalent semver constraint should return self, got %s", gu(v6), v6.Intersect(c1))
}
}
// The other test is about the semverVersion, this is about semverConstraint
func TestSemverConstraintOps(t *testing.T) {
v1 := NewBranch("master").(branchVersion)
v2 := NewVersion("ab123").(plainVersion)
v3 := NewVersion("1.0.0").(semVersion)
fozzie := Revision("fozzie bear")
v4 := v1.Is(fozzie).(versionPair)
v5 := v2.Is(fozzie).(versionPair)
v6 := v3.Is(fozzie).(versionPair)
// TODO(sdboyer) we can't use the same range as below b/c semver.rangeConstraint is
// still an incomparable type
c1, err := NewSemverConstraint("=1.0.0")
if err != nil {
t.Fatalf("Failed to create constraint: %s", err)
}
if !c1.MatchesAny(any) {
t.Errorf("Semver constraints should always match the any constraint")
}
if c1.Intersect(any) != c1 {
t.Errorf("Semver constraints should always return self when intersecting the any constraint, but got %s", c1.Intersect(any))
}
if c1.MatchesAny(none) {
t.Errorf("Semver constraints should never match the none constraint")
}
if c1.Intersect(none) != none {
t.Errorf("Semver constraints should always return none when intersecting the none constraint, but got %s", c1.Intersect(none))
}
c1, err = NewSemverConstraint(">= 1.0.0")
if err != nil {
t.Fatalf("Failed to create constraint: %s", err)
}
if c1.Matches(v1) {
t.Errorf("Semver constraint should not match simple branch")
}
if c1.Matches(v2) {
t.Errorf("Semver constraint should not match simple version")
}
if !c1.Matches(v3) {
t.Errorf("Semver constraint should match a simple semver version in its range")
}
if c1.Matches(v4) {
t.Errorf("Semver constraint should not match paired branch")
}
if c1.Matches(v5) {
t.Errorf("Semver constraint should not match paired version")
}
if !c1.Matches(v6) {
t.Errorf("Semver constraint should match a paired semver version in its range")
}
if c1.MatchesAny(v1) {
t.Errorf("Semver constraint should not allow any when intersected with simple branch")
}
if c1.MatchesAny(v2) {
t.Errorf("Semver constraint should not allow any when intersected with simple version")
}
if !c1.MatchesAny(v3) {
t.Errorf("Semver constraint should allow some when intersected with a simple semver version in its range")
}
if c1.MatchesAny(v4) {
t.Errorf("Semver constraint should not allow any when intersected with paired branch")
}
if c1.MatchesAny(v5) {
t.Errorf("Semver constraint should not allow any when intersected with paired version")
}
if !c1.MatchesAny(v6) {
t.Errorf("Semver constraint should allow some when intersected with a paired semver version in its range")
}
if c1.Intersect(v1) != none {
t.Errorf("Semver constraint should return none when intersected with a simple branch")
}
if c1.Intersect(v2) != none {
t.Errorf("Semver constraint should return none when intersected with a simple version")
}
if c1.Intersect(v3) != v3 {
t.Errorf("Semver constraint should return input when intersected with a simple semver version in its range")
}
if c1.Intersect(v4) != none {
t.Errorf("Semver constraint should return none when intersected with a paired branch")
}
if c1.Intersect(v5) != none {
t.Errorf("Semver constraint should return none when intersected with a paired version")
}
if c1.Intersect(v6) != v6 {
t.Errorf("Semver constraint should return input when intersected with a paired semver version in its range")
}
}
// Test that certain types of cross-version comparisons work when they are
// expressed as a version union (but that others don't).
func TestVersionUnion(t *testing.T) {
rev := Revision("flooboofoobooo")
v1 := NewBranch("master")
v2 := NewBranch("test")
v3 := NewVersion("1.0.0").Is(rev)
v4 := NewVersion("1.0.1")
v5 := NewVersion("v2.0.5").Is(Revision("notamatch"))
uv1 := versionTypeUnion{v1, v4, rev}
uv2 := versionTypeUnion{v2, v3}
if uv1.MatchesAny(none) {
t.Errorf("Union can't match none")
}
if none.MatchesAny(uv1) {
t.Errorf("Union can't match none")
}
if !uv1.MatchesAny(any) {
t.Errorf("Union must match any")
}
if !any.MatchesAny(uv1) {
t.Errorf("Union must match any")
}
// Basic matching
if !uv1.Matches(v4) {
t.Errorf("Union should match on branch to branch")
}
if !v4.Matches(uv1) {
t.Errorf("Union should reverse-match on branch to branch")
}
if !uv1.Matches(v3) {
t.Errorf("Union should match on rev to paired rev")
}
if !v3.Matches(uv1) {
t.Errorf("Union should reverse-match on rev to paired rev")
}
if uv1.Matches(v2) {
t.Errorf("Union should not match on anything in disjoint unpaired")
}
if v2.Matches(uv1) {
t.Errorf("Union should not reverse-match on anything in disjoint unpaired")
}
if uv1.Matches(v5) {
t.Errorf("Union should not match on anything in disjoint pair")
}
if v5.Matches(uv1) {
t.Errorf("Union should not reverse-match on anything in disjoint pair")
}
if !uv1.Matches(uv2) {
t.Errorf("Union should succeed on matching comparison to other union with some overlap")
}
// MatchesAny - repeat Matches for safety, but add more, too
if !uv1.MatchesAny(v4) {
t.Errorf("Union should match on branch to branch")
}
if !v4.MatchesAny(uv1) {
t.Errorf("Union should reverse-match on branch to branch")
}
if !uv1.MatchesAny(v3) {
t.Errorf("Union should match on rev to paired rev")
}
if !v3.MatchesAny(uv1) {
t.Errorf("Union should reverse-match on rev to paired rev")
}
if uv1.MatchesAny(v2) {
t.Errorf("Union should not match on anything in disjoint unpaired")
}
if v2.MatchesAny(uv1) {
t.Errorf("Union should not reverse-match on anything in disjoint unpaired")
}
if uv1.MatchesAny(v5) {
t.Errorf("Union should not match on anything in disjoint pair")
}
if v5.MatchesAny(uv1) {
t.Errorf("Union should not reverse-match on anything in disjoint pair")
}
c1, _ := NewSemverConstraint("~1.0.0")
c2, _ := NewSemverConstraint("~2.0.0")
if !uv1.MatchesAny(c1) {
t.Errorf("Union should have some overlap due to containing 1.0.1 version")
}
if !c1.MatchesAny(uv1) {
t.Errorf("Union should have some overlap due to containing 1.0.1 version")
}
if uv1.MatchesAny(c2) {
t.Errorf("Union should have no overlap with ~2.0.0 semver range")
}
if c2.MatchesAny(uv1) {
t.Errorf("Union should have no overlap with ~2.0.0 semver range")
}
if !uv1.MatchesAny(uv2) {
t.Errorf("Union should succeed on MatchAny against other union with some overlap")
}
// Intersect - repeat all previous
if uv1.Intersect(v4) != v4 {
t.Errorf("Union intersection on contained version should return that version")
}
if v4.Intersect(uv1) != v4 {
t.Errorf("Union reverse-intersection on contained version should return that version")
}
if uv1.Intersect(v3) != rev {
t.Errorf("Union intersection on paired version w/matching rev should return rev, got %s", uv1.Intersect(v3))
}
if v3.Intersect(uv1) != rev {
t.Errorf("Union reverse-intersection on paired version w/matching rev should return rev, got %s", v3.Intersect(uv1))
}
if uv1.Intersect(v2) != none {
t.Errorf("Union should not intersect with anything in disjoint unpaired")
}
if v2.Intersect(uv1) != none {
t.Errorf("Union should not reverse-intersect with anything in disjoint unpaired")
}
if uv1.Intersect(v5) != none {
t.Errorf("Union should not intersect with anything in disjoint pair")
}
if v5.Intersect(uv1) != none {
t.Errorf("Union should not reverse-intersect with anything in disjoint pair")
}
if uv1.Intersect(c1) != v4 {
t.Errorf("Union intersecting with semver range should return 1.0.1 version, got %s", uv1.Intersect(c1))
}
if c1.Intersect(uv1) != v4 {
t.Errorf("Union reverse-intersecting with semver range should return 1.0.1 version, got %s", c1.Intersect(uv1))
}
if uv1.Intersect(c2) != none {
t.Errorf("Union intersecting with non-overlapping semver range should return none, got %s", uv1.Intersect(c2))
}
if c2.Intersect(uv1) != none {
t.Errorf("Union reverse-intersecting with non-overlapping semver range should return none, got %s", uv1.Intersect(c2))
}
if uv1.Intersect(uv2) != rev {
t.Errorf("Unions should intersect down to rev, but got %s", uv1.Intersect(uv2))
}
}
func TestVersionUnionPanicOnType(t *testing.T) {
// versionTypeUnions need to panic if Type() gets called
defer func() {
if err := recover(); err == nil {
t.Error("versionTypeUnion did not panic on Type() call")
}
}()
_ = versionTypeUnion{}.Type()
}
func TestVersionUnionPanicOnString(t *testing.T) {
// versionStringUnions need to panic if String() gets called
defer func() {
if err := recover(); err == nil {
t.Error("versionStringUnion did not panic on String() call")
}
}()
_ = versionTypeUnion{}.String()
}
func TestTypedConstraintString(t *testing.T) {
// Also tests typedVersionString(), as this nests down into that
rev := Revision("flooboofoobooo")
v1 := NewBranch("master")
v2 := NewBranch("test").Is(rev)
v3 := NewVersion("1.0.1")
v4 := NewVersion("v2.0.5")
v5 := NewVersion("2.0.5.2")
table := []struct {
in Constraint
out string
}{
{
in: anyConstraint{},
out: "any-*",
},
{
in: noneConstraint{},
out: "none-",
},
{
in: mkSVC("^1.0.0"),
out: "svc-^1.0.0",
},
{
in: v1,
out: "b-master",
},
{
in: v2,
out: "b-test-r-" + string(rev),
},
{
in: v3,
out: "sv-1.0.1",
},
{
in: v4,
out: "sv-v2.0.5",
},
{
in: v5,
out: "pv-2.0.5.2",
},
}
for _, fix := range table {
got := fix.in.typedString()
if got != fix.out {
t.Errorf("Typed string for %v (%T) was not expected %q; got %q", fix.in, fix.in, fix.out, got)
}
}
}