Sync up with Digest-MD5-2.38 from CPAN
[p5sagit/p5-mst-13.2.git] / lib / version.t
index bb5a8f8..47989e3 100644 (file)
@@ -5,6 +5,7 @@
 #########################
 
 use Test::More qw(no_plan);
+use Data::Dumper;
 require Test::Harness;
 no warnings 'once';
 *Verbose = \$Test::Harness::Verbose;
@@ -73,25 +74,25 @@ sub BaseTests {
     # Test bare number processing
     diag "tests with bare numbers" if $Verbose;
     $version = $CLASS->new(5.005_03);
-    is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
+    is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' );
     $version = $CLASS->new(1.23);
-    is ( "$version" , "1.230" , '1.23 eq "1.230"' );
+    is ( "$version" , "1.23" , '1.23 eq "1.23"' );
     
     # Test quoted number processing
     diag "tests with quoted numbers" if $Verbose;
     $version = $CLASS->new("5.005_03");
-    is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
+    is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' );
     $version = $CLASS->new("v1.23");
-    is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
+    is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' );
     
     # Test stringify operator
     diag "tests with stringify" if $Verbose;
     $version = $CLASS->new("5.005");
     is ( "$version" , "5.005" , '5.005 eq "5.005"' );
     $version = $CLASS->new("5.006.001");
-    is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
+    is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' );
     $version = $CLASS->new("1.2.3_4");
-    is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
+    is ( "$version" , "1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
     
     # test illegal formats
     diag "test illegal formats" if $Verbose;
@@ -119,13 +120,14 @@ sub BaseTests {
        "Version string contains invalid data; ignoring");
 
     # from here on out capture the warning and test independently
+    {
     my $warning;
     local $SIG{__WARN__} = sub { $warning = $_[0] };
     $version = $CLASS->new("99 and 44/100 pure");
 
     like($warning, qr/$warnregex/,
        "Version string contains invalid data; ignoring");
-    ok ("$version" eq "99.000", '$version eq "99.000"');
+    is ("$version", "99", '$version eq "99"');
     ok ($version->numify == 99.0, '$version->numify == 99.0');
     ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
     
@@ -145,33 +147,12 @@ sub BaseTests {
     
     # Test comparison operators with self
     diag "tests with self" if $Verbose;
-    ok ( $version eq $version, '$version eq $version' );
-    is ( $version cmp $version, 0, '$version cmp $version == 0' );
+    is ( $version <=> $version, 0, '$version <=> $version == 0' );
     ok ( $version == $version, '$version == $version' );
     
-    # test first with non-object
-    $version = $CLASS->new("5.006.001");
-    $new_version = "5.8.0";
-    diag "tests with non-objects" if $Verbose;
-    ok ( $version ne $new_version, '$version ne $new_version' );
-    ok ( $version lt $new_version, '$version lt $new_version' );
-    ok ( $new_version gt $version, '$new_version gt $version' );
-    ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
-    $new_version = "$version";
-    ok ( $version eq $new_version, '$version eq $new_version' );
-    ok ( $new_version eq $version, '$new_version eq $version' );
-    
-    # now test with existing object
-    $new_version = $CLASS->new("5.8.0");
-    diag "tests with objects" if $Verbose;
-    ok ( $version ne $new_version, '$version ne $new_version' );
-    ok ( $version lt $new_version, '$version lt $new_version' );
-    ok ( $new_version gt $version, '$new_version gt $version' );
-    $new_version = $CLASS->new("$version");
-    ok ( $version eq $new_version, '$version eq $new_version' );
-    
     # Test Numeric Comparison operators
     # test first with non-object
+    $version = $CLASS->new("5.006.001");
     $new_version = "5.8.0";
     diag "numeric tests with non-objects" if $Verbose;
     ok ( $version == $version, '$version == $version' );
@@ -196,20 +177,20 @@ sub BaseTests {
     # test with long decimals
     diag "Tests with extended decimal versions" if $Verbose;
     $version = $CLASS->new(1.002003);
-    ok ( $version eq "1.2.3", '$version eq "1.2.3"');
+    ok ( $version == "1.2.3", '$version == "1.2.3"');
     ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
     $version = $CLASS->new("2002.09.30.1");
-    ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
+    ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
     ok ( $version->numify == 2002.009030001,
        '$version->numify == 2002.009030001');
     
     # now test with alpha version form with string
     $version = $CLASS->new("1.2.3");
     $new_version = "1.2.3_4";
-    diag "tests with alpha-style non-objects" if $Verbose;
-    ok ( $version lt $new_version, '$version lt $new_version' );
-    ok ( $new_version gt $version, '$new_version gt $version' );
-    ok ( $version ne $new_version, '$version ne $new_version' );
+    diag "numeric tests with alpha-style non-objects" if $Verbose;
+    ok ( $version < $new_version, '$version < $new_version' );
+    ok ( $new_version > $version, '$new_version > $version' );
+    ok ( $version != $new_version, '$version != $new_version' );
     
     $version = $CLASS->new("1.2.4");
     diag "numeric tests with alpha-style non-objects"
@@ -269,9 +250,9 @@ SKIP: {
     # test the qv() sub
     diag "testing qv" if $Verbose;
     $version = qv("1.2");
-    cmp_ok ( $version, "eq", "v1.2.0", 'qv("1.2") eq "1.2.0"' );
+    is ( "$version", "v1.2", 'qv("1.2") == "1.2.0"' );
     $version = qv(1.2);
-    cmp_ok ( $version, "eq", "v1.2.0", 'qv(1.2) eq "1.2.0"' );
+    is ( "$version", "v1.2", 'qv(1.2) == "1.2.0"' );
     isa_ok( qv('5.008'), $CLASS );
 }
 
@@ -282,76 +263,93 @@ SKIP: {
     ok ($new_version == $version, "class->new($version) identical");
     $new_version = $version->new();
     isa_ok ($new_version, $CLASS );
-    is ($new_version, "0.000", "version->new() doesn't clone");
+    is ($new_version, "0", "version->new() doesn't clone");
     $new_version = $version->new("1.2.3");
-    is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
+    is ($new_version, "1.2.3" , '$version->new("1.2.3") works too');
 
     # test the CVS revision mode
     diag "testing CVS Revision" if $Verbose;
     $version = new $CLASS qw$Revision: 1.2$;
-    ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
+    ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
     $version = new $CLASS qw$Revision: 1.2.3.4$;
-    ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
+    ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
     
     # test the CPAN style reduced significant digit form
     diag "testing CPAN-style versions" if $Verbose;
     $version = $CLASS->new("1.23_01");
-    is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
+    is ( "$version" , "1.23_01", "CPAN-style alpha version" );
     ok ( $version > 1.23, "1.23_01 > 1.23");
     ok ( $version < 1.24, "1.23_01 < 1.24");
 
     # test reformed UNIVERSAL::VERSION
     diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
+
+    my $error_regex = $] < 5.006
+       ? 'version \d required'
+       : 'does not define \$...::VERSION';
     
-    # we know this file is here since we require it ourselves
-    $version = $Test::More::VERSION;
-    eval "use Test::More $version";
-    unlike($@, qr/Test::More version $version/,
-           'Replacement eval works with exact version');
-    
-    # test as class method
-    $new_version = Test::More->VERSION;
-    cmp_ok($new_version,'cmp',$version, "Called as class method");
-
-    # this should fail even with old UNIVERSAL::VERSION
-    $version = $Test::More::VERSION+0.01;
-    eval "use Test::More $version";
-    like($@, qr/Test::More version $version/,
-           'Replacement eval works with incremented version');
-    
-    TODO: {
-        local $TODO = "Test fails with Test::More versions ending in _0X";
-        $version =~ s/\.0$//; #convert to string and remove trailing '.0'
-        chop($version);        # shorten by 1 digit, should still succeed
-        eval "use Test::More $version";
-        unlike($@, qr/Test::More version $version/,
-           'Replacement eval works with single digit');
-    }
+    {
+       open F, ">aaa.pm" or die "Cannot open aaa.pm: $!\n";
+       print F "package aaa;\n\$aaa::VERSION=0.58;\n1;\n";
+       close F;
+
+       $version = 0.58;
+       eval "use lib '.'; use aaa $version";
+       unlike($@, qr/aaa version $version/,
+               'Replacement eval works with exact version');
+       
+       # test as class method
+       $new_version = "aaa"->VERSION;
+       cmp_ok($new_version,'==',$version, "Called as class method");
+
+       eval "print Completely::Unknown::Module->VERSION";
+       if ( $] < 5.008 ) {
+           unlike($@, qr/$error_regex/,
+               "Don't freak if the module doesn't even exist");
+       }
+       else {
+           unlike($@, qr/defines neither package nor VERSION/,
+               "Don't freak if the module doesn't even exist");
+       }
 
-    $version += 0.1; # this would fail with old UNIVERSAL::VERSION
-    eval "use Test::More $version";
-    like($@, qr/Test::More version $version/,
-           'Replacement eval works with incremented digit');
+       # this should fail even with old UNIVERSAL::VERSION
+       $version += 0.01;
+       eval "use lib '.'; use aaa $version";
+       like($@, qr/aaa version $version/,
+               'Replacement eval works with incremented version');
+       
+       $version =~ s/0+$//; #convert to string and remove trailing 0's
+       chop($version); # shorten by 1 digit, should still succeed
+       eval "use lib '.'; use aaa $version";
+       unlike($@, qr/aaa version $version/,
+               'Replacement eval works with single digit');
+       
+       # this would fail with old UNIVERSAL::VERSION
+       $version += 0.1;
+       eval "use lib '.'; use aaa $version";
+       like($@, qr/aaa version $version/,
+               'Replacement eval works with incremented digit');
+       unlink 'aaa.pm';
+    }
 
     { # dummy up some variously broken modules for testing
        open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
        print F "1;\n";
        close F;
-       my $error_regex;
+
+       eval "use lib '.'; use xxx 3;";
        if ( $] < 5.008 ) {
-           $error_regex = 'xxx does not define \$xxx::VERSION';
+           like($@, qr/$error_regex/,
+               'Replacement handles modules without package or VERSION'); 
        }
        else {
-           $error_regex = 'xxx defines neither package nor VERSION';
+           like($@, qr/defines neither package nor VERSION/,
+               'Replacement handles modules without package or VERSION'); 
        }
-
-       eval "use lib '.'; use xxx 3;";
-       like ($@, qr/$error_regex/,
-           'Replacement handles modules without package or VERSION'); 
-       eval "use lib '.'; use xxx; $version = xxx->VERSION";
+       eval "use lib '.'; use xxx; \$version = xxx->VERSION";
        unlike ($@, qr/$error_regex/,
            'Replacement handles modules without package or VERSION'); 
-       ok (defined($version), "Called as class method");
+       ok (!defined($version), "Called as class method");
        unlink 'xxx.pm';
     }
     
@@ -360,10 +358,10 @@ SKIP: {
        print F "package yyy;\n#look ma no VERSION\n1;\n";
        close F;
        eval "use lib '.'; use yyy 3;";
-       like ($@, qr/^yyy does not define \$yyy::VERSION/,
+       like ($@, qr/$error_regex/,
            'Replacement handles modules without VERSION'); 
        eval "use lib '.'; use yyy; print yyy->VERSION";
-       unlike ($@, qr/^yyy does not define \$yyy::VERSION/,
+       unlike ($@, qr/$error_regex/,
            'Replacement handles modules without VERSION'); 
        unlink 'yyy.pm';
     }
@@ -373,28 +371,27 @@ SKIP: {
        print F "package zzz;\n\@VERSION = ();\n1;\n";
        close F;
        eval "use lib '.'; use zzz 3;";
-       like ($@, qr/^zzz does not define \$zzz::VERSION/,
+       like ($@, qr/$error_regex/,
            'Replacement handles modules without VERSION'); 
        eval "use lib '.'; use zzz; print zzz->VERSION";
-       unlike ($@, qr/^zzz does not define \$zzz::VERSION/,
+       unlike ($@, qr/$error_regex/,
            'Replacement handles modules without VERSION'); 
        unlink 'zzz.pm';
     }
 
 SKIP:  {
-       skip 'Cannot test bare v-strings with Perl < 5.8.1', 4
-               if $] < 5.008_001; 
+       skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
+               if $] < 5.006_000; 
        diag "Tests with v-strings" if $Verbose;
        $version = $CLASS->new(1.2.3);
-       ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
+       ok("$version" == "v1.2.3", '"$version" == 1.2.3');
        $version = $CLASS->new(1.0.0);
        $new_version = $CLASS->new(1);
        ok($version == $new_version, '$version == $new_version');
-       ok($version eq $new_version, '$version eq $new_version');
        skip "version require'd instead of use'd, cannot test qv", 1
            if defined $no_qv;
        $version = qv(1.2.3);
-       ok("$version" eq "v1.2.3", 'v-string initialized qv()');
+       ok("$version" == "v1.2.3", 'v-string initialized qv()');
     }
 
     diag "Tests with real-world (malformed) data" if $Verbose;
@@ -418,8 +415,8 @@ SKIP:       {
     ok($version->numify eq "1.700", "leading space ignored");
 
     # RT 19517 - deal with undef and 'undef' initialization
-    ok($version ne 'undef', "Undef version comparison #1");
-    ok($version ne undef, "Undef version comparison #2");
+    ok("$version" ne 'undef', "Undef version comparison #1");
+    ok("$version" ne undef, "Undef version comparison #2");
     $version = $CLASS->new('undef');
     unlike($warning, qr/^Version string 'undef' contains invalid data/,
        "Version string 'undef'");
@@ -427,16 +424,17 @@ SKIP:     {
     $version = $CLASS->new(undef);
     like($warning, qr/^Use of uninitialized value/,
        "Version string 'undef'");
-    ok($version eq 'undef', "Undef version comparison #3");
-    ok($version eq undef, "Undef version comparison #4");
+    ok($version == 'undef', "Undef version comparison #3");
+    ok($version ==  undef,  "Undef version comparison #4");
     eval "\$version = \$CLASS->new()"; # no parameter at all
     unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
-    ok($version eq 'undef', "Undef version comparison #5");
-    ok($version eq undef, "Undef version comparison #6");
+    ok($version == 'undef', "Undef version comparison #5");
+    ok($version ==  undef,  "Undef version comparison #6");
 
     $version = $CLASS->new(0.000001);
     unlike($warning, qr/^Version string '1e-06' contains invalid data/,
        "Very small version objects");
+    }
 
 SKIP: {
        # dummy up a legal module for testing RT#19017
@@ -449,27 +447,27 @@ EOF
        close F;
 
        eval "use lib '.'; use www 0.000008;";
-       like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
+       like ($@, qr/^www version 0.000008 required/,
            "Make sure very small versions don't freak"); 
        eval "use lib '.'; use www 1;";
-       like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
+       like ($@, qr/^www version 1 required/,
            "Comparing vs. version with no decimal"); 
        eval "use lib '.'; use www 1.;";
-       like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
+       like ($@, qr/^www version 1 required/,
            "Comparing vs. version with decimal only"); 
 
-       if ( $] < 5.006_002 ) {
+       if ( $] < 5.006_000 ) {
            unlink 'www.pm';
-           skip 'Cannot "use" extended versions with Perl < 5.6.2', 3; 
+           skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; 
        }
-       eval "use lib '.'; use www 0.0.8;";
-       like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
-           "Make sure very small versions don't freak"); 
+       eval "use lib '.'; use www v0.0.8;";
+       my $regex = "^www version v0.0.8 required";
+       like ($@, qr/$regex/, "Make sure very small versions don't freak"); 
 
-       eval "use lib '.'; use www 0.0.4;";
-       unlike($@, qr/^www version 0.000004 \(v0.0.4\) required/,
-           'Succeed - required == VERSION');
-       cmp_ok ( "www"->VERSION, 'eq', '0.000004', 'No undef warnings' );
+       $regex =~ s/8/4/; # set for second test
+       eval "use lib '.'; use www v0.0.4;";
+       unlike($@, qr/$regex/, 'Succeed - required == VERSION');
+       cmp_ok ( "www"->VERSION, 'eq', '0.0.4', 'No undef warnings' );
 
        unlink 'www.pm';
     }
@@ -490,7 +488,29 @@ EOF
     unlink 'vvv.pm';
 
 SKIP: {
+       if ( $] < 5.006_000 ) {
+           skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; 
+       }
+       open F, ">uuu.pm" or die "Cannot open uuu.pm: $!\n";
+       print F <<"EOF";
+package uuu;
+\$VERSION = 1.0;
+1;
+EOF
+       close F;
+       eval "use lib '.'; use uuu 1.001;";
+       like ($@, qr/^uuu version 1.001 required/,
+           "User typed numeric so we error with numeric"); 
+       eval "use lib '.'; use uuu v1.1.0;";
+       like ($@, qr/^uuu version v1.1.0 required/,
+           "User typed extended so we error with extended"); 
+       unlink 'uuu.pm';
+    }
+
+SKIP: {
        # test locale handling
+       my $warning;
+       local $SIG{__WARN__} = sub { $warning = $_[0] };
        my $ver = 1.23;  # has to be floating point number
        my $loc;
        while (<DATA>) {
@@ -506,9 +526,38 @@ SKIP: {
        my $v = $CLASS->new($ver);
        unlike($warning,qr/Version string '1,23' contains invalid data/,
            "Process locale-dependent floating point");
-       is ($v, "1.230", "Locale doesn't apply to version objects");
+       is ($v, "1.23", "Locale doesn't apply to version objects");
        ok ($v == $ver, "Comparison to locale floating point");
     }
+
+    eval 'my $v = $CLASS->new("1._1");';
+    unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
+       "Invalid version format 1._1");
+
+    {
+       my $warning;
+       local $SIG{__WARN__} = sub { $warning = $_[0] };
+       eval 'my $v = $CLASS->new(~0);';
+       unlike($@, qr/Integer overflow in version/, "Too large version");
+       like($warning, qr/Integer overflow in version/, "Too large version");
+    }
+
+    {
+       # http://rt.cpan.org/Public/Bug/Display.html?id=30004
+       my $v1 = $CLASS->new("v0.1_1");
+       (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
+       my $v2 = $CLASS->new($v1);
+       (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
+       is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks";
+    }
+
+    {
+       # http://rt.perl.org/rt3/Ticket/Display.html?id=56606
+       my $badv = bless { version => [1,2,3] }, "version";
+       is $badv, '1.002003', "Deal with badly serialized versions from YAML";  
+       my $badv2 = bless { qv => 1, version => [1,2,3] }, "version";
+       is $badv2, 'v1.2.3', "Deal with badly serialized versions from YAML ";  
+    }
 }
 
 1;