This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to version.pm 0.71, by John Peacock
[perl5.git] / t / comp / use.t
index 8e9eb8b..9df08d2 100755 (executable)
 BEGIN {
     chdir 't' if -d 't';
     @INC = '../lib';
 BEGIN {
     chdir 't' if -d 't';
     @INC = '../lib';
+    $INC{"feature.pm"} = 1; # so we don't attempt to load feature.pm
 }
 
 }
 
-print "1..28\n";
+print "1..59\n";
 
 
-my $i = 1;
-eval "use 5.000";      # implicit semicolon
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
-}
-print "ok ",$i++,"\n";
+# Can't require test.pl, as we're testing the use/require mechanism here.
 
 
-eval "use 5.000;";
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
+my $test = 1;
+
+sub _ok {
+    my ($type, $got, $expected, $name) = @_;
+
+    my $result;
+    if ($type eq 'is') {
+       $result = $got eq $expected;
+    } elsif ($type eq 'isnt') {
+       $result = $got ne $expected;
+    } elsif ($type eq 'like') {
+       $result = $got =~ $expected;
+    } else {
+       die "Unexpected type '$type'$name";
+    }
+    if ($result) {
+       if ($name) {
+           print "ok $test - $name\n";
+       } else {
+           print "ok $test\n";
+       }
+    } else {
+       if ($name) {
+           print "not ok $test - $name\n";
+       } else {
+           print "not ok $test\n";
+       }
+       my @caller = caller(2);
+       print "# Failed test at $caller[1] line $caller[2]\n";
+       print "# Got      '$got'\n";
+       if ($type eq 'is') {
+           print "# Expected '$expected'\n";
+       } elsif ($type eq 'isnt') {
+           print "# Expected not '$expected'\n";
+       } elsif ($type eq 'like') {
+           print "# Expected $expected\n";
+       }
+    }
+    $test = $test + 1;
+    $result;
 }
 }
-print "ok ",$i++,"\n";
 
 
-eval sprintf "use %.5f;", $];
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
+sub like ($$;$) {
+    _ok ('like', @_);
+}
+sub is ($$;$) {
+    _ok ('is', @_);
 }
 }
-print "ok ",$i++,"\n";
+sub isnt ($$;$) {
+    _ok ('isnt', @_);
+}
+
+eval "use 5.000";      # implicit semicolon
+is ($@, '');
 
 
+eval "use 5.000;";
+is ($@, '');
 
 
-eval sprintf "use %.5f;", $] - 0.000001;
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
-}
-print "ok ",$i++,"\n";
+eval "use 6.000;";
+like ($@, qr/Perl v6\.0\.0 required--this is only \Q$^V\E, stopped/);
 
 
-eval sprintf("use %.5f;", $] + 1);
-unless ($@) {
-    print "not ";
-}
-print "ok ",$i++,"\n";
+eval "no 6.000;";
+is ($@, '');
 
 
-eval sprintf "use %.5f;", $] + 0.00001;
-unless ($@) {
-    print "not ";
-}
-print "ok ",$i++,"\n";
+eval "no 5.000;";
+like ($@, qr/Perls since v5\.0\.0 too modern--this is \Q$^V\E, stopped/);
 
 
+eval sprintf "use %.6f;", $];
+is ($@, '');
+
+
+eval sprintf "use %.6f;", $] - 0.000001;
+is ($@, '');
+
+eval sprintf("use %.6f;", $] + 1);
+like ($@, qr/Perl v6.\d+.\d+ required--this is only \Q$^V\E, stopped/);
+
+eval sprintf "use %.6f;", $] + 0.00001;
+like ($@, qr/Perl v5.\d+.\d+ required--this is only \Q$^V\E, stopped/);
 
 { use lib }    # check that subparse saves pending tokens
 
 local $lib::VERSION = 1.0;
 
 eval "use lib 0.9";
 
 { use lib }    # check that subparse saves pending tokens
 
 local $lib::VERSION = 1.0;
 
 eval "use lib 0.9";
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
-}
-print "ok ",$i++,"\n";
+is ($@, '');
 
 eval "use lib 1.0";
 
 eval "use lib 1.0";
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
-}
-print "ok ",$i++,"\n";
+is ($@, '');
 
 eval "use lib 1.01";
 
 eval "use lib 1.01";
-unless ($@) {
-    print "not ";
-}
-print "ok ",$i++,"\n";
+isnt ($@, '');
 
 
 eval "use lib 0.9 qw(fred)";
 
 
 eval "use lib 0.9 qw(fred)";
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
-}
-print "ok ",$i++,"\n";
+is ($@, '');
 
 
-print "not " unless ($INC[0] eq "fred" || ($^O eq 'MacOS' && $INC[0] eq ":fred:"));
-print "ok ",$i++,"\n";
+if ($^O eq 'MacOS') {
+    is($INC[0], ":fred:");
+} else {
+    is($INC[0], "fred");
+}
 
 eval "use lib 1.0 qw(joe)";
 
 eval "use lib 1.0 qw(joe)";
