modernise t/cmd/while.t
authorGerard Goossen <gerard@ggoossen.net>
Thu, 12 Nov 2009 11:42:37 +0000 (12:42 +0100)
committerZefram <zefram@fysh.org>
Sat, 8 Jan 2011 13:38:39 +0000 (13:38 +0000)
Add t/base/while.t testing the basic of a while loop with minimal
dependencies.  Change t/cmd/while.t into a non-base test using "test.pl".

(Includes bugfixes by Zefram over Gerard's original patch.)

MANIFEST
t/base/while.t [new file with mode: 0644]
t/cmd/while.t

index 34a96ba..7c011a2 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -4516,6 +4516,7 @@ t/base/num.t                      See if numbers work
 t/base/pat.t                   See if pattern matching works
 t/base/rs.t                    See if record-read works
 t/base/term.t                  See if various terms work
+t/base/while.t                 See if while work
 t/benchmark/rt26188-speed-up-keys-on-empty-hash.t      Benchmark if keys on empty hashes is fast enough
 t/cmd/elsif.t                  See if else-if works
 t/cmd/for.t                    See if for loops work
diff --git a/t/base/while.t b/t/base/while.t
new file mode 100644 (file)
index 0000000..fd37979
--- /dev/null
@@ -0,0 +1,33 @@
+#!./perl
+
+print "1..4\n";
+
+# very basic tests of while
+
+$x = 0;
+while ($x != 3) {
+    $x = $x + 1;
+}
+if ($x == 3) { print "ok 1\n"; } else { print "not ok 1\n";}
+
+$x = 0;
+while (1) {
+    $x = $x + 1;
+    last if $x == 3;
+}
+if ($x == 3) { print "ok 2\n"; } else { print "not ok 2\n";}
+
+$x = 0;
+while ($x != 3) {
+    $x = $x + 1;
+    next;
+    print "not ";
+}
+print "ok 3\n";
+
+$x = 0;
+while (0) {
+    $x = 1;
+}
+if ($x == 0) { print "ok 4\n"; } else { print "not ok 4\n";}
+
index 226db47..06ff200 100644 (file)
@@ -1,8 +1,13 @@
 #!./perl
 
-print "1..22\n";
+BEGIN {
+    require "test.pl";
+}
+
+plan(20);
 
-open (tmp,'>Cmd_while.tmp') || die "Can't create Cmd_while.tmp.";
+my $tmpfile = tempfile();
+open (tmp,'>', $tmpfile) || die "Can't create Cmd_while.tmp.";
 print tmp "tvi925\n";
 print tmp "tvi920\n";
 print tmp "vt100\n";
@@ -12,26 +17,26 @@ close tmp or die "Could not close: $!";
 
 # test "last" command
 
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh, $tmpfile) || die "Can't open Cmd_while.tmp.";
 while (<fh>) {
     last if /vt100/;
 }
-if (!eof && /vt100/) {print "ok 1\n";} else {print "not ok 1 $_\n";}
+ok(!eof && /vt100/);
 
 # test "next" command
 
 $bad = '';
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh, $tmpfile) || die "Can't open Cmd_while.tmp.";
 while (<fh>) {
     next if /vt100/;
     $bad = 1 if /vt100/;
 }
