File: 58-yield-misc.t

package info (click to toggle)
libscope-upper-perl 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 536 kB
  • ctags: 78
  • sloc: perl: 6,745; makefile: 7
file content (183 lines) | stat: -rw-r--r-- 3,668 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!perl -T

use strict;
use warnings;

use Test::More tests => 4 * 3 + 1 + 3 + 7;

use lib 't/lib';
use VPIT::TestHelpers;

use Scope::Upper qw<yield leave HERE UP SUB>;

# Test timely destruction of values returned from yield()

our $destroyed;
sub guard { VPIT::TestHelpers::Guard->new(sub { ++$destroyed }) }

{
 my $desc = 'scalar context, above';
 local $destroyed;
 {
  my $obj = guard();
  my $res = do {
   is $destroyed, undef, "$desc: not yet destroyed 1";
   yield $obj => HERE;
   fail 'not reached 1';
  };
  is $destroyed, undef, "$desc: not yet destroyed 2";
 }
 is $destroyed, 1, "$desc: destroyed 1";
}

{
 my $desc = 'scalar context, below';
 local $destroyed;
 {
  my $res = do {
   my $obj = guard();
   is $destroyed, undef, "$desc: not yet destroyed 1";
   yield $obj => HERE;
   fail 'not reached 1';
  };
  is $destroyed, undef, "$desc: not yet destroyed 2";
 }
 is $destroyed, 1, "$desc: destroyed 1";
}

{
 my $desc = 'void context, above';
 local $destroyed;
 {
  my $obj = guard();
  {
   is $destroyed, undef, "$desc: not yet destroyed 1";
   yield $obj => HERE;
   fail 'not reached 1';
  }
  is $destroyed, undef, "$desc: not yet destroyed 2";
 }
 is $destroyed, 1, "$desc: destroyed 1";
}

{
 my $desc = 'void context, below';
 local $destroyed;
 {
  {
   is $destroyed, undef, "$desc: not yet destroyed 1";
   my $obj = guard();
   yield $obj => HERE;
   fail 'not reached 2';
  }
  is $destroyed, 1, "$desc: destroyed 1";
 }
 is $destroyed, 1, "$desc: destroyed 2";
}

# Test 'return from do' in special cases

{
 no warnings 'void';
 my @res = (1, do {
  my $cxt = HERE;
  my $thing = (777, do {
   my @stuff = (888, do {
    yield 2, 3 => $cxt;
    map { my $x; $_ x 3 } qw<x y z>
   }, 999);
   if (@stuff) {
    my $y;
    ++$y;
    'YYY';
   } else {
    die 'not reached';
   }
  });
  if (1) {
   my $z;
   'ZZZ';
  }
  'VVV'
 }, 4);
 is "@res", '1 2 3 4', 'yield() found the op to return to';
}

# Test leave

{
 my @res = (1, do {
  leave;
  'XXX';
 }, 2);
 is "@res", '1 2', 'leave without arguments';
}

{
 my @res = (1, do {
  leave 2, 3;
  'XXX';
 }, 4);
 is "@res", '1 2 3 4', 'leave with arguments';
}

SKIP: {
 skip '"eval { $str =~ s/./die q[foo]/e }" breaks havoc on perl 5.8 and below'
                                                           => 1 if "$]" < 5.010;
 my $s = 'a';
 local $@;
 eval {
  $s =~ s/./leave; die 'not reached'/e;
 };
 my $err  = $@;
 my $line = __LINE__-3;
 like $err,
      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';
}