-if ($@) {
-    print STDERR $@,"\n";
-    print "not ";
+is ($@, '');
+
+
+if ($^O eq 'MacOS') {
+    is($INC[0], ":joe:");
+} else {
+    is($INC[0], "joe");
 }
 }
-print "ok ",$i++,"\n";
 
 
-print "not " unless ($INC[0] eq "joe" || ($^O eq 'MacOS' && $INC[0] eq ":joe:"));
-print "ok ",$i++,"\n";
 
 eval "use lib 1.01 qw(freda)";
 
 eval "use lib 1.01 qw(freda)";
-unless ($@) {
-    print "not ";
-}
-print "ok ",$i++,"\n";
+isnt($@, '');
 
 
-print "not " if ($INC[0] eq "freda" || ($^O eq 'MacOS' && $INC[0] eq ":freda:"));
-print "ok ",$i++,"\n";
+if ($^O eq 'MacOS') {
+    isnt($INC[0], ":freda:");
+} else {
+    isnt($INC[0], "freda");
+}
 
 {
     local $lib::VERSION = 35.36;
     eval "use lib v33.55";
 
 {
     local $lib::VERSION = 35.36;
     eval "use lib v33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    is ($@, '');
 
     eval "use lib v100.105";
 
     eval "use lib v100.105";
-    unless ($@ =~ /lib version 100\.105 required--this is only version 35\.3/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
 
     eval "use lib 33.55";
 
     eval "use lib 33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    is ($@, '');
 
     eval "use lib 100.105";
 
     eval "use lib 100.105";
-    unless ($@ =~ /lib version 100\.105 required--this is only version 35\.3/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version 100.105 required--this is only version 35.360/);
 
     local $lib::VERSION = '35.36';
     eval "use lib v33.55";
 
     local $lib::VERSION = '35.36';
     eval "use lib v33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    like ($@, '');
 
     eval "use lib v100.105";
 
     eval "use lib v100.105";
-    unless ($@ =~ /lib version 100\.105 required--this is only version 35\.36/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
 
     eval "use lib 33.55";
 
     eval "use lib 33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    is ($@, '');
 
     eval "use lib 100.105";
 
     eval "use lib 100.105";
-    unless ($@ =~ /lib version 100\.105 required--this is only version 35\.36/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version 100.105 required--this is only version 35.360/);
 
     local $lib::VERSION = v35.36;
     eval "use lib v33.55";
 
     local $lib::VERSION = v35.36;
     eval "use lib v33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    is ($@, '');
 
     eval "use lib v100.105";
 
     eval "use lib v100.105";
-    unless ($@ =~ /lib v100\.105 required--this is only v35\.36/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version v100.105.0 required--this is only version v35\.36\.0/);
 
     eval "use lib 33.55";
 
     eval "use lib 33.55";
-    print "not " if $@;
-    print "ok ",$i++,"\n";
+    is ($@, '');
 
     eval "use lib 100.105";
 
     eval "use lib 100.105";
-    unless ($@ =~ /lib version 100\.105 required--this is only version 35\.036/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/lib version 100.105 required--this is only version 35.036000/);
 }
 
 
 }
 
 
@@ -177,9 +182,53 @@ print "ok ",$i++,"\n";
     print F "1;\n";
     close F;
     eval "use lib '.'; use xxx 3;";
     print F "1;\n";
     close F;
     eval "use lib '.'; use xxx 3;";
-    unless ($@ =~ /^xxx defines neither package nor VERSION--version check failed at/) {
-       print "not ";
-    }
-    print "ok ",$i++,"\n";
+    like ($@, qr/^xxx defines neither package nor VERSION--version check failed at/);
     unlink 'xxx.pm';
 }
     unlink 'xxx.pm';
 }
+
+my @ver = split /\./, sprintf "%vd", $^V;
+
+foreach my $index (-3..+3) {
+    foreach my $v (0, 1) {
+       my @parts = @ver;
+       if ($index) {
+           if ($index < 0) {
+               # Jiggle one of the parts down
+               --$parts[-$index - 1];
+           } else {
+               # Jiggle one of the parts up
+               ++$parts[$index - 1];
+           }
+       }
+       my $v_version = sprintf "v%d.%d.%d", @parts;
+       my $version;
+       if ($v) {
+           $version = $v_version;
+       } else {
+           $version = $parts[0] + $parts[1] / 1000 + $parts[2] / 1000000;
+       }
+
+       eval "use $version";
+       if ($index > 0) {
+           # The future
+           like ($@,
+                 qr/Perl $v_version required--this is only \Q$^V\E, stopped/,
+                 "use $version");
+       } else {
+           # The present or past
+           is ($@, '', "use $version");
+       }
+
+       eval "no $version";
+       if ($index <= 0) {
+           # The present or past
+           like ($@,
+                 qr/Perls since $v_version too modern--this is \Q$^V\E, stopped/,
+                 "no $version");
+       } else {
+           # future
+           is ($@, '', "no $version");
+       }
+    }
+}
+