Add tests and improve docs for Version.primary (#47)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 40f7c3e..63f7285 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,5 @@
+# 1.4.5-dev
+
# 1.4.4
- Fix a bug of `VersionRange.union` where ranges bounded at infinity would get
diff --git a/lib/src/version.dart b/lib/src/version.dart
index e769dcd..314937d 100644
--- a/lib/src/version.dart
+++ b/lib/src/version.dart
@@ -131,10 +131,12 @@
}
}
- /// Returns the primary version out of a list of candidates.
+ /// Returns the primary version out of [versions].
///
/// This is the highest-numbered stable (non-prerelease) version. If there
/// are no stable versions, it's just the highest-numbered version.
+ ///
+ /// If [versions] is empty, returns `null`.
static Version primary(List<Version> versions) {
Version primary;
for (var version in versions) {
diff --git a/pubspec.yaml b/pubspec.yaml
index a2238d2..78de107 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: pub_semver
-version: 1.4.4
+version: 1.4.5-dev
description: >-
Versions and version constraints implementing pub's versioning policy. This
diff --git a/test/version_test.dart b/test/version_test.dart
index df0d25b..70d2779 100644
--- a/test/version_test.dart
+++ b/test/version_test.dart
@@ -2,9 +2,8 @@
// 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:test/test.dart';
-
import 'package:pub_semver/pub_semver.dart';
+import 'package:test/test.dart';
import 'utils.dart';
@@ -330,4 +329,55 @@
equals('001.02.0003-01.dev+pre.002'));
});
});
+
+ 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(
+ _primary([]),
+ isNull,
+ );
+ });
+ });
}
+
+Version _primary(List<String> input) =>
+ Version.primary(input.map((e) => Version.parse(e)).toList());