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