From 96a76eeffca02e63917346ac76daedaec86c7a7e Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Sat, 29 Sep 2012 16:27:38 -0700 Subject: [PATCH] Semantic Version Parsing: ~version Implemented `~` according to #643 and used the following versions as a test case as defined by @Seldaek: * "~1.2.3.4" = ">=1.2.3.4 <1.2.4.0-dev" * "~1.2.3" = ">=1.2.3 <1.3.0-dev" * "~1.2" = ">=1.2.0 <2.0.0-dev" * "~1" = ">=1.0.0 <2.0.0-dev" Refs #643 --- .../Package/Version/VersionParser.php | 22 ++++++++++++++++ .../Package/Version/VersionParserTest.php | 25 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index a87184f53..acced8173 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -245,6 +245,28 @@ class VersionParser return array(); } + if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$}', $constraint, $matches)) { + if (isset($matches[4])) { + $highVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] + 1) . '.0-dev'; + $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.' . $matches[4]; + } elseif (isset($matches[3])) { + $highVersion = $matches[1] . '.' . ($matches[2] + 1) . '.0.0-dev'; + $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.0'; + } else { + $highVersion = ($matches[1] + 1) . '.0.0.0-dev'; + if (isset($matches[2])) { + $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0'; + } else { + $lowVersion = $matches[1] . '.0.0.0'; + } + } + + return array( + new VersionConstraint('>=', $lowVersion), + new VersionConstraint('<', $highVersion), + ); + } + // match wildcard constraints if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) { if (isset($matches[3])) { diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 45cfb7848..3b9db857a 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -240,6 +240,31 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase ); } + /** + * @dataProvider tildeConstraints + */ + public function testParseTildeWildcard($input, $min, $max) + { + $parser = new VersionParser; + if ($min) { + $expected = new MultiConstraint(array($min, $max)); + } else { + $expected = $max; + } + + $this->assertSame((string) $expected, (string) $parser->parseConstraints($input)); + } + + public function tildeConstraints() + { + return array( + array('~1', new VersionConstraint('>=', '1.0.0.0'), new VersionConstraint('<', '2.0.0.0-dev')), + array('~1.2', new VersionConstraint('>=', '1.2.0.0'), new VersionConstraint('<', '2.0.0.0-dev')), + array('~1.2.3', new VersionConstraint('>=', '1.2.3.0'), new VersionConstraint('<', '1.3.0.0-dev')), + array('~1.2.3.4', new VersionConstraint('>=', '1.2.3.4'), new VersionConstraint('<', '1.2.4.0-dev')), + ); + } + public function testParseConstraintsMulti() { $parser = new VersionParser;