state $foo if 0 shouldn't warn. Spotted by Abigail.
[p5sagit/p5-mst-13.2.git] / t / op / state.t
index 6d09813..f7db804 100644 (file)
@@ -10,7 +10,7 @@ BEGIN {
 use strict;
 use feature "state";
 
-plan tests => 32;
+plan tests => 38;
 
 ok( ! defined state $uninit, q(state vars are undef by default) );
 
@@ -20,23 +20,27 @@ sub stateful {
     state $x;
     state $y = 1;
     my $z = 2;
-    return ($x++, $y++, $z++);
+    state ($t) //= 3;
+    return ($x++, $y++, $z++, $t++);
 }
 
-my ($x, $y, $z) = stateful();
+my ($x, $y, $z, $t) = stateful();
 is( $x, 0, 'uninitialized state var' );
 is( $y, 1, 'initialized state var' );
 is( $z, 2, 'lexical' );
+is( $t, 3, 'initialized state var, list syntax' );
 
-($x, $y, $z) = stateful();
+($x, $y, $z, $t) = stateful();
 is( $x, 1, 'incremented state var' );
 is( $y, 2, 'incremented state var' );
 is( $z, 2, 'reinitialized lexical' );
+is( $t, 4, 'incremented state var, list syntax' );
 
-($x, $y, $z) = stateful();
+($x, $y, $z, $t) = stateful();
 is( $x, 2, 'incremented state var' );
 is( $y, 3, 'incremented state var' );
 is( $z, 2, 'reinitialized lexical' );
+is( $t, 5, 'incremented state var, list syntax' );
 
 # in a nested block
 
@@ -105,11 +109,11 @@ is( gen_cashier()->{bal}->(), 42, '$42 in my drawer' );
 # stateless assignment to a state variable
 
 sub stateless {
-    (state $reinitme) = 42;
+    state $reinitme = 42;
     ++$reinitme;
 }
 is( stateless(), 43, 'stateless function, first time' );
-is( stateless(), 43, 'stateless function, second time' );
+is( stateless(), 44, 'stateless function, second time' );
 
 # array state vars
 
@@ -138,20 +142,19 @@ is( $xhval, 0, 'uninitialized state hash' );
 $xhval = stateful_hash();
 is( $xhval, 1, 'uninitialized state hash after one iteration' );
 
-# state declaration with a list
+# Recursion
 
-sub statelist {
-    # note that this should be a state assignment, while (state $lager, state $stout) shouldn't
-    state($lager, $stout) = (11, 22);
-    $lager++;
-    $stout++;
-    "$lager/$stout";
+sub noseworth {
+    my $level = shift;
+    state $recursed_state = 123;
+    is($recursed_state, 123, "state kept through recursion ($level)");
+    noseworth($level - 1) if $level;
 }
+noseworth(2);
 
-my $ls = statelist();
-is($ls, "12/23", 'list assignment to state scalars');
-$ls = statelist();
-{
-    local our $TODO = 'make aassign handle state vars';
-    is($ls, "13/24", 'list assignment to state scalars');
-}
+# Assignment return value
+
+sub pugnax { my $x = state $y = 42; $y++; $x; }
+
+is( pugnax(), 42, 'scalar state assignment return value' );
+is( pugnax(), 43, 'scalar state assignment return value' );