From: Vincent Pit Date: Sat, 11 Sep 2010 15:33:31 +0000 (+0200) Subject: Fix $atom1 cmp $atom2 X-Git-Tag: v0.11~18 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FCPANPLUS-Dist-Gentoo.git;a=commitdiff_plain;h=f1e11349a9e94499b1025601c2f7e4c73e18810e Fix $atom1 cmp $atom2 --- diff --git a/lib/CPANPLUS/Dist/Gentoo/Atom.pm b/lib/CPANPLUS/Dist/Gentoo/Atom.pm index fcfa685..dfddd42 100644 --- a/lib/CPANPLUS/Dist/Gentoo/Atom.pm +++ b/lib/CPANPLUS/Dist/Gentoo/Atom.pm @@ -192,20 +192,21 @@ sub _spaceship { sub _cmp { my ($a1, $a2, $r) = @_; - my $s1 = $a1->qualified_name; - my $v1 = $a1->version; - $s1 .= "-$v1" if defined $v1; + if (defined $a2) { + my $p1 = $a1->qualified_name; - my $s2; - if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) { - $s2 = $a2->qualified_name; - my $v2 = $a2->version; - $s2 .= "-$v2" if defined $v2; - } else { - $s2 = $a2; + unless (Scalar::Util::blessed($a2) && $a2->isa(__PACKAGE__)) { + $a2 = eval { __PACKAGE__->new(atom => $a2) }; + Carp::confess("Can't compare an atom against something that's not an atom or an atom string ($@)") if $@; + } + my $p2 = $a2->qualified_name; + + if (my $c = $p1 cmp $p2) { + return $r ? -$c : $c; + } } - $s1 cmp $s2; + $a1 <=> $a2; } sub _stringify { diff --git a/t/31-atom-cmp.t b/t/31-atom-cmp.t index 5801010..9422071 100644 --- a/t/31-atom-cmp.t +++ b/t/31-atom-cmp.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 2 * 5 * ((8 * 7) / 2); +use Test::More tests => 2 * 8 * ((8 * 7) / 2); use CPANPLUS::Dist::Gentoo::Atom; @@ -128,7 +128,8 @@ for my $t (@tests) { my $bs = "$b"; compare_ok($a, '<=>', $bs, $exp); - if (my $bv = $b->version) { + my $bv = $b->version; + if (defined $bv) { compare_ok($a, '<=>', $bv, $exp); compare_ok($a, '<=>', "$bv", $exp); } else { @@ -136,5 +137,14 @@ for my $t (@tests) { } compare_ok($a, 'cmp', $b, $exp); + + my $bz = $b->qualified_name; + $bz .= "-$bv" if defined $bv; + compare_ok($a, 'cmp', $bz, $exp); + + $bz = "test/zzz"; + $bz .= "-$bv" if defined $bv; + compare_ok($a, 'cmp', $bz, -1); + compare_ok($bz, 'cmp', $b, 1); } }