-if (!eof || /vt100/ || $bad) {print "not ok 2\n";} else {print "ok 2\n";}
+ok(eof && !/vt100/ && !$bad);
 
 # test "redo" command
 
 $bad = '';
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
 while (<fh>) {
     if (s/vt100/VT100/g) {
        s/VT100/Vt100/g;
@@ -40,41 +45,41 @@ while (<fh>) {
     $bad = 1 if /vt100/;
     $bad = 1 if /VT100/;
 }
-if (!eof || $bad) {print "not ok 3\n";} else {print "ok 3\n";}
+ok(eof && !$bad);
 
 # now do the same with a label and a continue block
 
 # test "last" command
 
 $badcont = '';
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
 line: while (<fh>) {
     if (/vt100/) {last line;}
 } continue {
     $badcont = 1 if /vt100/;
 }
-if (!eof && /vt100/) {print "ok 4\n";} else {print "not ok 4\n";}
-if (!$badcont) {print "ok 5\n";} else {print "not ok 5\n";}
+ok(!eof && /vt100/);
+ok(!$badcont);
 
 # test "next" command
 
 $bad = '';
 $badcont = 1;
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
 entry: while (<fh>) {
     next entry if /vt100/;
     $bad = 1 if /vt100/;
 } continue {
     $badcont = '' if /vt100/;
 }
-if (!eof || /vt100/ || $bad) {print "not ok 6\n";} else {print "ok 6\n";}
-if (!$badcont) {print "ok 7\n";} else {print "not ok 7\n";}
+ok(eof && !/vt100/ && !$bad);
+ok(!$badcont);
 
 # test "redo" command
 
 $bad = '';
 $badcont = '';
-open(fh,'Cmd_while.tmp') || die "Can't open Cmd_while.tmp.";
+open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
 loop: while (<fh>) {
     if (s/vt100/VT100/g) {
        s/VT100/Vt100/g;
@@ -85,95 +90,88 @@ loop: while (<fh>) {
 } continue {
     $badcont = 1 if /vt100/;
 }
-if (!eof || $bad) {print "not ok 8\n";} else {print "ok 8\n";}
-if (!$badcont) {print "ok 9\n";} else {print "not ok 9\n";}
+ok(eof && !$bad);
+ok(!$badcont);
 
 close(fh) || die "Can't close Cmd_while.tmp.";
-unlink 'Cmd_while.tmp' || `/bin/rm Cmd_While.tmp`;
-
-#$x = 0;
-#while (1) {
-#    if ($x > 1) {last;}
-#    next;
-#} continue {
-#    if ($x++ > 10) {last;}
-#    next;
-#}
-#
-#if ($x < 10) {print "ok 10\n";} else {print "not ok 10\n";}
 
 $i = 9;
 {
     $i++;
 }
-print "ok $i\n";
+is($i, 10);
 
 # Check curpm is reset when jumping out of a scope
+$i = 0;
 'abc' =~ /b/;
 WHILE:
 while (1) {
   $i++;
-  print "#$`,$&,$',\nnot " unless $` . $& . $' eq "abc";
-  print "ok $i\n";
+  is($` . $& . $', "abc");
   {                             # Localize changes to $` and friends
     'end' =~ /end/;
-    redo WHILE if $i == 11;
-    next WHILE if $i == 12;
-    # 13 do a normal loop
-    last WHILE if $i == 14;
+    redo WHILE if $i == 1;
+    next WHILE if $i == 2;
+    # 3 do a normal loop
+    last WHILE if $i == 4;
   }
 }
-$i++;
-print "not " unless $` . $& . $' eq "abc";
-print "ok $i\n";
+is($` . $& . $', "abc");
 
 # check that scope cleanup happens right when there's a continue block
 {
     my $var = 16;
+    my (@got_var, @got_i);
     while (my $i = ++$var) {
        next if $i == 17;
        last if $i > 17;
        my $i = 0;
     }
     continue {
-        print "ok ", $var-1, "\nok $i\n";
+        ($got_var, $got_i) = ($var, $i);
     }
+    is($got_var, 17);
+    is($got_i, 17);
 }
 
 {
+    my $got_l;
     local $l = 18;
     {
         local $l = 0
     }
     continue {
-        print "ok $l\n"
+        $got_l = $l;
     }
+    is($got_l, 18);
 }
 
 {
+    my $got_l;
     local $l = 19;
     my $x = 0;
     while (!$x++) {
         local $l = 0
     }
     continue {
-        print "ok $l\n"
+        $got_l = $l;
     }
+    is($got_l, $l);
 }
 
-$i = 20;
 {
+    my $ok = 1;
+    $i = 20;
     while (1) {
        my $x;
-       print $x if defined $x;
-       $x = "not ";
-       print "ok $i\n"; ++$i;
+       $ok = 0 if defined $x;
        if ($i == 21) {
            next;
        }
        last;
     }
     continue {
-        print "ok $i\n"; ++$i;
+        ++$i;
     }
+    ok($ok);
 }