This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert t/op/do.t to test.pl, strict and warnings.
[perl5.git] / t / op / do.t
old mode 100755 (executable)
new mode 100644 (file)
index 1d6fb90..787d632
--- a/t/op/do.t
+++ b/t/op/do.t
-#!./perl
+#!./perl -w
 
-# $RCSfile: do.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:45 $
+require './test.pl';
+use strict;
+no warnings 'void';
 
 sub foo1
 {
-    ok($_[0]);
+    ok($_[0], 'in foo1');
     'value';
 }
 
 sub foo2
 {
     shift;
-    ok($_[0]);
-    $x = 'value';
+    ok($_[0], 'in foo2');
+    my $x = 'value';
     $x;
 }
 
-my $test = 1;
-sub ok {
-    my($ok, $name) = @_;
-
-    # You have to do it this way or VMS will get confused.
-    printf "%s %d%s\n", $ok ? "ok" : "not ok", 
-                        $test,
-                        defined $name ? " - $name" : '';
-
-    printf "# Failed test at line %d\n", (caller)[2] unless $ok;
-
-    $test++;
-    return $ok;
-}
-
-print "1..20\n";
-
-# Test do &sub and proper @_ handling.
+my $result;
 $_[0] = 0;
-$result = do foo1(1);
+{
+    no warnings 'deprecated';
+    $result = do foo1(1);
+}
 
-ok( $result eq 'value',  ":$result: eq :value:" );
-ok( $_[0] == 0 );
+is($result, 'value', 'do &sub and proper @_ handling');
+cmp_ok($_[0], '==', 0, 'do &sub and proper @_ handling');
 
 $_[0] = 0;
-$result = do foo2(0,1,0);
-ok( $result eq 'value', ":$result: eq :value:" );
-ok( $_[0] == 0 );
+{
+    no warnings 'deprecated';
+    $result = do foo2(0,1,0);
+}
+is($result, 'value', 'do &sub and proper @_ handling');
+cmp_ok($_[0], '==', 0, 'do &sub and proper @_ handling');
 
-$result = do{ ok 1; 'value';};
-ok( $result eq 'value',  ":$result: eq :value:" );
+my $called;
+$result = do{ ++$called; 'value';};
+is($called, 1, 'do block called');
+is($result, 'value', 'do block returns correct value');
 
+my @blathered;
 sub blather {
-    ok 1 foreach @_;
+    push @blathered, $_ foreach @_;
 }
 
-do blather("ayep","sho nuff");
-@x = ("jeepers", "okydoke");
-@y = ("uhhuh", "yeppers");
-do blather(@x,"noofie",@y);
+{
+    no warnings 'deprecated';
+    do blather("ayep","sho nuff");
+    is("@blathered", "ayep sho nuff", 'blathered called with list');
+}
+@blathered = ();
+
+my @x = ("jeepers", "okydoke");
+my @y = ("uhhuh", "yeppers");
+{
+    no warnings 'deprecated';
+    do blather(@x,"noofie",@y);
+    is("@blathered", "@x noofie @y", 'blathered called with arrays too');
+}
 
 unshift @INC, '.';
 
