From: Frank Lichtenheld Date: Sat, 22 Sep 2007 21:00:44 +0000 (+0200) Subject: Dpkg::Version: Add new function compare_versions X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8584f930e4085af71cb0b2bfddfde912c35176ac;p=dpkg Dpkg::Version: Add new function compare_versions Emulates the dpkg --compare-versions behaviour (Doesn't support the -nl operators yet, though). This is often easier to write and read instead of using vercmp directly. Also add tests for the new function. --- diff --git a/scripts/Dpkg/Version.pm b/scripts/Dpkg/Version.pm index 6a9a1681..abebd167 100644 --- a/scripts/Dpkg/Version.pm +++ b/scripts/Dpkg/Version.pm @@ -162,6 +162,36 @@ sub vercmp ($$) return verrevcmp($version{revision}, $refversion{revision}); } +=item compare_versions + +Emulates dpkg --compare-versions. Takes two versions as arguments +one and three and one operator as argument two. Supports the following +operators: 'gt', 'ge', 'eq', 'le', 'lt', and '>>', '>=', '=', '<=', '<<'. +Returns a true value if the specified condition is true, a false value +otherwise. + +=cut + +sub compare_versions ($$$) +{ + my $rel = $_[1]; + my $res = vercmp($_[0], $_[2]); + + if ($rel eq 'gt' or $rel =~ />?>/) { + return $res > 0; + } elsif ($rel eq 'ge' or $rel eq '>=') { + return $res >= 0; + } elsif ($rel eq 'eq' or $rel eq '=') { + return $res == 0; + } elsif ($rel eq 'le' or $rel eq '<=') { + return $res <= 0; + } elsif ($rel eq 'lt' or $rel =~ / '1.0-1', result => 0, relation => 'eq', }, + {a => '1.0000-1', + b => '1.0-1', + result => 0, + relation => 'ge', + }, + {a => '1', + b => '0:1', + result => 0, + relation => 'eq', + }, + {a => '2:2.5', + b => '1:7.5', + result => 1, + relation => 'gt', + }, ); +my @test_failure = ({a => '1.0-1', + b => '2.0-2', + relation => 'gt', + }, + {a => '2.2~rc-4', + b => '2.2-1', + relation => 'eq', + }, + ); -plan tests => @versions * 2 + 1; +plan tests => @versions * 3 + @test_failure * 2 + 1; sub dpkg_vercmp{ my ($a,$b,$cmp) = @_; @@ -41,8 +65,16 @@ use_ok('Dpkg::Version'); for my $version_cmp (@versions) { ok(Dpkg::Version::vercmp($$version_cmp{a},$$version_cmp{b}) == $$version_cmp{result}, - "Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b} ok"); + "vercmp: Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b} ok"); + ok(Dpkg::Version::compare_versions($$version_cmp{a},$$version_cmp{relation},$$version_cmp{b}), + "compare_versions: Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b} ok"); ok(dpkg_vercmp($$version_cmp{a},$$version_cmp{b},$$version_cmp{relation}), "Dpkg concures: Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b}"); } +for my $version_cmp (@test_failure) { + ok(!Dpkg::Version::compare_versions($$version_cmp{a},$$version_cmp{relation},$$version_cmp{b}), + "compare_versions: Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b} false"); + ok(!dpkg_vercmp($$version_cmp{a},$$version_cmp{b},$$version_cmp{relation}), + "Dpkg concures: Version $$version_cmp{a} $$version_cmp{relation} $$version_cmp{b}"); +}