From: Vincent Pit Date: Sat, 5 Feb 2011 19:10:09 +0000 (+0100) Subject: Correctly handle leading zeros in version components X-Git-Tag: v0.12~13 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=400c6076d23a0fa83acac09fc3351653c527cb65;p=perl%2Fmodules%2FCPANPLUS-Dist-Gentoo.git Correctly handle leading zeros in version components Again, thanks Torsten Veller for bringing this to my attention. --- diff --git a/lib/CPANPLUS/Dist/Gentoo/Version.pm b/lib/CPANPLUS/Dist/Gentoo/Version.pm index e1f7674..d76bcb4 100644 --- a/lib/CPANPLUS/Dist/Gentoo/Version.pm +++ b/lib/CPANPLUS/Dist/Gentoo/Version.pm @@ -126,13 +126,26 @@ sub _spaceship { my @a = @{ $v1->version }; my @b = @{ $v2->version }; - while (@a and @b) { + { my $x = shift @a; my $y = shift @b; my $c = $x <=> $y; return $c if $c; } + while (@a and @b) { + my $x = shift @a; + my $y = shift @b; + my $c; + if ($x =~ /^0/ or $y =~ /^0/) { + s/0+\z// for $x, $y; + $c = $x cmp $y; + } else { + $c = $x <=> $y; + } + return $c if $c; + } + return 1 if @a; return -1 if @b; } diff --git a/t/20-version.t b/t/20-version.t index 14ea329..bd48d75 100644 --- a/t/20-version.t +++ b/t/20-version.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 3 + (2 + 2 * 3) * (52 + 4 * 7); +use Test::More tests => 3 + (2 + 2 * 3) * (66 + 4 * 7); use CPANPLUS::Dist::Gentoo::Version; @@ -21,25 +21,41 @@ like $@, qr/Couldn't\s+parse\s+version\s+string/, "'dongs' < V->new(1)"; my @tests = ( [ 0, 0 => 0 ], [ 1, 0 => 1 ], - [ 0, 1 => -1 ], [ 1, 1 => 0 ], + [ '00', '0' => 0 ], + [ '01', '1' => 0 ], + [ '001', '1' => 0 ], + [ '001', '01' => 0 ], + [ '1.0', 1 => 1 ], # Yes, 1.0 > 1. Deal with it [ '1.0', '1.0' => 0 ], [ '1.1', 1 => 1 ], [ '1.1', '1.0' => 1 ], - [ '1.0', '1.1' => -1 ], - [ '1.0.0', 1 => 1 ], # Ditto - [ '1.0.0', '1.0' => 1 ], # Tritto - [ '1.0.0', '1.0.0' => 0 ], - [ '1.0.1', '1.1' => -1 ], - [ '1.0.1', '1.0.0' => 1 ], - - [ '1a', 1 => 1 ], - [ '1.0a', 1 => 1 ], - [ '1.0', '1a' => 1 ], # Same - [ '1a', '1b' => -1 ], - [ '1.1a', '1.0b' => 1 ], + [ '1.1', '1.1' => 0 ], + [ '1.1', '1.10' => -1 ], + [ '1.1', '1.01' => 1 ], + [ '1.1', '1.010' => 1 ], + [ '1.01', '1.010' => 0 ], + + [ '1.0.0', 1 => 1 ], # Ditto + [ '1.0.0', '1.0' => 1 ], # Tritto + [ '1.0.0', '1.0.0' => 0 ], + [ '1.0.1', '1.1' => -1 ], + [ '1.0.1', '1.0.0' => 1 ], + [ '1.0.1', '1.0.1' => 0 ], + [ '1.0.1', '1.0.10' => -1 ], + [ '1.0.1', '1.0.01' => 1 ], + [ '1.0.1', '1.0.010' => 1 ], + [ '1.0.01', '1.0.010' => 0 ], + + [ '1a', 1 => 1 ], + [ '1.0a', 1 => 1 ], + [ '1.0', '1a' => 1 ], # Same + [ '1a', '1b' => -1 ], + [ '1.1a', '1.0b' => 1 ], + [ '1.1a', '1.01a' => 1 ], + [ '1.01a', '1.010a' => 0 ], map( { [ '1.0', "1.0_${_}" => 1 ],