-if (open(DO, ">$$.16")) {
-    print DO "ok(1, 'do in scalar context') if defined wantarray && not wantarray\n";
-    close DO;
+my $file16 = tempfile();
+if (open my $do, '>', $file16) {
+    print $do "isnt(wantarray, undef, 'do in scalar context');\n";
+    print $do "cmp_ok(wantarray, '==', 0, 'do in scalar context');\n";
+    close $do or die "Could not close: $!";
 }
 
-my $a = do "$$.16";
+my $a = do $file16; die $@ if $@;
 
-if (open(DO, ">$$.17")) {
-    print DO "ok(1, 'do in list context') if defined wantarray &&     wantarray\n";
-    close DO;
+my $file17 = tempfile();
+if (open my $do, '>', $file17) {
+    print $do "isnt(wantarray, undef, 'do in list context');\n";
+    print $do "cmp_ok(wantarray, '!=', 0, 'do in list context');\n";
+    close $do or die "Could not close: $!";
 }
 
-my @a = do "$$.17";
+my @a = do $file17; die $@ if $@;
 
-if (open(DO, ">$$.18")) {
-    print DO "ok(1, 'do in void context') if not defined wantarray\n";
-    close DO;
+my $file18 = tempfile();
+if (open my $do, '>', $file18) {
+    print $do "is(wantarray, undef, 'do in void context');\n";
+    close $do or die "Could not close: $!";
 }
 
-do "$$.18";
+do $file18; die $@ if $@;
 
 # bug ID 20010920.007
 eval qq{ do qq(a file that does not exist); };
-ok( !$@ );
+is($@, '', "do on a non-existing file, first try");
 
 eval qq{ do uc qq(a file that does not exist); };
-ok( !$@ );
+is($@, '', "do on a non-existing file, second try");
 
-eval qq{ do qq(a file that does not exist); };
-print "not " if $@;
-print "ok 19\n";
-
-eval qq{ do uc qq(a file that does not exist); };
-print "not " if $@;
-print "ok 20\n";
+# 6 must be interpreted as a file name here
+$! = 0;
+my $do6 = do 6;
+my $errno = $1;
+is($do6, undef, 'do 6 must be interpreted as a filename');
+isnt($!, 0, 'and should set $!');
 
-END {
-    1 while unlink("$$.16", "$$.17", "$$.18");
+# [perl #19545]
+my ($u, @t);
+{
+    no warnings 'uninitialized';
+    push @t, ($u = (do {} . "This should be pushed."));
 }
+is($#t, 0, "empty do result value" );
+
+my $zok = '';
+my $owww = do { 1 if $zok };
+is($owww, '', 'last is unless');
+$owww = do { 2 unless not $zok };
+is($owww, 1, 'last is if not');
+
+$zok = 'swish';
+$owww = do { 3 unless $zok };
+is($owww, 'swish', 'last is unless');
+$owww = do { 4 if not $zok };
+is($owww, '', 'last is if not');
+
+# [perl #38809]
+@a = (7);
+my $x = sub { do { return do { @a } }; 2 }->();
+is($x, 1, 'return do { } receives caller scalar context');
+@x = sub { do { return do { @a } }; 2 }->();
+is("@x", "7", 'return do { } receives caller list context');
+
+@a = (7, 8);
+$x = sub { do { return do { 1; @a } }; 3 }->();
+is($x, 2, 'return do { ; } receives caller scalar context');
+@x = sub { do { return do { 1; @a } }; 3 }->();
+is("@x", "7 8", 'return do { ; } receives caller list context');
+
+my @b = (11 .. 15);
+$x = sub { do { return do { 1; @a, @b } }; 3 }->();
+is($x, 5, 'return do { ; , } receives caller scalar context');
+@x = sub { do { return do { 1; @a, @b } }; 3 }->();
+is("@x", "7 8 11 12 13 14 15", 'return do { ; , } receives caller list context');
+
+$x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
+is($x, 5, 'return do { ; }, do { ; } receives caller scalar context');
+@x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
+is("@x", "7 8 11 12 13 14 15", 'return do { ; }, do { ; } receives caller list context');
+
+@a = (7, 8, 9);
+$x = sub { do { do { 1; return @a } }; 4 }->();
+is($x, 3, 'do { return } receives caller scalar context');
+@x = sub { do { do { 1; return @a } }; 4 }->();
+is("@x", "7 8 9", 'do { return } receives caller list context');
+
+@a = (7, 8, 9, 10);
+$x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
+is($x, 4, 'return do { do { ; } } receives caller scalar context');
+@x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
+is("@x", "7 8 9 10", 'return do { do { ; } } receives caller list context');
+
+# Do blocks created by constant folding
+# [perl #68108]
+$x = sub { if (1) { 20 } }->();
+is($x, 20, 'if (1) { $x } receives caller scalar context');
+
+@a = (21 .. 23);
+$x = sub { if (1) { @a } }->();
+is($x, 3, 'if (1) { @a } receives caller scalar context');
+@x = sub { if (1) { @a } }->();
+is("@x", "21 22 23", 'if (1) { @a } receives caller list context');
+
+$x = sub { if (1) { 0; 20 } }->();
+is($x, 20, 'if (1) { ...; $x } receives caller scalar context');
+
+@a = (24 .. 27);
+$x = sub { if (1) { 0; @a } }->();
+is($x, 4, 'if (1) { ...; @a } receives caller scalar context');
+@x = sub { if (1) { 0; @a } }->();
+is("@x", "24 25 26 27", 'if (1) { ...; @a } receives caller list context');
+
+$x = sub { if (1) { 0; 20 } else{} }->();
+is($x, 20, 'if (1) { ...; $x } else{} receives caller scalar context');
+
+@a = (24 .. 27);
+$x = sub { if (1) { 0; @a } else{} }->();
+is($x, 4, 'if (1) { ...; @a } else{} receives caller scalar context');
+@x = sub { if (1) { 0; @a } else{} }->();
+is("@x", "24 25 26 27", 'if (1) { ...; @a } else{} receives caller list context');
+
+$x = sub { if (0){} else { 0; 20 } }->();
+is($x, 20, 'if (0){} else { ...; $x } receives caller scalar context');
+
+@a = (24 .. 27);
+$x = sub { if (0){} else { 0; @a } }->();
+is($x, 4, 'if (0){} else { ...; @a } receives caller scalar context');
+@x = sub { if (0){} else { 0; @a } }->();
+is("@x", "24 25 26 27", 'if (0){} else { ...; @a } receives caller list context');
+
+done_testing();