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 22
/
version_test.go
185 lines (168 loc) · 4.69 KB
/
version_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
package gps
import "testing"
func TestVersionSorts(t *testing.T) {
rev := Revision("flooboofoobooo")
v1 := NewBranch("master").Is(rev)
v2 := NewBranch("test").Is(rev)
v3 := NewVersion("1.0.0").Is(rev)
v4 := NewVersion("1.0.1").Is(rev)
v5 := NewVersion("v2.0.5").Is(rev)
v6 := NewVersion("2.0.5.2").Is(rev)
v7 := newDefaultBranch("unwrapped").Is(rev)
v8 := NewVersion("20.0.5.2").Is(rev)
v9 := NewVersion("v1.5.5-beta.4").Is(rev)
v10 := NewVersion("v3.0.1-alpha.1").Is(rev)
start := []Version{
v1,
v2,
v3,
v4,
v5,
v6,
v7,
v8,
v9,
v10,
rev,
}
down := make([]Version, len(start))
copy(down, start)
up := make([]Version, len(start))
copy(up, start)
edown := []Version{
v3, v4, v5, // semvers
v9, v10, // prerelease semver
v7, v1, v2, // floating/branches
v6, v8, // plain versions
rev, // revs
}
eup := []Version{
v5, v4, v3, // semvers
v10, v9, // prerelease semver
v7, v1, v2, // floating/branches
v6, v8, // plain versions
rev, // revs
}
SortForUpgrade(up)
var wrong []int
for k, v := range up {
if eup[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", eup[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
}
SortForDowngrade(down)
wrong = wrong[:0]
for k, v := range down {
if edown[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", edown[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
}
// Now make sure we sort back the other way correctly...just because
SortForUpgrade(down)
wrong = wrong[:0]
for k, v := range down {
if eup[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", eup[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
}
// Now make sure we sort back the other way correctly...just because
SortForDowngrade(up)
wrong = wrong[:0]
for k, v := range up {
if edown[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", edown[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Fatalf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
}
///////////
// Repeat for PairedVersion slices & sorts
pdown, pup := make([]PairedVersion, 0, len(start)), make([]PairedVersion, 0, len(start))
for _, v := range start {
if _, ok := v.(Revision); ok {
continue
}
pdown = append(pdown, v.(PairedVersion))
pup = append(pup, v.(PairedVersion))
}
pedown, peup := make([]PairedVersion, 0, len(edown)), make([]PairedVersion, 0, len(eup))
for _, v := range edown {
if _, ok := v.(Revision); ok {
continue
}
pedown = append(pedown, v.(PairedVersion))
}
for _, v := range eup {
if _, ok := v.(Revision); ok {
continue
}
peup = append(peup, v.(PairedVersion))
}
SortPairedForUpgrade(pup)
for k, v := range pup {
if peup[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", peup[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
}
SortPairedForDowngrade(pdown)
wrong = wrong[:0]
for k, v := range pdown {
if pedown[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", pedown[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
}
// Now make sure we sort back the other way correctly...just because
SortPairedForUpgrade(pdown)
wrong = wrong[:0]
for k, v := range pdown {
if peup[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", peup[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
}
// Now make sure we sort back the other way correctly...just because
SortPairedForDowngrade(pup)
wrong = wrong[:0]
for k, v := range pup {
if pedown[k] != v {
wrong = append(wrong, k)
t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", pedown[k], k, v)
}
}
if len(wrong) > 0 {
// Just helps with readability a bit
t.Errorf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
}
}