Add VersionConstraint.compatibleWith constructor
diff --git a/pkgs/pub_semver/lib/src/version_constraint.dart b/pkgs/pub_semver/lib/src/version_constraint.dart index 29dd574..1b86602 100644 --- a/pkgs/pub_semver/lib/src/version_constraint.dart +++ b/pkgs/pub_semver/lib/src/version_constraint.dart
@@ -25,11 +25,9 @@ /// /// This string is one of: /// - /// * "any". See [any]. - /// * "^" followed by a version string. Versions compatible with the - /// version number. This allows versions greater than or equal to the - /// version, and less then the next breaking version (see - /// [Version.nextBreaking]). + /// * "any". [any] version. + /// * "^" followed by a version string. Versions compatible with + /// ([VersionConstraint.compatibleWith]) the version. /// * a series of version parts. Each part can be one of: /// * A version string like `1.2.3`. In other words, anything that can be /// parsed by [Version.parse()]. @@ -107,7 +105,7 @@ getCurrentTextIndex() => originalText.length - text.length; var startTextIndex = getCurrentTextIndex(); - if(constraints.isNotEmpty || text.isNotEmpty) { + if (constraints.isNotEmpty || text.isNotEmpty) { var constraint = op + originalText.substring(startTextIndex, getCurrentTextIndex()); throw new FormatException('Cannot include other constraints with ' @@ -152,6 +150,17 @@ return new VersionConstraint.intersection(constraints); } + /// Creates a version constraint which allows all versions that are + /// backward compatible with [version]. + /// + /// Versions are considered backward compatible with [version] if they + /// are greater than or equal to [version], but less than the next breaking + /// version ([Version.nextBreaking]) of [version]. + factory VersionConstraint.compatibleWith(Version version) { + return new VersionRange(min: version, includeMin: true, + max: version.nextBreaking); + } + /// Creates a new version constraint that is the intersection of /// [constraints]. ///
diff --git a/pkgs/pub_semver/test/version_constraint_test.dart b/pkgs/pub_semver/test/version_constraint_test.dart index 16a3fdc..b58eab7 100644 --- a/pkgs/pub_semver/test/version_constraint_test.dart +++ b/pkgs/pub_semver/test/version_constraint_test.dart
@@ -126,30 +126,26 @@ test('parses a "^" post-1.0.0 version', () { var constraint = new VersionConstraint.parse('^1.2.3'); - expect(constraint, equals(new VersionRange(min: v123, includeMin: true, - max: v123.nextBreaking))); + expect(constraint, equals(new VersionConstraint.compatibleWith(v123))); }); test('parses a "^" pre-1.0.0, post-0.1.0 version', () { var constraint = new VersionConstraint.parse('^0.7.2'); - expect(constraint, equals(new VersionRange(min: v072, includeMin: true, - max: v072.nextBreaking))); + expect(constraint, equals(new VersionConstraint.compatibleWith(v072))); }); test('parses a "^" pre-0.1.0 version', () { var constraint = new VersionConstraint.parse('^0.0.3'); - expect(constraint, equals(new VersionRange(min: v003, includeMin: true, - max: v003.nextBreaking))); + expect(constraint, equals(new VersionConstraint.compatibleWith(v003))); }); test('parses a "^" pre-release version', () { var constraint = new VersionConstraint.parse('^0.7.2-pre+1'); var min = new Version.parse('0.7.2-pre+1'); - expect(constraint, equals(new VersionRange(min: min, includeMin: true, - max: min.nextBreaking))); + expect(constraint, equals(new VersionConstraint.compatibleWith(min))); }); test('does not allow "^" to be mixed with other constraints', () { @@ -177,4 +173,11 @@ } }); }); + + test('compatibleWith', () { + var constraint = new VersionConstraint.compatibleWith(v072); + + expect(constraint, equals(new VersionRange(min: v072, includeMin: true, + max: v072.nextBreaking))); + }); }