]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/57-yield-context.t
fix unwind()
[perl/modules/Scope-Upper.git] / t / 57-yield-context.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 33;
7
8 use Scope::Upper qw<yield HERE SCOPE SUB>;
9
10 my ($res, @res);
11
12 # --- Void to void ------------------------------------------------------------
13
14 do {
15  $res = 1;
16  yield(qw<a b c> => HERE);
17  $res = 0;
18 };
19 ok $res, 'yield in void context at sub to void';
20
21 do {
22  $res = 1;
23  eval {
24   yield(qw<d e f> => SCOPE(1));
25  };
26  $res = 0;
27 };
28 ok $res, 'yield in void context at sub across eval to void';
29
30 do {
31  $res = 1;
32  for (1 .. 5) {
33   yield qw<g h i> => SCOPE(1);
34  }
35  $res = 0;
36 };
37 ok $res, 'yield in void context at sub across loop to void';
38
39 # --- Void to scalar ----------------------------------------------------------
40
41 $res = do {
42  yield(qw<a b c> => HERE);
43  return 'XXX';
44 };
45 is $res, 'c', 'yield in void context at sub to scalar';
46
47 $res = do {
48  eval {
49   yield qw<d e f> => SCOPE(1);
50  };
51  return 'XXX';
52 };
53 is $res, 'f', 'yield in void context at sub across eval to scalar';
54
55 $res = do {
56  for (1 .. 5) {
57   yield qw<g h i> => SCOPE(1);
58  }
59 };
60 is $res, 'i', 'yield in void context at sub across loop to scalar';
61
62 $res = do {
63  for (6, yield qw<j k l> => SCOPE(0)) {
64   $res = 'NO';
65  }
66  'XXX';
67 };
68 is $res, 'l', 'yield in void context at sub across loop iterator to scalar';
69
70 # --- Void to list ------------------------------------------------------------
71
72 @res = do {
73  yield qw<a b c> => HERE;
74  return 'XXX';
75 };
76 is_deeply \@res, [ qw<a b c> ], 'yield in void context at sub to list';
77
78 @res = do {
79  eval {
80   yield qw<d e f> => SCOPE(1);
81  };
82  'XXX';
83 };
84 is_deeply \@res, [ qw<d e f> ], 'yield in void context at sub across eval to list';
85
86 @res = do {
87  for (1 .. 5) {
88   yield qw<g h i> => SCOPE(1);
89  }
90 };
91 is_deeply \@res, [ qw<g h i> ], 'yield in void context at sub across loop to list';
92
93 # --- Scalar to void ----------------------------------------------------------
94
95 do {
96  $res = 1;
97  my $temp = yield(qw<a b c> => HERE);
98  $res = 0;
99 };
100 ok $res, 'yield in scalar context at sub to void';
101
102 do {
103  $res = 1;
104  my $temp = eval {
105   yield(qw<d e f> => SCOPE(1));
106  };
107  $res = 0;
108 };
109 ok $res, 'yield in scalar context at sub across eval to void';
110
111 do {
112  $res = 1;
113  for (1 .. 5) {
114   my $temp = (yield qw<g h i> => SCOPE(1));
115  }
116  $res = 0;
117 };
118 ok $res, 'yield in scalar context at sub across loop to void';
119
120 do {
121  $res = 1;
122  if (yield qw<m n o> => SCOPE(0)) {
123   $res = undef;
124  }
125  $res = 0;
126 };
127 ok $res, 'yield in scalar context at sub across test to void';
128
129 # --- Scalar to scalar --------------------------------------------------------
130
131 $res = sub {
132  1, yield(qw<a b c> => HERE);
133 }->(0);
134 is $res, 'c', 'yield in scalar context at sub to scalar';
135
136 $res = sub {
137  eval {
138   8, yield qw<d e f> => SCOPE(1);
139  };
140 }->(0);
141 is $res, 'f', 'yield in scalar context at sub across eval to scalar';
142
143 $res = sub {
144  if (yield qw<m n o> => SCOPE(0)) {
145   return 'XXX';
146  }
147 }->(0);
148 is $res, 'o', 'yield in scalar context at sub across test to scalar';
149
150 # --- Scalar to list ----------------------------------------------------------
151
152 @res = sub {
153  if (yield qw<m n o> => SCOPE(0)) {
154   return 'XXX';
155  }
156 }->(0);
157 is_deeply \@res, [ qw<m n o> ], 'yield in scalar context at sub across test to list';
158
159 # --- List to void ------------------------------------------------------------
160
161 do {
162  $res = 1;
163  my @temp = yield(qw<a b c> => HERE);
164  $res = 0;
165 };
166 ok $res, 'yield in list context at sub to void';
167
168 do {
169  $res = 1;
170  my @temp = eval {
171   yield(qw<d e f> => SCOPE(1));
172  };
173  $res = 0;
174 };
175 ok $res, 'yield in list context at sub across eval to void';
176
177 do {
178  $res = 1;
179  for (1 .. 5) {
180   my @temp = (yield qw<g h i> => SCOPE(1));
181  }
182  $res = 0;
183 };
184 ok $res, 'yield in list context at sub across loop to void';
185
186 do {
187  $res = 1;
188  for (6, yield qw<j k l> => SCOPE(0)) {
189   $res = undef;
190  }
191  $res = 0;
192 };
193 ok $res, 'yield in list context at sub across test to void';
194
195 # --- List to scalar ----------------------------------------------------------
196
197 $res = do {
198  my @temp = (1, yield(qw<a b c> => HERE));
199  'XXX';
200 };
201 is $res, 'c', 'yield in list context at sub to scalar';
202
203 $res = do {
204  my @temp = eval {
205   8, yield qw<d e f> => SCOPE(1);
206  };
207  'XXX';
208 };
209 is $res, 'f', 'yield in list context at sub across eval to scalar';
210
211 $res = do {
212  for (1 .. 5) {
213   my @temp = (7, yield qw<g h i> => SCOPE(1));
214  }
215  'XXX';
216 };
217 is $res, 'i', 'yield in list context at sub across loop to scalar';
218
219 $res = sub {
220  for (6, yield qw<j k l> => SCOPE(0)) {
221   return 'XXX';
222  }
223 }->(0);
224 is $res, 'l', 'yield in list context at sub across loop iterator to scalar';
225
226 # --- List to list ------------------------------------------------------------
227
228 @res = do {
229  2, yield qw<a b c> => HERE;
230 };
231 is_deeply \@res, [ qw<a b c> ], 'yield in list context at sub to list';
232
233 @res = do {
234  eval {
235   8, yield qw<d e f> => SCOPE(1);
236  };
237 };
238 is_deeply \@res, [ qw<d e f> ], 'yield in list context at sub across eval to list';
239
240 @res = sub {
241  for (6, yield qw<j k l> => SCOPE(0)) {
242   return 'XXX';
243  }
244 }->(0);
245 is_deeply \@res, [ qw<j k l> ], 'yield in list context at sub across loop iterator to list';
246
247 # --- Prototypes --------------------------------------------------------------
248
249 sub pie { 7, yield qw<pie good>, $_[0] => SUB }
250
251 sub wlist (@) { return @_ }
252
253 $res = wlist pie 1;
254 is $res, 3, 'yield to list prototype to scalar';
255
256 @res = wlist pie 2;
257 is_deeply \@res, [ qw<pie good 2> ], 'yield to list prototype to list';
258
259 sub wscalar ($$) { return @_ }
260
261 $res = wscalar pie(6), pie(7);
262 is $res, 2, 'yield to scalar prototype to scalar';
263
264 @res = wscalar pie(8), pie(9);
265 is_deeply \@res, [ 8, 9 ], 'yield to scalar prototype to list';
266