]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - t/58-yield-misc.t
Preserve lexicals across dounwind() calls on perl 5.19.4 and above
[perl/modules/Scope-Upper.git] / t / 58-yield-misc.t
index 3243272da8fcd30f8383803de941d568717ca0a4..e89e8926ceb5cbac39232c04f780d14b6e0b4780 100644 (file)
@@ -3,12 +3,12 @@
 use strict;
 use warnings;
 
-use Test::More tests => 4 * 3 + 1 + 3;
+use Test::More tests => 4 * 3 + 1 + 3 + 7;
 
 use lib 't/lib';
 use VPIT::TestHelpers;
 
-use Scope::Upper qw<yield leave HERE>;
+use Scope::Upper qw<yield leave HERE UP SUB>;
 
 # Test timely destruction of values returned from yield()
 
@@ -135,3 +135,49 @@ SKIP: {
       qr/^leave\(\) can't target a substitution context at \Q$0\E line $line/,
       'leave() cannot exit subst';
 }
+
+{
+ my @destroyed;
+
+ {
+  package Scope::Upper::TestTimelyDestruction;
+
+  sub new {
+   my ($class, $label) = @_;
+   bless { label => $label }, $class;
+  }
+
+  sub label { $_[0]->{label} }
+
+  sub DESTROY {
+   push @destroyed, $_[0]->label;
+  }
+ }
+
+ sub SU_TTD () { 'Scope::Upper::TestTimelyDestruction' }
+
+ sub foo {
+  my $r = SU_TTD->new('a');
+  my @x = (SU_TTD->new('c'), SU_TTD->new('d'));
+  yield 123, $r, SU_TTD->new('b'), @x, sub { SU_TTD->new('e') }->() => UP SUB;
+ }
+
+ sub bar {
+  foo();
+  die 'not reached';
+ }
+
+ {
+  my $desc = sub { "yielding @_ across a sub" };
+  my @res = bar();
+  is $res[0],        123, $desc->('a constant literal');
+  is $res[1]->label, 'a', $desc->('a lexical');
+  is $res[2]->label, 'b', $desc->('a temporary object');
+  is $res[3]->label, 'c', $desc->('the contents of a lexical array (1)');
+  is $res[4]->label, 'd', $desc->('the contents of a lexical array (2)');
+  is $res[5]->label, 'e', $desc->('a temporary object returned by a sub');
+ }
+
+ is_deeply \@destroyed, [ qw<e d c b a> ],
+                                    'all these objects were properly destroyed';
+}