This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathversion_test.dart
411 lines (346 loc) · 12.7 KB
/
version_test.dart
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
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
import 'utils.dart';
void main() {
test('none', () {
expect(Version.none.toString(), equals('0.0.0'));
});
test('prioritize()', () {
// A correctly sorted list of versions in order of increasing priority.
var versions = [
'1.0.0-alpha',
'2.0.0-alpha',
'1.0.0',
'1.0.0+build',
'1.0.1',
'1.1.0',
'2.0.0'
];
// Ensure that every pair of versions is prioritized in the order that it
// appears in the list.
for (var i = 0; i < versions.length; i++) {
for (var j = 0; j < versions.length; j++) {
var a = Version.parse(versions[i]);
var b = Version.parse(versions[j]);
expect(Version.prioritize(a, b), equals(i.compareTo(j)));
}
}
});
test('antiprioritize()', () {
// A correctly sorted list of versions in order of increasing antipriority.
var versions = [
'2.0.0-alpha',
'1.0.0-alpha',
'2.0.0',
'1.1.0',
'1.0.1',
'1.0.0+build',
'1.0.0'
];
// Ensure that every pair of versions is prioritized in the order that it
// appears in the list.
for (var i = 0; i < versions.length; i++) {
for (var j = 0; j < versions.length; j++) {
var a = Version.parse(versions[i]);
var b = Version.parse(versions[j]);
expect(Version.antiprioritize(a, b), equals(i.compareTo(j)));
}
}
});
group('constructor', () {
test('throws on negative numbers', () {
expect(() => Version(-1, 1, 1), throwsArgumentError);
expect(() => Version(1, -1, 1), throwsArgumentError);
expect(() => Version(1, 1, -1), throwsArgumentError);
});
});
group('comparison', () {
// A correctly sorted list of versions.
var versions = [
'1.0.0-alpha',
'1.0.0-alpha.1',
'1.0.0-beta.2',
'1.0.0-beta.11',
'1.0.0-rc.1',
'1.0.0-rc.1+build.1',
'1.0.0',
'1.0.0+0.3.7',
'1.3.7+build',
'1.3.7+build.2.b8f12d7',
'1.3.7+build.11.e0f985a',
'2.0.0',
'2.1.0',
'2.2.0',
'2.11.0',
'2.11.1'
];
test('compareTo()', () {
// Ensure that every pair of versions compares in the order that it
// appears in the list.
for (var i = 0; i < versions.length; i++) {
for (var j = 0; j < versions.length; j++) {
var a = Version.parse(versions[i]);
var b = Version.parse(versions[j]);
expect(a.compareTo(b), equals(i.compareTo(j)));
}
}
});
test('operators', () {
for (var i = 0; i < versions.length; i++) {
for (var j = 0; j < versions.length; j++) {
var a = Version.parse(versions[i]);
var b = Version.parse(versions[j]);
expect(a < b, equals(i < j));
expect(a > b, equals(i > j));
expect(a <= b, equals(i <= j));
expect(a >= b, equals(i >= j));
expect(a == b, equals(i == j));
expect(a != b, equals(i != j));
}
}
});
test('equality', () {
expect(Version.parse('01.2.3'), equals(Version.parse('1.2.3')));
expect(Version.parse('1.02.3'), equals(Version.parse('1.2.3')));
expect(Version.parse('1.2.03'), equals(Version.parse('1.2.3')));
expect(Version.parse('1.2.3-01'), equals(Version.parse('1.2.3-1')));
expect(Version.parse('1.2.3+01'), equals(Version.parse('1.2.3+1')));
});
});
test('allows()', () {
expect(v123, allows(v123));
expect(
v123,
doesNotAllow(
Version.parse('2.2.3'),
Version.parse('1.3.3'),
Version.parse('1.2.4'),
Version.parse('1.2.3-dev'),
Version.parse('1.2.3+build')));
});
test('allowsAll()', () {
expect(v123.allowsAll(v123), isTrue);
expect(v123.allowsAll(v003), isFalse);
expect(v123.allowsAll(VersionRange(min: v114, max: v124)), isFalse);
expect(v123.allowsAll(VersionConstraint.any), isFalse);
expect(v123.allowsAll(VersionConstraint.empty), isTrue);
});
test('allowsAny()', () {
expect(v123.allowsAny(v123), isTrue);
expect(v123.allowsAny(v003), isFalse);
expect(v123.allowsAny(VersionRange(min: v114, max: v124)), isTrue);
expect(v123.allowsAny(VersionConstraint.any), isTrue);
expect(v123.allowsAny(VersionConstraint.empty), isFalse);
});
test('intersect()', () {
// Intersecting the same version returns the version.
expect(v123.intersect(v123), equals(v123));
// Intersecting a different version allows no versions.
expect(v123.intersect(v114).isEmpty, isTrue);
// Intersecting a range returns the version if the range allows it.
expect(v123.intersect(VersionRange(min: v114, max: v124)), equals(v123));
// Intersecting a range allows no versions if the range doesn't allow it.
expect(v114.intersect(VersionRange(min: v123, max: v124)).isEmpty, isTrue);
});
group('union()', () {
test('with the same version returns the version', () {
expect(v123.union(v123), equals(v123));
});
test('with a different version returns a version that matches both', () {
var result = v123.union(v080);
expect(result, allows(v123));
expect(result, allows(v080));
// Nothing in between should match.
expect(result, doesNotAllow(v114));
});
test('with a range returns the range if it contains the version', () {
var range = VersionRange(min: v114, max: v124);
expect(v123.union(range), equals(range));
});
test('with a range with the version on the edge, expands the range', () {
expect(
v124.union(VersionRange(
min: v114, max: v124, alwaysIncludeMaxPreRelease: true)),
equals(VersionRange(min: v114, max: v124, includeMax: true)));
expect(
v124.firstPreRelease.union(VersionRange(min: v114, max: v124)),
equals(VersionRange(
min: v114, max: v124.firstPreRelease, includeMax: true)));
expect(v114.union(VersionRange(min: v114, max: v124)),
equals(VersionRange(min: v114, max: v124, includeMin: true)));
});
test(
'with a range allows both the range and the version if the range '
"doesn't contain the version", () {
var result = v123.union(VersionRange(min: v003, max: v114));
expect(result, allows(v123));
expect(result, allows(v010));
});
});
group('difference()', () {
test('with the same version returns an empty constraint', () {
expect(v123.difference(v123), isEmpty);
});
test('with a different version returns the original version', () {
expect(v123.difference(v080), equals(v123));
});
test('returns an empty constraint with a range that contains the version',
() {
expect(v123.difference(VersionRange(min: v114, max: v124)), isEmpty);
});
test("returns the version constraint with a range that doesn't contain it",
() {
expect(v123.difference(VersionRange(min: v140, max: v300)), equals(v123));
});
});
test('isEmpty', () {
expect(v123.isEmpty, isFalse);
});
test('nextMajor', () {
expect(v123.nextMajor, equals(v200));
expect(v114.nextMajor, equals(v200));
expect(v200.nextMajor, equals(v300));
// Ignores pre-release if not on a major version.
expect(Version.parse('1.2.3-dev').nextMajor, equals(v200));
// Just removes it if on a major version.
expect(Version.parse('2.0.0-dev').nextMajor, equals(v200));
// Strips build suffix.
expect(Version.parse('1.2.3+patch').nextMajor, equals(v200));
});
test('nextMinor', () {
expect(v123.nextMinor, equals(v130));
expect(v130.nextMinor, equals(v140));
// Ignores pre-release if not on a minor version.
expect(Version.parse('1.2.3-dev').nextMinor, equals(v130));
// Just removes it if on a minor version.
expect(Version.parse('1.3.0-dev').nextMinor, equals(v130));
// Strips build suffix.
expect(Version.parse('1.2.3+patch').nextMinor, equals(v130));
});
test('nextPatch', () {
expect(v123.nextPatch, equals(v124));
expect(v200.nextPatch, equals(v201));
// Just removes pre-release version if present.
expect(Version.parse('1.2.4-dev').nextPatch, equals(v124));
// Strips build suffix.
expect(Version.parse('1.2.3+patch').nextPatch, equals(v124));
});
test('nextBreaking', () {
expect(v123.nextBreaking, equals(v200));
expect(v072.nextBreaking, equals(v080));
expect(v003.nextBreaking, equals(v010));
// Removes pre-release version if present.
expect(Version.parse('1.2.3-dev').nextBreaking, equals(v200));
// Strips build suffix.
expect(Version.parse('1.2.3+patch').nextBreaking, equals(v200));
});
test('parse()', () {
expect(Version.parse('0.0.0'), equals(Version(0, 0, 0)));
expect(Version.parse('12.34.56'), equals(Version(12, 34, 56)));
expect(Version.parse('1.2.3-alpha.1'),
equals(Version(1, 2, 3, pre: 'alpha.1')));
expect(Version.parse('1.2.3-x.7.z-92'),
equals(Version(1, 2, 3, pre: 'x.7.z-92')));
expect(Version.parse('1.2.3+build.1'),
equals(Version(1, 2, 3, build: 'build.1')));
expect(Version.parse('1.2.3+x.7.z-92'),
equals(Version(1, 2, 3, build: 'x.7.z-92')));
expect(Version.parse('1.0.0-rc-1+build-1'),
equals(Version(1, 0, 0, pre: 'rc-1', build: 'build-1')));
expect(() => Version.parse('1.0'), throwsFormatException);
expect(() => Version.parse('1a2b3'), throwsFormatException);
expect(() => Version.parse('1.2.3.4'), throwsFormatException);
expect(() => Version.parse('1234'), throwsFormatException);
expect(() => Version.parse('-2.3.4'), throwsFormatException);
expect(() => Version.parse('1.3-pre'), throwsFormatException);
expect(() => Version.parse('1.3+build'), throwsFormatException);
expect(() => Version.parse('1.3+bu?!3ild'), throwsFormatException);
});
group('toString()', () {
test('returns the version string', () {
expect(Version(0, 0, 0).toString(), equals('0.0.0'));
expect(Version(12, 34, 56).toString(), equals('12.34.56'));
expect(
Version(1, 2, 3, pre: 'alpha.1').toString(), equals('1.2.3-alpha.1'));
expect(Version(1, 2, 3, pre: 'x.7.z-92').toString(),
equals('1.2.3-x.7.z-92'));
expect(Version(1, 2, 3, build: 'build.1').toString(),
equals('1.2.3+build.1'));
expect(Version(1, 2, 3, pre: 'pre', build: 'bui').toString(),
equals('1.2.3-pre+bui'));
});
test('preserves leading zeroes', () {
expect(Version.parse('001.02.0003-01.dev+pre.002').toString(),
equals('001.02.0003-01.dev+pre.002'));
});
});
group('canonicalizedVersion', () {
test('returns version string', () {
expect(Version(0, 0, 0).canonicalizedVersion, equals('0.0.0'));
expect(Version(12, 34, 56).canonicalizedVersion, equals('12.34.56'));
expect(Version(1, 2, 3, pre: 'alpha.1').canonicalizedVersion,
equals('1.2.3-alpha.1'));
expect(Version(1, 2, 3, pre: 'x.7.z-92').canonicalizedVersion,
equals('1.2.3-x.7.z-92'));
expect(Version(1, 2, 3, build: 'build.1').canonicalizedVersion,
equals('1.2.3+build.1'));
expect(Version(1, 2, 3, pre: 'pre', build: 'bui').canonicalizedVersion,
equals('1.2.3-pre+bui'));
});
test('discards leading zeroes', () {
expect(Version.parse('001.02.0003-01.dev+pre.002').canonicalizedVersion,
equals('1.2.3-1.dev+pre.2'));
});
test('example from documentation', () {
final v = Version.parse('01.02.03-01.dev+pre.02');
assert(v.toString() == '01.02.03-01.dev+pre.02');
assert(v.canonicalizedVersion == '1.2.3-1.dev+pre.2');
assert(Version.parse(v.canonicalizedVersion) == v);
});
});
group('primary', () {
test('single', () {
expect(
_primary([
'1.2.3',
]).toString(),
'1.2.3',
);
});
test('normal', () {
expect(
_primary([
'1.2.3',
'1.2.2',
]).toString(),
'1.2.3',
);
});
test('all prerelease', () {
expect(
_primary([
'1.2.2-dev.1',
'1.2.2-dev.2',
]).toString(),
'1.2.2-dev.2',
);
});
test('later prerelease', () {
expect(
_primary([
'1.2.3',
'1.2.3-dev',
]).toString(),
'1.2.3',
);
});
test('empty', () {
expect(() => Version.primary([]), throwsStateError);
});
});
}
Version _primary(List<String> input) =>
Version.primary(input.map(Version.parse).toList());