]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - t/23-localize-ctl.t
Update VPIT::TestHelpers to 6ca15279
[perl/modules/Scope-Upper.git] / t / 23-localize-ctl.t
index 1a0c2a2e5688ba33d347cc2df85868b517cb871f..b72be227f4a8ca94783e56af611df612761852e0 100644 (file)
@@ -3,9 +3,9 @@
 use strict;
 use warnings;
 
-use Test::More tests => 28;
+use Test::More tests => 44 + 30;
 
-use Scope::Upper qw/localize/;
+use Scope::Upper qw<localize UP HERE>;
 
 our ($x, $y);
 
@@ -13,8 +13,76 @@ our ($x, $y);
  local $x = 1;
  {
   local $x = 2;
+  localize '$y' => 1 => HERE;
+  is $x, 2, 'last 0 [ok - x]';
+  is $y, 1, 'last 0 [ok - y]';
+  last;
+  $y = 2;
+ }
+ is $x, 1,     'last 0 [end - x]';
+ is $y, undef, 'last 0 [end - y]';
+}
+
+{
+ local $x = 1;
+LOOP:
+ {
+  local $x = 2;
+  local $y = 0;
   {
-   localize '$y' => 1 => 2;
+   local $x = 3;
+   localize '$y' => 1 => UP;
+   is $x, 3, 'last 1 [ok - x]';
+   is $y, 0, 'last 1 [ok - y]';
+   last LOOP;
+   $y = 3;
+  }
+  $y = 2;
+ }
+ is $x, 1,     'last 1 [end - x]';
+ is $y, undef, 'last 1 [end - y]';
+}
+
+{
+ local $x = 1;
+ {
+  local $x = 2;
+  localize '$y' => 1 => HERE;
+  is $x, 2, 'next 0 [ok - x]';
+  is $y, 1, 'next 0 [ok - y]';
+  next;
+  $y = 2;
+ }
+ is $x, 1,     'next 0 [end - x]';
+ is $y, undef, 'next 0 [end - y]';
+}
+
+{
+ local $x = 1;
+LOOP:
+ {
+  local $x = 2;
+  local $y = 0;
+  {
+   local $x = 3;
+   localize '$y' => 1 => UP;
+   is $x, 3, 'next 1 [ok - x]';
+   is $y, 0, 'next 1 [ok - y]';
+   next LOOP;
+   $y = 3;
+  }
+  $y = 2;
+ }
+ is $x, 1,     'next 1 [end - x]';
+ is $y, undef, 'next 1 [end - y]';
+}
+
+{
+ local $x = 1;
+ {
+  local $x = 2;
+  {
+   localize '$y' => 1 => UP UP;
   }
   is $x, 2,     'goto 1 [not yet - x]';
   is $y, undef, 'goto 1 [not yet - y]';
@@ -37,7 +105,7 @@ $y = undef;
   {
    local $x = 3;
    {
-    localize '$y' => 1 => 3;
+    localize '$y' => 1 => UP UP UP;
    }
    is $x, 3,     'goto 2 [not yet - x]';
    is $y, undef, 'goto 2 [not yet - y]';
@@ -62,20 +130,20 @@ $y = undef;
    {
     {
      local $x = 3;
-     localize '$y' => 1 => 4;
-     is $x, 3,     'die - reap outside eval [not yet 1 - x]';
-     is $y, undef, 'die - reap outside eval [not yet 1 - y]';
+     localize '$y' => 1 => UP UP UP UP;
+     is $x, 3,     'die - localize outside eval [not yet 1 - x]';
+     is $y, undef, 'die - localize outside eval [not yet 1 - y]';
     }
-    is $x, 2,     'die - reap outside eval [not yet 2 - x]';
-    is $y, undef, 'die - reap outside eval [not yet 2 - y]';
+    is $x, 2,     'die - localize outside eval [not yet 2 - x]';
+    is $y, undef, 'die - localize outside eval [not yet 2 - y]';
     die;
    }
   };
-  is $x, 1,     'die - reap outside eval [not yet 3 - x]';
-  is $y, undef, 'die - reap outside eval [not yet 3 - y]';
+  is $x, 1,     'die - localize outside eval [not yet 3 - x]';
+  is $y, undef, 'die - localize outside eval [not yet 3 - y]';
  } # should trigger here
- is $x, 1, 'die - reap outside eval [ok - x]';
- is $y, 1, 'die - reap outside eval [ok - y]';
+ is $x, 1, 'die - localize outside eval [ok - x]';
+ is $y, 1, 'die - localize outside eval [ok - y]';
 }
 
 $y = undef;
