-
Notifications
You must be signed in to change notification settings - Fork 16
/
version_test.go
299 lines (284 loc) · 8.01 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
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
package version
import (
"reflect"
"testing"
)
func TestVersion(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool // If true, v1 less than v2
}{
// RedHat
{"7.4.629-3", "7.4.629-5", true},
{"7.4.622-1", "7.4.629-1", true},
{"6.0-4.el6.x86_64", "6.0-5.el6.x86_64", true},
{"6.0-4.el6.x86_64", "6.1-3.el6.x86_64", true},
{"7.0-4.el6.x86_64", "6.1-3.el6.x86_64", false},
// Debian
{"2:7.4.052-1ubuntu3", "2:7.4.052-1ubuntu3.1", true},
{"2:7.4.052-1ubuntu2", "2:7.4.052-1ubuntu3", true},
{"2:7.4.052-1", "2:7.4.052-1ubuntu3", true},
{"2:7.4.052", "2:7.4.052-1", true},
{"1:7.4.052", "2:7.4.052", true},
{"1:7.4.052", "7.4.052", false},
{"2:7.4.052-1ubuntu3.2", "2:7.4.052-1ubuntu3.1", false},
}
for _, tc := range cases {
v1, _ := NewVersion(tc.v1)
v2, _ := NewVersion(tc.v2)
actual := v1.LessThan(v2)
if actual != tc.expected {
t.Fatalf("v1: %v\nv2: %v\nexpected: %v\nactual: %v",
tc.v1, tc.v2, tc.expected, actual)
}
}
}
func TestNewVersion(t *testing.T) {
cases := []struct {
version string
expected Version
err bool
}{
{"1.2.3", Version{0, "1.2.3", ""}, false},
{"1:1.2.3", Version{1, "1.2.3", ""}, false},
{"A:1.2.3", Version{}, true},
{"-1:1.2.3", Version{}, true},
{"6.0-4.el6.x86_64", Version{0, "6.0", "4.el6.x86_64"}, false},
{"6.0-9ubuntu1.5", Version{0, "6.0", "9ubuntu1.5"}, false},
{"2:7.4.052-1ubuntu3.1", Version{2, "7.4.052", "1ubuntu3.1"}, false},
{"2:-1ubuntu3.1", Version{}, true},
{"2:A7.4.052-1ubuntu3.1", Version{}, true},
{"2:7.4.!052-1ubuntu3.1", Version{}, true},
{"7.4.052-!1ubuntu3.1", Version{}, true},
}
for _, tc := range cases {
actual, err := NewVersion(tc.version)
if tc.err && err == nil {
t.Fatalf("expected error for version: %s", tc.version)
} else if !tc.err && err != nil {
t.Fatalf("unexpected error for version %s: %v", tc.version, err)
} else if !tc.err && err == nil {
if actual.epoch != tc.expected.epoch {
t.Fatalf(
"version: %s\nexpected: %v\nactual: %v",
tc.version, tc.expected, actual)
}
}
}
}
func TestValid(t *testing.T) {
cases := []struct {
version string
expected bool
}{
{"1.2.3", true},
{"1:1.2.3", true},
{"A:1.2.3", false},
{"-1:1.2.3", false},
{"6.0-4.el6.x86_64", true},
{"6.0-9ubuntu1.5", true},
{"2:7.4.052-1ubuntu3.1", true},
{"2:-1ubuntu3.1", false},
{"2:A7.4.052-1ubuntu3.1", false},
{"2:7.4.!052-1ubuntu3.1", false},
{"7.4.052-!1ubuntu3.1", false},
}
for _, tc := range cases {
actual := Valid(tc.version)
if actual != tc.expected {
t.Fatalf(
"valid: %s\nexpected: %t\nactual: %t",
tc.version, tc.expected, actual)
}
}
}
func TestEqual(t *testing.T) {
cases := []struct {
v1 Version
v2 Version
expected bool // If true, v1 = v2
}{
{Version{2, "7.4.052", "1ubuntu3"}, Version{2, "7.4.052", "1ubuntu3.1"}, false},
{Version{2, "7.4.052", "1ubuntu2"}, Version{2, "7.4.052", "1ubuntu3"}, false},
{Version{2, "7.4.052", "1ubuntu3"}, Version{2, "7.4.052", "1ubuntu3"}, true},
{Version{2, "7.4.052", "1ubuntu1"}, Version{2, "7.4.052", "1"}, false},
{Version{upstreamVersion: "7.4.052"}, Version{upstreamVersion: "7.4.052"}, true},
{Version{upstreamVersion: "3.12", debianRevision: "1+deb9u1build0.16.4.1"}, Version{upstreamVersion: "3.12", debianRevision: "1+deb9u1build0.16.04.1"}, true},
}
for _, tc := range cases {
actual := tc.v1.Equal(tc.v2)
if actual != tc.expected {
t.Fatalf("v1: %v\nv2: %v\nexpected: %v\nactual: %v",
tc.v1, tc.v2, tc.expected, actual)
}
}
}
func TestGreaterThan(t *testing.T) {
cases := []struct {
v1 Version
v2 Version
expected bool // If true, v1 greater than v2
}{
{Version{2, "7.4.052", "1ubuntu3"}, Version{2, "7.4.052", "1ubuntu3.1"}, false},
{Version{2, "7.4.052", "1ubuntu2"}, Version{2, "7.4.052", "1ubuntu3"}, false},
{Version{2, "7.4.052", "1ubuntu1"}, Version{2, "7.4.052", "1"}, true},
{Version{2, "6.0", "9ubuntu1.4"}, Version{2, "6.0", "9ubuntu1.5"}, false},
{
Version{upstreamVersion: "7.4.052"},
Version{upstreamVersion: "7.4.052", debianRevision: "1ubuntu2"},
false,
},
{
Version{upstreamVersion: "7.4.052"},
Version{epoch: 2, upstreamVersion: "7.4.052"},
false,
},
}
for _, tc := range cases {
actual := tc.v1.GreaterThan(tc.v2)
if actual != tc.expected {
t.Fatalf("v1: %v\nv2: %v\nexpected: %v\nactual: %v",
tc.v1, tc.v2, tc.expected, actual)
}
}
}
func TestLessThan(t *testing.T) {
cases := []struct {
v1 Version
v2 Version
expected bool // If true, v1 less than v2
}{
{Version{2, "7.4.052", "1ubuntu3"}, Version{2, "7.4.052", "1ubuntu3.1"}, true},
{Version{2, "7.4.052", "1ubuntu2"}, Version{2, "7.4.052", "1ubuntu3"}, true},
{Version{2, "7.4.052", "1ubuntu1"}, Version{2, "7.4.052", "1"}, false},
{Version{2, "6.0", "9ubuntu1.4"}, Version{2, "6.0", "9ubuntu1.5"}, true},
{
Version{upstreamVersion: "7.4.052"},
Version{upstreamVersion: "7.4.052", debianRevision: "1ubuntu2"},
true,
},
{
Version{upstreamVersion: "1.9.1", debianRevision: "2"},
Version{upstreamVersion: "1.16", debianRevision: "1+deb8u1"},
true,
},
{
Version{upstreamVersion: "1.9", debianRevision: "2"},
Version{upstreamVersion: "1.9.1", debianRevision: "1+deb8u1"},
true,
},
{
Version{upstreamVersion: "7.4.052"},
Version{epoch: 2, upstreamVersion: "7.4.052"},
true,
},
}
for _, tc := range cases {
actual := tc.v1.LessThan(tc.v2)
if actual != tc.expected {
t.Fatalf("v1: %v\nv2: %v\nexpected: %v\nactual: %v",
tc.v1, tc.v2, tc.expected, actual)
}
}
}
func TestString(t *testing.T) {
cases := []struct {
v Version
expected string
}{
{Version{2, "7.4.052", "1ubuntu3"}, "2:7.4.052-1ubuntu3"},
{Version{2, "7.4.052", "1"}, "2:7.4.052-1"},
{Version{0, "7.4.052", "1"}, "7.4.052-1"},
{Version{upstreamVersion: "7.4.052", debianRevision: "1"}, "7.4.052-1"},
{Version{epoch: 1, upstreamVersion: "7.4.052"}, "1:7.4.052"},
{Version{upstreamVersion: "7.4.052"}, "7.4.052"},
}
for _, tc := range cases {
actual := tc.v.String()
if actual != tc.expected {
t.Fatalf("v: %v\n\nexpected: %v\nactual: %v",
tc.v, tc.expected, actual)
}
}
}
func TestCompare(t *testing.T) {
cases := []struct {
v1 string
v2 string
negative bool // If true, v1 less than v2
}{
{"6.4.052", "7.4.052", true},
{"6.4.052", "6.5.052", true},
{"6.4.052", "6.4.053", true},
{"1ubuntu1", "1ubuntu3.1", true},
{"1", "1ubuntu1", true},
{"7.4.027", "7.4.052", true},
}
for _, tc := range cases {
ret := compare(tc.v1, tc.v2)
if tc.negative && ret > 0 {
t.Fatalf("v1: %s\nv2: %s\nexpected: %v\nactual: %v",
tc.v1, tc.v2, tc.negative, ret < 0)
}
}
}
func TestExtract(t *testing.T) {
cases := []struct {
version string
expectedNum defaultNumSlice
expectedStr defaultStringSlice
}{
{"1.2.3", []int{1, 2, 3}, defaultStringSlice{".", "."}},
{"12.+34.~45", []int{12, 34, 45}, []string{".+", ".~"}},
{".+-:~123.45", []int{123, 45}, []string{".+-:~", "."}},
}
for _, tc := range cases {
n, s := extract(tc.version)
if !reflect.DeepEqual(n, tc.expectedNum) {
t.Fatalf("version: %s\nexpected: %v\nactual: %v",
tc.version, tc.expectedNum, n)
}
if !reflect.DeepEqual(s, tc.expectedStr) {
t.Fatalf("version: %s\nexpected: %v\nactual: %v",
tc.version, tc.expectedStr, s)
}
}
}
func TestOrder(t *testing.T) {
cases := []struct {
r rune
expected int
}{
{'A', 65},
{'~', -1},
{'\a', 263},
}
for _, tc := range cases {
o := order(tc.r)
if o != tc.expected {
t.Fatalf("rune: %c\n\nexpected: %d\nactual: %d",
tc.r, tc.expected, o)
}
}
}
func TestCompareString(t *testing.T) {
cases := []struct {
s1 string
s2 string
negative bool // If true, s1 less than s2
}{
{"~~", "~~a", true},
{"~~a", "~", true},
{"~", "", true},
{"", "a", true},
{"a", ".", true},
}
for _, tc := range cases {
ret := compareString(tc.s1, tc.s2)
if tc.negative && ret > 0 {
t.Fatalf("s1: %s\ns2: %s\nexpected: %v\nactual: %v",
tc.s1, tc.s2, tc.negative, ret < 0)
}
}
}