]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/10-reap.t
Importing Scope-Upper-0.01
[perl/modules/Scope-Upper.git] / t / 10-reap.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 4 + 8 + 8 + 18 + 4 + 8 + 6 + 11 + 5 + 17;
7
8 use Scope::Upper qw/reap/;
9
10 my $x;
11
12 sub add { local $_; my $y = $_[1]; reap sub { $x += $y }, $_[0] + 1 }
13
14 $x = 0;
15 {
16  is($x, 0, 'start');
17  {
18   add(0, 1);
19   is($x, 0, '1 didn\'t run');
20  }
21  is($x, 1, '1 ran');
22 }
23 is($x, 1, 'end');
24
25 $x = 0;
26 {
27  is($x, 0, 'start');
28  local $_ = 3;
29  is($_, 3, '$_ has the right value');
30  {
31   add(0, 1);
32   is($_, 3, '$_ has the right value');
33   local $_ = 7;
34   is($_, 7, '$_ has the right value');
35   is($x, 0, '1 didn\'t run');
36  }
37  is($x, 1, '1 ran');
38  is($_, 3, '$_ has the right value');
39 }
40 is($x, 1, 'end');
41
42 $x = 0;
43 {
44  is($x, 0, 'start');
45  {
46   add(0, 1);
47   is($x, 0, '1 didn\'t run');
48   {
49    add(0, 2);
50    is($x, 0, '1 and 2 didn\'t run');
51   }
52   is($x, 2, '1 didn\'t run, 2 ran');
53   {
54    add(0, 4);
55    is($x, 2, '1 and 3 didn\'t run, 2 ran');
56   }
57   is($x, 6, '1 didn\'t run, 2 and 3 ran');
58  }
59  is($x, 7, '1, 2 and 3 ran');
60 }
61 is($x, 7, 'end');
62
63 $x = 0;
64 {
65  is($x, 0, 'start');
66  local $_ = 3;
67  is($_, 3, '$_ has the right value');
68  {
69   add(0, 1);
70   is($_, 3, '$_ has the right value');
71   local $_ = 5;
72   is($x, 0, '1 didn\'t run');
73   is($_, 5, '$_ has the right value');
74   {
75    add(0, 2);
76    is($_, 5, '$_ has the right value');
77    local $_ = 7;
78    is($_, 7, '$_ has the right value');
79    is($x, 0, '1 and 2 didn\'t run');
80   }
81   is($x, 2, '1 didn\'t run, 2 ran');
82   is($_, 5, '$_ has the right value');
83   {
84    local $_ = 9;
85    is($_, 9, '$_ has the right value');
86    add(0, 4);
87    local $_ = 11;
88    is($_, 11, '$_ has the right value');
89    is($x, 2, '1 and 3 didn\'t run, 2 ran');
90   }
91   is($x, 6, '1 didn\'t run, 2 and 3 ran');
92   is($_, 5, '$_ has the right value');
93  }
94  is($x, 7, '1, 2 and 3 ran');
95  is($_, 3, '$_ has the right value');
96 }
97 is($x, 7, 'end');
98
99 $x = 0;
100 {
101  is($x, 0, 'start');
102  {
103   add(0, 1);
104   add(0, 2);
105   is($x, 0, '1 and 2 didn\'t run');
106  }
107  is($x, 3, '1 and 2 ran');
108 }
109 is($x, 3, 'end');
110
111 $x = 0;
112 {
113  is($x, 0, 'start');
114  local $_ = 3;
115  {
116   local $_ = 5;
117   add(0, 1);
118   is($_, 5, '$_ has the right value');
119   local $_ = 7;
120   add(0, 2);
121   is($_, 7, '$_ has the right value');
122   is($x, 0, '1 and 2 didn\'t run');
123   local $_ = 9;
124   is($_, 9, '$_ has the right value');
125  }
126  is($x, 3, '1 and 2 ran');
127  is($_, 3, '$_ has the right value');
128 }
129 is($x, 3, 'end');
130
131 $x = 0;
132 {
133  is($x, 0, 'start');
134  {
135   {
136    {
137     add(2, 1);
138     is($x, 0, '1 didn\'t run');
139    }
140    is($x, 0, '1 didn\'t run');
141   }
142   is($x, 0, '1 didn\'t run');
143  }
144  is($x, 1, '1 ran');
145 }
146 is($x, 1, 'end');
147
148 $x = 0;
149 {
150  is($x, 0, 'start');
151  {
152   {
153    add(1, 1);
154    is($x, 0, '1 didn\'t run');
155   }
156   is($x, 0, '1 didn\'t run');
157  }
158  is($x, 1, '1 ran');
159  { 
160   {
161    {
162     add(2, 2);
163     is($x, 1, '2 didn\'t run');
164    }
165    is($x, 1, '2 didn\'t run');
166    {
167     add(1, 4);
168     is($x, 1, '2 and 3 didn\'t run');
169    }
170    is($x, 1, '2 and 3 didn\'t run');
171   }
172   is($x, 5, '2 didn\'t run, 3 ran');
173  }
174  is($x, 7, '2 and 3 ran');
175 }
176 is($x, 7, 'end');
177
178 sub bleh { add(1, 2); }
179
180 $x = 0;
181 {
182  is($x, 0, 'start');
183  {
184   add(0, 1);
185   is($x, 0, '1 didn\'t run');
186   bleh();
187   is($x, 0, '1 didn\'t run');
188  }
189  is($x, 3, '1 ran');
190 }
191 is($x, 3, 'end');
192
193 sub bar {
194  is($_, 7, '$_ has the right value');
195  local $_ = 9;
196  add(2, 4);
197  is($_, 9, '$_ has the right value');
198  add(3, 8);
199  is($_, 9, '$_ has the right value');
200 }
201
202 sub foo {
203  local $_ = 7;
204  add(0, 2);
205  is($_, 7, '$_ has the right value');
206  is($x, 0, '1, 2 didn\'t run');
207  bar();
208  is($x, 0, '1, 2, 3, 4 didn\'t run');
209  is($_, 7, '$_ has the right value');
210  add(1, 16);
211  is($_, 7, '$_ has the right value');
212 }
213
214 $x = 0;
215 {
216  is($x, 0, 'start');
217  local $_ = 3;
218  add(0, 1);
219  is($_, 3, '$_ has the right value');
220  {
221   local $_ = 5;
222   is($_, 5, '$_ has the right value');
223   is($x, 0, '1 didn\'t run');
224   {
225    foo();
226    is($x, 2, '1, 3, 4 and 5 didn\'t run, 2 ran');
227    is($_, 5, '$_ has the right value');
228   }
229   is($x, 22, '1 and 4 didn\'t run, 2, 3 and 5 ran');
230  }
231  is($x, 30, '1 didn\'t run, 2, 3, 4 and 5 ran');
232 }
233 is($x, 31, 'end');