@@ -86,17 +154,17 @@ $y = undef;
   {
    {
     local $x = 3;
-    localize '$y' => 1 => 3;
-    is $x, 3,     'die - reap at eval [not yet 1 - x]';
-    is $y, undef, 'die - reap at eval [not yet 1 - y]';
+    localize '$y' => 1 => UP UP UP;
+    is $x, 3,     'die - localize at eval [not yet 1 - x]';
+    is $y, undef, 'die - localize at eval [not yet 1 - y]';
    }
-   is $x, 2,     'die - reap at eval [not yet 2 - x]';
-   is $y, undef, 'die - reap at eval [not yet 2 - y]';
+   is $x, 2,     'die - localize at eval [not yet 2 - x]';
+   is $y, undef, 'die - localize at eval [not yet 2 - y]';
    die;
   }
  }; # should trigger here
- is $x, 1, 'die - reap at eval [ok - x]';
- is $y, 1, 'die - reap at eval [ok - y]';
+ is $x, 1, 'die - localize at eval [ok - x]';
+ is $y, 1, 'die - localize at eval [ok - y]';
 }
 
 $y = undef;
@@ -107,15 +175,172 @@ $y = undef;
   {
    {
     local $x = 3;
-    localize '$y' => 1 => 2;
-    is $x, 3,     'die - reap inside eval [not yet 1 - x]';
-    is $y, undef, 'die - reap inside eval [not yet 1 - y]';
+    localize '$y' => 1 => UP UP;
+    is $x, 3,     'die - localize inside eval [not yet 1 - x]';
+    is $y, undef, 'die - localize inside eval [not yet 1 - y]';
    }
-   is $x, 2,     'die - reap inside eval [not yet 2 - x]';
-   is $y, undef, 'die - reap inside eval [not yet 2 - y]';
+   is $x, 2,     'die - localize inside eval [not yet 2 - x]';
+   is $y, undef, 'die - localize inside eval [not yet 2 - y]';
    die;
   } # should trigger here
  };
