more while tests
authorGerard Goossen <gerard@ggoossen.net>
Thu, 16 Dec 2010 21:12:33 +0000 (22:12 +0100)
committerZefram <zefram@fysh.org>
Sat, 8 Jan 2011 13:38:39 +0000 (13:38 +0000)
Add some while tests, about the context of the last statement in a block
and about reinitializaiton of lexical variables.

t/cmd/while.t

index 06ff200..5107fe2 100644 (file)
@@ -4,7 +4,7 @@ BEGIN {
     require "test.pl";
 }
 
-plan(20);
+plan(25);
 
 my $tmpfile = tempfile();
 open (tmp,'>', $tmpfile) || die "Can't create Cmd_while.tmp.";
@@ -175,3 +175,40 @@ is($` . $& . $', "abc");
     }
     ok($ok);
 }
+
+sub save_context { $_[0] = wantarray; $_[1] }
+
+{
+    my $context = -1;
+    my $p = sub {
+        my $x = 1;
+        while ($x--) {
+            save_context($context, "foo");
+        }
+    };
+    is(scalar($p->()), 0);
+    is($context, undef, "last statement in while block has 'void' context");
+}
+
+{
+    my $context = -1;
+    my $p = sub {
+        my $x = 1;
+        {
+            save_context($context, "foo");
+        }
+    };
+    is(scalar($p->()), "foo");
+    is($context, "", "last statement in block has 'scalar' context");
+}
+
+{
+    # test scope is cleaned
+    my $i = 0;
+    my @a;
+    while ($i++ < 2) {
+        my $x;
+        push @a, \$x;
+    }
+    ok($a[0] ne $a[1]);
+}