Debian Codegraph @

pkg/semver/constraint_test.go

raw symbols SCIP
1 package semverscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/"
5 "testingscip-go gomod github.com/golang/go/src go1.15 testing/"
6
7 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/"
8 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/"
9 )
10
11 func TestNewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/TestNewConstraints().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
12 tests := []struct {
13 input string
14 wantErr bool
15 }{
16 {">= 1.1", false},
17 {">40.50.60, < 50.70", false},
18 {"2.0", false},
19 {"2.3.5-20161202202307-sha.e8fc5e5", false},
20 {">= bar", true},
21 {"BAR >= 1.2.3", true},
22
23 // Test with commas separating AND
24 {">= 1.2.3, < 2.0", false},
25 {">= 1.2.3, < 2.0 || => 3.0, < 4", false},
26
27 // The 3 - 4 should be broken into 2 by the range rewriting
28 //{"3 - 4 || => 3.0, < 4", false},
29 }
30 for _, tt := range tests {
31 t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(tt.input, func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
32 _, err := NewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/NewConstraints().(tt.input)
33 if tt.wantErr {
34 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.NotNilscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/NotNil().(t, err)
35 } else {
36 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/NoError().(t, err)
37 }
38 })
39 }
40 }
41
42 func TestConstraint_Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/TestConstraint_Check().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
43 tests := []struct {
44 constraint string
45 version string
46 want bool
47 }{
48 // Equal: =
49 {"=2.0.0", "1.2.3", false},
50 {"=2.0.0", "2.0.0", true},
51 {"=2.0", "1.2.3", false},
52 {"=2.0", "2.0.0", true},
53 {"=2.0", "2.0.1", true},
54 {"=0", "1.0.0", false},
55
56 // Equal: ==
57 {"== 2.0.0", "1.2.3", false},
58 {"==2.0.0", "2.0.0", true},
59 {"== 2.0", "1.2.3", false},
60 {"==2.0", "2.0.0", true},
61 {"== 2.0", "2.0.1", true},
62 {"==0", "1.0.0", false},
63
64 // Equal without "="
65 {"4.1", "4.1.0", true},
66 {"2", "1.0.0", false},
67 {"2", "3.4.5", false},
68 {"2", "2.1.1", true},
69 {"2.1", "2.1.1", true},
70 {"2.1", "2.2.1", false},
71
72 // Not equal
73 {"!=4.1.0", "4.1.0", false},
74 {"!=4.1.0", "4.1.1", true},
75 {"!=4.1", "4.1.0", false},
76 {"!=4.1", "4.1.1", false},
77 {"!=4.1", "5.1.0-alpha.1", false},
78 {"!=4.1-alpha", "4.1.0", false},
79 {"!=4.1", "5.1.0", true},
80
81 // Less than
82 {"<11", "0.1.0", true},
83 {"<11", "11.1.0", false},
84 {"<1.1", "0.1.0", true},
85 {"<1.1", "1.1.0", false},
86 {"<1.1", "1.1.1", false},
87 {"<0", "0.0.0-alpha", false},
88 {"<0.0.0-z", "0.0.0-alpha", true},
89
90 // Less than or equal
91 {"<=11", "1.2.3", true},
92 {"<=11", "12.2.3", false},
93 {"<=11", "11.2.3", true},
94 {"<=1.1", "1.2.3", false},
95 {"<=1.1", "0.1.0", true},
96 {"<=1.1", "1.1.0", true},
97 {"<=1.1", "1.1.1", true},
98
99 // Greater than
100 {">1.1", "4.1.0", true},
101 {">1.1", "1.1.0", false},
102 {">0", "0.0.0", false},
103 {">0", "1.0.0", true},
104 {">0", "0.0.1-alpha", false},
105 {">0.0", "0.0.1-alpha", false},
106 {">0", "0.0.0-alpha", false},
107 {">0.0.0-0", "0.0.0-alpha", true},
108 {">0.0.0-0", "0.0.0-alpha", true},
109 {">1.2.3-alpha.1", "1.2.3-alpha.2", true},
110 {">1.2.3-alpha.1", "1.3.3-alpha.2", true},
111 {">11", "11.1.0", false},
112 {">11.1", "11.1.0", false},
113 {">11.1", "11.1.1", false},
114 {">11.1", "11.2.1", true},
115
116 // Greater than or equal
117 {">=11", "11.1.2", true},
118 {">=11.1", "11.1.2", true},
119 {">=11.1", "11.0.2", false},
120 {">=1.1", "4.1.0", true},
121 {">=1.1", "1.1.0", true},
122 {">=1.1", "0.0.9", false},
123 {">=0", "0.0.1-alpha", false},
124 {">=0.0", "0.0.1-alpha", false},
125 {">=0", "0.0.0-alpha", false},
126 {">=0.0.0-0", "0.0.0-alpha", true},
127 {">=0.0.0-0", "1.2.3", true},
128 {">=0.0.0-0", "3.4.5-beta.1", true},
129 {">=0", "0.0.0", true},
130
131 // Asterisk
132 {"*", "1.0.0", true},
133 {"*", "4.5.6", true},
134 {"*", "1.2.3-alpha.1", false},
135 {"2.*", "1.0.0", false},
136 {"2.*", "3.4.5", false},
137 {"2.*", "2.1.1", true},
138 {"2.1.*", "2.1.1", true},
139 {"2.1.*", "2.2.1", false},
140
141 // Empty: an empty string is treated as * or wild card
142 {"", "1.0.0", true},
143 {" ", "1.0.0", true},
144 {" ", "1.0.0", true},
145 {"", "4.5.6", true},
146 {"", "1.2.3-alpha.1", false},
147
148 // Tilde
149 {"~1.2.3", "1.2.4", true},
150 {"~1.2.3", "1.3.4", false},
151 {"~1.2", "1.2.4", true},
152 {"~1.2", "1.3.4", false},
153 {"~1", "1.2.4", true},
154 {"~1", "2.3.4", false},
155 {"~0.2.3", "0.2.5", true},
156 {"~0.2.3", "0.3.5", false},
157 {"~1.2.3-beta.2", "1.2.3-beta.4", true},
158
159 // Caret
160 // https://docs.npmjs.com/cli/v6/using-npm/semver#caret-ranges-123-025-004
161 {"^1.2.3", "1.8.9", true},
162 {"^1.2.3", "2.8.9", false},
163 {"^1.2.3", "1.2.1", false},
164 {"^1.1.0", "2.1.0", false},
165 {"^1.2.0", "2.2.1", false},
166 {"^1.2.0", "1.2.1-alpha.1", false},
167 {"^1.2.0-alpha.2", "1.2.0-alpha.1", false},
168 {"^1.2", "1.8.9", true},
169 {"^1.2", "2.8.9", false},
170 {"^1", "1.8.9", true},
171 {"^1", "2.8.9", false},
172 {"^0.2.3", "0.2.5", true},
173 {"^0.2.3", "0.5.6", false},
174 {"^0.2", "0.2.5", true},
175 {"^0.2", "0.5.6", false},
176 {"^0.0.3", "0.0.3", true},
177 {"^0.0.3", "0.0.4", false},
178 {"^0.0", "0.0.3", true},
179 {"^0.0", "0.1.4", false},
180 {"^0.0", "1.0.4", false},
181 {"^0", "0.2.3", true},
182 {"^0", "1.1.4", false},
183 {"^0.2.3-beta.2", "0.2.3-beta.4", true},
184
185 // This next test is a case that is different from npm/js semver handling.
186 // Their prereleases are only range scoped to patch releases. This is
187 // technically not following semver as docs note. In our case we are
188 // following semver.
189 {"^1.2.0-alpha.0", "1.2.1-alpha.1", true},
190 {"^1.2.0-alpha.0", "1.2.1-alpha.0", true},
191 {"^0.2.3-beta.2", "0.2.4-beta.2", true},
192 {"^0.2.3-beta.2", "0.3.4-beta.2", false},
193 {"^0.2.3-beta.2", "0.2.3-beta.2", true},
194
195 // Not supported: always false
196 {">0.0-0", "0.0.1-alpha", false},
197 {">0-0", "0.0.1-alpha", false},
198 {">0-0", "0.0.0-alpha", false},
199 {">=0-0", "0.0.1-alpha", false},
200 {">=0.0-0", "0.0.1-alpha", false},
201 {">=0-0", "0.0.0-alpha", false},
202 {"<0-z", "0.0.0-alpha", false},
203 }
204
205 for _, tc := range tests {
206 t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("%s vs %s", tc.constraint, tc.version), func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
207 c, err := NewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/NewConstraints().(tc.constraint)
208 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
209
210 v, err := Parsescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Parse().(tc.version)
211 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
212
213 got := c.Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Constraints#Check().(v)
214 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/Equal().(t, tc.want, got)
215 })
216 }
217 }
218
219 func TestConstraint_CheckWithPreReleasescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/TestConstraint_CheckWithPreRelease().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
220 tests := []struct {
221 constraint string
222 version string
223 want bool
224 }{
225 // Equal
226 {"=2.0.0", "1.2.3", false},
227 {"=2.0.0", "2.0.0", true},
228 {"=2.0", "1.2.3", false},
229 {"=2.0", "2.0.0", true},
230 {"=2.0", "2.0.1", true},
231 {"4.1", "4.1.0", true},
232 {"== 2.0.0", "1.2.3", false},
233 {"==2.0.0", "2.0.0", true},
234 {"== 2.0", "2.0.0", true},
235 {"==2.0", "2.0.1", true},
236
237 // Not equal
238 {"!=4.1.0", "4.1.0", false},
239 {"!=4.1.0", "4.1.1", true},
240 {"!=4.1", "4.1.0", false},
241 {"!=4.1", "4.1.1", false},
242 {"!=4.1", "5.1.0", true},
243
244 // Less than
245 {"<11", "0.1.0", true},
246 {"<11", "11.1.0", false},
247 {"<1.1", "0.1.0", true},
248 {"<1.1", "1.1.0", false},
249 {"<1.1", "1.1.1", false},
250
251 // Less than or equal
252 {"<=11", "1.2.3", true},
253 {"<=11", "12.2.3", false},
254 {"<=11", "11.2.3", true},
255 {"<=1.1", "1.2.3", false},
256 {"<=1.1", "0.1.0", true},
257 {"<=1.1", "1.1.0", true},
258 {"<=1.1", "1.1.1", true},
259
260 // Greater than
261 {">1.1", "4.1.0", true},
262 {">1.1", "1.1.0", false},
263 {">0", "0.0.0", false},
264 {">0", "1.0.0", true},
265 {">11", "11.1.0", false},
266 {">11.1", "11.1.0", false},
267 {">11.1", "11.1.1", false},
268 {">11.1", "11.2.1", true},
269
270 // Greater than or equal
271 {">=11", "11.1.2", true},
272 {">=11.1", "11.1.2", true},
273 {">=11.1", "11.0.2", false},
274 {">=1.1", "4.1.0", true},
275 {">=1.1", "1.1.0", true},
276 {">=1.1", "0.0.9", false},
277 {">=0", "0.0.0", true},
278 {"=0", "1.0.0", false},
279
280 // Asterisk
281 {"*", "1.0.0", true},
282 {"*", "4.5.6", true},
283 {"2.*", "1.0.0", false},
284 {"2.*", "3.4.5", false},
285 {"2.*", "2.1.1", true},
286 {"2.1.*", "2.1.1", true},
287 {"2.1.*", "2.2.1", false},
288
289 // Empty
290 {"", "1.0.0", true}, // An empty string is treated as * or wild card
291 {"", "4.5.6", true},
292 {"2", "1.0.0", false},
293 {"2", "3.4.5", false},
294 {"2", "2.1.1", true},
295 {"2.1", "2.1.1", true},
296 {"2.1", "2.2.1", false},
297
298 // Tilde
299 {"~1.2.3", "1.2.4", true},
300 {"~1.2.3", "1.3.4", false},
301 {"~1.2", "1.2.4", true},
302 {"~1.2", "1.3.4", false},
303 {"~1", "1.2.4", true},
304 {"~1", "2.3.4", false},
305 {"~0.2.3", "0.2.5", true},
306 {"~0.2.3", "0.3.5", false},
307 {"~1.2.3-beta.2", "1.2.3-beta.4", true},
308 {"^1.2.3", "1.8.9", true},
309 {"^1.2.3", "2.8.9", false},
310 {"^1.2.3", "1.2.1", false},
311 {"^1.1.0", "2.1.0", false},
312 {"^1.2.0", "2.2.1", false},
313 {"^1.2", "1.8.9", true},
314 {"^1.2", "2.8.9", false},
315 {"^1", "1.8.9", true},
316 {"^1", "2.8.9", false},
317 {"^0.2.3", "0.2.5", true},
318 {"^0.2.3", "0.5.6", false},
319 {"^0.2", "0.2.5", true},
320 {"^0.2", "0.5.6", false},
321 {"^0.0.3", "0.0.3", true},
322 {"^0.0.3", "0.0.4", false},
323 {"^0.0", "0.0.3", true},
324 {"^0.0", "0.1.4", false},
325 {"^0.0", "1.0.4", false},
326 {"^0", "0.2.3", true},
327 {"^0", "1.1.4", false},
328
329 // pre-release: Not equal
330 {"!=4.1", "5.1.0-alpha.1", true},
331 {"!=4.1-alpha", "4.1.0", false},
332
333 // pre-release: Greater than
334 {">0", "0.0.1-alpha", false},
335 {">0.0", "0.0.1-alpha", false},
336 {">0-0", "0.0.1-alpha", false},
337 {">0.0-0", "0.0.1-alpha", false},
338 {">0", "0.0.0-alpha", false},
339 {">0-0", "0.0.0-alpha", false},
340 {">0.0.0-0", "0.0.0-alpha", true},
341 {">1.2.3-alpha.1", "1.2.3-alpha.2", true},
342 {">1.2.3-alpha.1", "1.3.3-alpha.2", true},
343
344 // pre-release: Less than
345 {"<0", "0.0.0-alpha", true},
346 {"<0-z", "0.0.0-alpha", false},
347
348 // pre-release: Greater than or equal
349 {">=0", "0.0.1-alpha", true},
350 {">=0.0", "0.0.1-alpha", true},
351 {">=0-0", "0.0.1-alpha", false},
352 {">=0.0-0", "0.0.1-alpha", false},
353 {">=0", "0.0.0-alpha", true}, // ">=0.*.*-*"
354 {">=0-0", "0.0.0-alpha", false},
355 {">=0.0.0", "0.0.0-alpha", false},
356 {">=0.0.0-0", "0.0.0-alpha", true},
357 {">=0.0.0-0", "1.2.3", true},
358 {">=0.0.0-0", "3.4.5-beta.1", true},
359
360 // pre-release: Asterisk
361 {"*", "1.2.3-alpha.1", true},
362
363 // pre-release: Empty
364 {"", "1.2.3-alpha.1", true},
365
366 // pre-release: Tilde
367 {"~1.2.3-beta.2", "1.2.3-beta.4", true},
368 {"~1.2.3-beta.2", "1.2.4-beta.2", true},
369 {"~1.2.3-beta.2", "1.3.4-beta.2", false},
370
371 // pre-release: Caret
372 {"^1.2.0", "1.2.1-alpha.1", true},
373 {"^1.2.0-alpha.0", "1.2.1-alpha.1", true},
374 {"^1.2.0-alpha.0", "1.2.1-alpha.0", true},
375 {"^1.2.0-alpha.2", "1.2.0-alpha.1", false},
376 {"^0.2.3-beta.2", "0.2.3-beta.4", true},
377 {"^0.2.3-beta.2", "0.2.4-beta.2", true},
378 {"^0.2.3-beta.2", "0.3.4-beta.2", false},
379 {"^0.2.3-beta.2", "0.2.3-beta.2", true},
380 }
381
382 for _, tc := range tests {
383 t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("%s vs %s", tc.constraint, tc.version), func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
384 c, err := NewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/NewConstraints().(tc.constraint, WithPreReleasescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/WithPreRelease#(true))
385 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
386
387 v, err := Parsescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Parse().(tc.version)
388 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
389
390 got := c.Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Constraints#Check().(v)
391 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/Equal().(t, tc.want, got)
392 })
393 }
394 }
395
396 func TestConstraint_CheckWithPreReleaseAndZeroPaddingscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/TestConstraint_CheckWithPreReleaseAndZeroPadding().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
397 tests := []struct {
398 constraint string
399 version string
400 want bool
401 }{
402 // Equal
403 {"=2.0.0", "1.2.3", false},
404 {"=2.0.0", "2.0.0", true},
405
406 // Not equal
407 {"!=4.1.0", "4.1.0", false},
408 {"!=4.1.0", "4.1.1", true},
409
410 // Less than
411 {"<0.0.5", "0.1.0", false},
412 {"<1.0.0", "0.1.0", true},
413
414 // Less than or equal
415 {"<=0.2.3", "1.2.3", false},
416 {"<=1.2.3", "1.2.3", true},
417
418 // Greater than
419 {">5.0.0", "4.1.0", false},
420 {">4.0.0", "4.1.0", true},
421
422 // Greater than or equal
423 {">=11.1.3", "11.1.2", false},
424 {">=11.1.2", "11.1.2", true},
425
426 // Asterisk
427 {"*", "1.0.0", true},
428 {"*", "4.5.6", true},
429 {"2.*", "1.0.0", false},
430 {"2.*", "3.4.5", false},
431 {"2.*", "2.1.1", true},
432 {"2.1.*", "2.1.1", true},
433 {"2.1.*", "2.2.1", false},
434
435 // Empty
436 {"", "1.0.0", true}, // An empty string is treated as * or wild card
437 {"", "4.5.6", true},
438 {"2", "1.0.0", false},
439 {"2", "3.4.5", false},
440 {"2", "2.1.1", false}, // different
441 {"2.1", "2.1.1", false}, // different
442 {"2.1", "2.2.1", false},
443
444 // Tilde
445 {"~1.2.3", "1.2.4", true},
446 {"~1.2.3", "1.3.4", false},
447 {"~1.2", "1.2.4", true},
448 {"~1.2", "1.3.4", false},
449 {"~1", "1.2.4", true},
450 {"~1", "2.3.4", false},
451 {"~0.2.3", "0.2.5", true},
452 {"~0.2.3", "0.3.5", false},
453 {"~1.2.3-beta.2", "1.2.3-beta.4", true},
454
455 // Caret
456 {"^1.2.3", "1.8.9", true},
457 {"^1.2.3", "2.8.9", false},
458 {"^1.2.3", "1.2.1", false},
459 {"^1.1.0", "2.1.0", false},
460 {"^1.2.0", "2.2.1", false},
461 {"^1.2", "1.8.9", true},
462 {"^1.2", "2.8.9", false},
463 {"^1", "1.8.9", true},
464 {"^1", "2.8.9", false},
465 {"^0.2.3", "0.2.5", true},
466 {"^0.2.3", "0.5.6", false},
467 {"^0.2", "0.2.5", true},
468 {"^0.2", "0.5.6", false},
469 {"^0.0.3", "0.0.3", true},
470 {"^0.0.3", "0.0.4", false},
471 {"^0.0", "0.0.3", true},
472 {"^0.0", "0.1.4", false},
473 {"^0.0", "1.0.4", false},
474 {"^0", "0.2.3", true},
475 {"^0", "1.1.4", false},
476
477 // pre-release: Equal
478 {"=4.1", "4.1.0-alpha.1", false},
479 {"=4.1-alpha", "4.1.0-alpha", true},
480 {"== 4.1", "4.1.0-alpha.1", false},
481 {"==4.1-alpha", "4.1.0-alpha", true},
482
483 // pre-release: Not equal
484 {"!=4.1", "5.1.0-alpha.1", true},
485 {"!=4.1-alpha", "4.1.0", true},
486
487 // pre-release: Greater than
488 {">0", "0.0.1-alpha", true}, // different
489 {">0.0", "0.0.1-alpha", true}, // different
490 {">0-0", "0.0.1-alpha", true}, // different
491 {">0.0-0", "0.0.1-alpha", true}, // different
492 {">0", "0.0.0-alpha", false},
493 {">0-0", "0.0.0-alpha", true}, // different
494 {">0.0.0-0", "0.0.0-alpha", true},
495 {">1.2.3-alpha.1", "1.2.3-alpha.2", true},
496 {">1.2.3-alpha.1", "1.3.3-alpha.2", true},
497
498 // pre-release: Less than
499 {"<0", "0.0.0-alpha", true}, // different
500 {"<0-z", "0.0.0-alpha", true}, // different
501 {"<0", "1.0.0-alpha", false},
502 {"<1", "1.0.0-alpha", true}, // different
503
504 // pre-release: Greater than or equal
505 {">=0", "0.0.1-alpha", true},
506 {">=0.0", "0.0.1-alpha", true},
507 {">=0-0", "0.0.1-alpha", true},
508 {">=0.0-0", "0.0.1-alpha", true},
509 {">=0", "0.0.0-alpha", false},
510 {">=0-0", "0.0.0-alpha", true},
511 {">=0.0.0-0", "0.0.0-alpha", true},
512 {">=0.0.0-0", "1.2.3", true},
513 {">=0.0.0-0", "3.4.5-beta.1", true},
514
515 // pre-release: Asterisk
516 {"*", "1.2.3-alpha.1", true},
517
518 // pre-release: Empty
519 {"", "1.2.3-alpha.1", true},
520
521 // pre-release: Tilde
522 {"~1.2.3-beta.2", "1.2.3-beta.4", true},
523 {"~1.2.3-beta.2", "1.2.4-beta.2", true},
524 {"~1.2.3-beta.2", "1.3.4-beta.2", false},
525
526 // pre-release: Caret
527 {"^1.2.0", "1.2.1-alpha.1", true},
528 {"^1.2.0-alpha.0", "1.2.1-alpha.1", true},
529 {"^1.2.0-alpha.0", "1.2.1-alpha.0", true},
530 {"^1.2.0-alpha.2", "1.2.0-alpha.1", false},
531 {"^0.2.3-beta.2", "0.2.3-beta.4", true},
532 {"^0.2.3-beta.2", "0.2.4-beta.2", true},
533 {"^0.2.3-beta.2", "0.3.4-beta.2", false},
534 {"^0.2.3-beta.2", "0.2.3-beta.2", true},
535
536 // missing patch: Equal
537 {"=2.0", "1.2.3", false},
538 {"=2.0", "2.0.0", true},
539 {"=2.0", "2.0.1", false}, // different
540 {"4.1", "4.1.0", true},
541 {"=0", "1.0.0", false},
542
543 // missing patch: Not equal
544 {"!=4.1", "4.1.0", false},
545 {"!=4.1", "4.1.1", true},
546 {"!=4.1", "5.1.0", true},
547
548 // missing minor/patch: Less than
549 {"<11", "0.1.0", true},
550 {"<11", "11.1.0", false},
551 {"<1.1", "0.1.0", true},
552 {"<1.1", "1.1.0", false},
553 {"<1.1", "1.1.1", false},
554
555 // missing minor/patch: Less than or equal
556 {"<=11", "1.2.3", true},
557 {"<=11", "12.2.3", false},
558 {"<=11", "11.2.3", false}, // different
559 {"<=1.1", "1.2.3", false},
560 {"<=1.1", "0.1.0", true},
561 {"<=1.1", "1.1.0", true},
562 {"<=1.1", "1.1.1", false}, // different
563
564 // missing minor/patch: Greater than
565 {">1.1", "4.1.0", true},
566 {">1.1", "1.1.0", false},
567 {">0", "0.0.0", false},
568 {">0", "1.0.0", true},
569 {">11", "11.1.0", true}, // different
570 {">11.1", "11.1.0", false},
571 {">11.1", "11.1.1", true}, // different
572 {">11.1", "11.2.1", true},
573
574 // missing minor/patch: Greater than or equal
575 {">=11", "11.1.2", true},
576 {">=11.1", "11.1.2", true},
577 {">=11.1", "11.0.2", false},
578 {">=1.1", "4.1.0", true},
579 {">=1.1", "1.1.0", true},
580 {">=1.1", "0.0.9", false},
581 {">=0", "0.0.0", true},
582 }
583
584 for _, tc := range tests {
585 t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("%s vs %s", tc.constraint, tc.version), func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
586 c, err := NewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/NewConstraints().(tc.constraint, WithPreReleasescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/WithPreRelease#(true), WithZeroPaddingscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/WithZeroPadding#(true))
587 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
588
589 v, err := Parsescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Parse().(tc.version)
590 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
591
592 got := c.Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Constraints#Check().(v)
593 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/Equal().(t, tc.want, got)
594 })
595 }
596 }
597
598 func TestConstraints_Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/TestConstraints_Check().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
599 tests := []struct {
600 constraint string
601 version string
602 want bool
603 }{
604 {"*", "1.2.3", true},
605 {"~0.0.0", "1.2.3", false},
606 {"0.x.x", "1.2.3", false},
607 {"0.0.x", "1.2.3", false},
608 {"0.0.0", "1.2.3", false},
609 {"*", "1.2.3", true},
610 {"^0.0.0", "1.2.3", false},
611 {"= 2.0", "1.2.3", false},
612 {"= 2.0", "2.0.0", true},
613 {"4.1", "4.1.0", true},
614 {"4.1.x", "4.1.3", true},
615 {"1.x", "1.4.0", true},
616 {"!=4.1", "4.1.0", false},
617 {"!=4.1-alpha", "4.1.0-alpha", false},
618 {"!=4.1-alpha", "4.1.1-alpha", false},
619 {"!=4.1-alpha", "4.1.0", false},
620 {"!=4.1", "5.1.0", true},
621 {"!=4.x", "5.1.0", true},
622 {"!=4.x", "4.1.0", false},
623 {"!=4.1.x", "4.2.0", true},
624 {"!=4.2.x", "4.2.3", false},
625 {">1.1", "4.1.0", true},
626 {">1.1", "1.1.0", false},
627 {"<1.1", "0.1.0", true},
628 {"<1.1", "1.1.0", false},
629 {"<1.1", "1.1.1", false},
630 {"<1.x", "1.1.1", false},
631 {"<1.x", "0.1.1", true},
632 {"<1.x", "2.0.0", false},
633 {"<1.1.x", "1.2.1", false},
634 {"<1.1.x", "1.1.500", false},
635 {"<1.1.x", "1.0.500", true},
636 {"<1.2.x", "1.1.1", true},
637 {">=1.1", "4.1.0", true},
638 {">=1.1", "4.1.0-beta", false},
639 {">=1.1", "1.1.0", true},
640 {">=1.1", "0.0.9", false},
641 {"<=1.1", "0.1.0", true},
642 {"<=1.1", "0.1.0-alpha", false},
643 {"<=1.1-a", "0.1.0-alpha", false},
644 {"<=1.1", "1.1.0", true},
645 {"<=1.x", "1.1.0", true},
646 {"<=2.x", "3.0.0", false},
647 {"<=1.1", "1.1.1", true},
648 {"<=1.1.x", "1.2.500", false},
649 {"<=4.5", "3.4.0", true},
650 {"<=4.5", "3.7.0", true},
651 {"<=4.5", "4.6.3", false},
652 {"1.1-3", "4.3.2", false},
653 {"^1.1", "1.1.1", true},
654 {"^1.1", "4.3.2", false},
655 {"^1.x", "1.1.1", true},
656 {"^2.x", "1.1.1", false},
657 {"^1.x", "2.1.1", false},
658 {"^1.x", "1.1.1-beta1", false},
659 {"^1.1.2-alpha", "1.2.1-beta1", true},
660 {"^1.2.x-alpha", "1.1.1-beta1", false},
661 {"~*", "2.1.1", true},
662 {"~1", "2.1.1", false},
663 {"~1", "1.3.5", true},
664 {"~1", "1.4.0", true},
665 {"~1.x", "2.1.1", false},
666 {"~1.x", "1.3.5", true},
667 {"~1.x", "1.4.0", true},
668 {"~1.1", "1.1.1", true},
669 {"~1.1", "1.1.1-alpha", false},
670 {"~1.1-alpha", "1.1.1-beta", false},
671 {"~1.1.1-beta", "1.1.1-alpha", false},
672 {"~1.1.1-beta", "1.1.1", true},
673 {"~1.2.3", "1.2.5", true},
674 {"~1.2.3", "1.2.2", false},
675 {"~1.2.3", "1.3.2", false},
676 {"~1.1", "1.2.3", false},
677 {"~1.3", "2.4.5", false},
678
679 // compound
680 {">1.1, <2", "1.1.1", false},
681 {" >1.1 <2 ", "1.1.1", false},
682 {">1.1, <2", "1.2.1", true},
683 {">1.1 <2", "1.2.1", true},
684 {">1.1, <3", "4.3.2", false},
685 {">1.1 <3", "4.3.2", false},
686 {">=1.1, <2, !=1.2.3", "1.2.3", false},
687 {">=1.1 <2 !=1.2.3", "1.2.3", false},
688 {">=1.1, <2, !=1.2.3 || > 3", "4.1.2", true},
689 {">=1.1 <2 !=1.2.3 || > 3", "4.1.2", true},
690 {">=1.1, <2, !=1.2.3 || > 3", "3.1.2", false},
691 {">=1.1, <2, !=1.2.3 || >= 3", "3.0.0", true},
692 {">=1.1, <2, !=1.2.3 || > 3", "3.0.0", false},
693 {"> 1.1, <2", "1.2.1", true},
694 {">1.1, < 3", "4.3.2", false},
695 {">= 1.1, < 2, !=1.2.3", "1.2.3", false},
696 {">= 1.1, <2, !=1.2.3 || > 3", "4.1.2", true},
697 {">= 1.1, <2, != 1.2.3 || > 3", "3.1.2", false},
698 {">= 1.1, <2, != 1.2.3 || >= 3", "3.0.0", true},
699 {">= 1.1, <2, !=1.2.3 || > 3", "3.0.0", false},
700 {">= 1.1, <2, !=1.2.3 || > 3", "1.2.3", false},
701 }
702 for _, tt := range tests {
703 t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.15 fmt/Sprintf().("%s vs %s", tt.constraint, tt.version), func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) {
704 c, err := NewConstraintsscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/NewConstraints().(tt.constraint)
705 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
706
707 v, err := Parsescip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Parse().(tt.version)
708 requirescip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/require`/NoError().(t, err)
709
710 got := c.Checkscip-go gomod github.com/aquasecurity/go-version . `github.com/aquasecurity/go-version/pkg/semver`/Constraints#Check().(v)
711 assertscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.6.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.want, got)
712 })
713 }
714 }