- is $x, 1,     'die - reap inside eval [ok - x]';
- is $y, undef, 'die - reap inside eval [ok - y]';
+ is $x, 1,     'die - localize inside eval [ok - x]';
+ is $y, undef, 'die - localize inside eval [ok - y]';
+}
+
+SKIP:
+{
+ skip 'Perl 5.10 required to test given/when' => 30 if "$]" < 5.010;
+
+ eval <<' GIVEN_TEST_1';
+  BEGIN {
+   if ("$]" >= 5.017_011) {
+    require warnings;
+    warnings->unimport('experimental::smartmatch');
+   }
+  }
+  use feature 'switch';
+  local $y;
+  {
+   local $x = 1;
+   given (1) {
+    local $x = 2;
+    when (1) {
+     local $x = 3;
+     localize '$y' => 1 => UP UP;
+     is $x, 3,     'given/when - localize at given [not yet - x]';
+     is $y, undef, 'given/when - localize at given [not yet - y]';
+    }
+    fail 'not reached';
+   }
+   is $x, 1, 'given/when - localize at given [ok - x]';
+   is $y, 1, 'given/when - localize at given [ok - y]';
+  }
+ GIVEN_TEST_1
+ fail $@ if $@;
+
+ eval <<' GIVEN_TEST_2';
+  BEGIN {
+   if ("$]" >= 5.017_011) {
+    require warnings;
+    warnings->unimport('experimental::smartmatch');
+   }
+  }
+  use feature 'switch';
+  local $y;
+  {
+   local $x = 1;
+   given (1) {
+    local $x = 2;
+    when (1) {
+     local $x = 3;
+     localize '$y' => 1 => UP UP;
+     is $x, 3,     'given/when/continue - localize at given [not yet 1 - x]';
+     is $y, undef, 'given/when/continue - localize at given [not yet 1 - y]';
+     continue;
+    }
+    is $x, 2,     'given/when/continue - localize at given [not yet 2 - x]';
+    is $y, undef, 'given/when/continue - localize at given [not yet 2 - y]';
+   }
+   is $x, 1, 'given/when/continue - localize at given [ok - x]';
+   is $y, 1, 'given/when/continue - localize at given [ok - y]';
+  }
+ GIVEN_TEST_2
+ fail $@ if $@;
+
+ eval <<' GIVEN_TEST_3';
+  BEGIN {
+   if ("$]" >= 5.017_011) {
+    require warnings;
+    warnings->unimport('experimental::smartmatch');
+   }
+  }
+  use feature 'switch';
+  local $y;
+  {
+   local $x = 1;
+   given (1) {
+    local $x = 2;
+    default {
+     local $x = 3;
+     localize '$y' => 1 => UP UP;
+     is $x, 3,     'given/default - localize at given [not yet - x]';
+     is $y, undef, 'given/default - localize at given [not yet - y]';
+    }
+    fail 'not reached';
+   }
+   is $x, 1, 'given/default - localize at given [ok - x]';
+   is $y, 1, 'given/default - localize at given [ok - y]';
+  }
+ GIVEN_TEST_3
+ fail $@ if $@;
+
+ eval <<' GIVEN_TEST_4';
+  BEGIN {
+   if ("$]" >= 5.017_011) {
+    require warnings;
+    warnings->unimport('experimental::smartmatch');
+   }
+  }
+  use feature 'switch';
+  local $y;
+  {
+   local $x = 1;
+   given (1) {
+    local $x = 2;
+    default {
+     local $x = 3;
+     localize '$y' => 1 => UP UP;
+     is $x, 3,     'given/default/continue - localize at given [not yet 1 - x]';
+     is $y, undef, 'given/default/continue - localize at given [not yet 1 - y]';
+     continue;
+    }
+    is $x, 2,     'given/default/continue - localize at given [not yet 2 - x]';
+    is $y, undef, 'given/default/continue - localize at given [not yet 2 - y]';
+   }
+   is $x, 1, 'given/default/continue - localize at given [ok - x]';
+   is $y, 1, 'given/default/continue - localize at given [ok - y]';
+  }
+ GIVEN_TEST_4
+ fail $@ if $@;
+
+ eval <<' GIVEN_TEST_5';
+  BEGIN {
+   if ("$]" >= 5.017_011) {
+    require warnings;
+    warnings->unimport('experimental::smartmatch');
+   }
+  }
+  use feature 'switch';
+  local $y;
+  {
+   local $x = 1;
+   given (1) {
+    local $x = 2;
+    default {
+     local $x = 3;
+     given (2) {
+      local $x = 4;
+      when (2) {
+       local $x = 5;
+       localize '$y' => 1 => UP UP UP;
+       is $x, 5,     'given/default/given/when - localize at default [not yet 1 - x]';
+       is $y, undef, 'given/default/given/when - localize at default [not yet 1 - y]';
+       continue;
+      }
+      is $x, 4,     'given/default/given/when - localize at default [not yet 2 - x]';
+      is $y, undef, 'given/default/given/when - localize at default [not yet 2 - y]';
+     }
+     is $x, 3,     'given/default/given/when - localize at default [not yet 3 - x]';
+     is $y, undef, 'given/default/given/when - localize at default [not yet 3 - y]';
+     continue;
+    }
+    is $x, 2, 'given/default/given/when - localize at default [ok 1 - x]';
+    is $y, 1, 'given/default/given/when - localize at default [ok 1 - y]';
+   }
+   is $x, 1,     'given/default/given/when - localize at default [ok 2 - x]';
+   is $y, undef, 'given/default/given/when - localize at default [ok 2 - y]';
+  }
+ GIVEN_TEST_5
+ fail $@ if $@;
 }