-
Notifications
You must be signed in to change notification settings - Fork 135
/
wrap.t
376 lines (307 loc) · 9.56 KB
/
wrap.t
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use v6;
use Test;
use soft;
# L<S06/Wrapping>
# TODO
# nextsame, nextwith, callsame
# unwrap with no args pops the top most (is this spec?)
#
# mutating wraps -- those should be "deep", as in not touching coderefs
# but actually mutating how the coderef works.
plan 85;
my @log;
sub foo {
push @log, "foo";
}
sub wrapper {
push @log, "wrapper before";
try { callwith() };
push @log, "wrapper after";
}
sub other_wrapper () {
push @log, "wrapper2";
try { callwith() };
}
foo();
is(+@log, 1, "one event logged");
is(@log[0], "foo", "it's foo");
dies-ok { &foo.unwrap() }, 'cannot upwrap a never-wrapped sub.';
@log = ();
wrapper();
is(+@log, 2, "two events logged");
is(@log[0], "wrapper before", "wrapper before");
is(@log[1], "wrapper after", "wrapper after");
@log = ();
my $wrapped = &foo.wrap(&wrapper);
foo();
is @log.join('|'), 'wrapper before|foo|wrapper after', 'logged the correct events';
@log = ();
my $doublywrapped = &foo.wrap(&other_wrapper);
foo();
is(+@log, 4, "four events");
is(@log[0], "wrapper2", "additional wrapping takes effect");
is(@log[1], "wrapper before", "... on top of initial wrapping");
@log = ();
&foo.unwrap($doublywrapped);
foo();
is(+@log, 3, "old wrapped sub was not destroyed");
is(@log[0], "wrapper before", "the original wrapper is still in effect");
@log = ();
&foo.unwrap($wrapped);
foo();
is(+@log, 1, "one events for unwrapped (should be back to original now)");
is(@log[0], "foo", "got execpted value");
@log = ();
$wrapped = &foo.wrap(&wrapper);
$doublywrapped = &foo.wrap(&other_wrapper);
&foo.unwrap($wrapped);
foo();
is(+@log, 2, "out of order unwrapping gave right number of results");
is(@log[0], "wrapper2", "got execpted value from remaining wrapper");
is(@log[1], "foo", "got execpted value from original sub");
dies-ok { &foo.unwrap($wrapped) }, "can't re-unwrap an already unwrapped sub";
#First level wrapping
sub hi { "Hi" };
is( hi, "Hi", "Basic sub." );
my $handle;
lives-ok( { $handle = &hi.wrap({ callsame() ~ " there" }) },
"Basic wrapping works ");
ok( $handle, "Recieved handle for unwrapping." );
is( hi, "Hi there", "Function produces expected output after wrapping" );
#unwrap the handle
lives-ok { $handle = &hi.unwrap( $handle )}, "unwrap the function";
is( hi, "Hi", "Function is no longer wrapped." );
#Check 10 levels of wrapping
#useless function.
sub levelwrap($n) {
return $n;
}
# Make sure useless function does it's job.
is( levelwrap( 1 ), 1, "Sanity test." );
is( levelwrap( 2 ), 2, "Sanity test." );
#?rakudo todo 'callwith RT #124653'
lives-ok { &levelwrap.callwith( 1 )},
"Check that functions have a 'callwith' that works. ";
#?DOES 20
{
for (1..10) -> $num {
lives-ok {
&levelwrap.wrap({
callwith( $^t + 1 );
}),
" Wrapping #$num"
}, "wrapping $num";
is( levelwrap( 1 ), 1 + $num, "Checking $num level wrapping" );
}
}
#Check removal of wrap in the middle by handle.
sub functionA {
return 'z';
}
is( functionA(), 'z', "Sanity." );
my $middle;
lives-ok { $middle = &functionA.wrap(sub { return 'y' ~ callsame })},
"First wrapping lived";
is( functionA(), "yz", "Middle wrapper sanity." );
lives-ok { &functionA.wrap(sub { return 'x' ~ callsame })},
'Second wraping lived';
is( functionA(), "xyz", "three wrappers sanity." );
lives-ok { &functionA.unwrap( $middle )}, 'unwrap the middle wrapper.';
is( functionA(), "xz", "First wrapper and final function only, middle removed." );
#temporization (end scope removal of wrapping)
sub functionB {
return 'xxx';
}
{
is( functionB, "xxx", "Sanity" );
{
try {
temp &functionB.wrap({ return 'yyy' });
};
is( functionB, 'yyy', 'Check that function is wrapped.' );
}
#?rakudo todo 'temp and wrap'
is( functionB, 'xxx', "Wrap is now out of scope, should be back to normal." );
}
#?rakudo todo 'temp and wrap RT #124654'
is( functionB, 'xxx', "Wrap is now out of scope, should be back to normal." );
#?rakudo todo 'RT #70267: call to nextsame with nowhere to go'
dies-ok { {nextsame}() }, '{nextsame}() dies properly';
# RT #66658
#?niecza skip "undefined undefined"
{
sub meet( $person ) { return "meet $person" }
sub greet( $person ) { return "greet $person" }
my $wrapped;
for &greet, &meet -> $wrap {
my $name = $wrap.name;
$wrap.wrap({ $wrapped = $name; callsame; });
}
ok ! $wrapped.defined, 'wrapper test variable is undefined';
is greet('japhb'), 'greet japhb', 'wrapped greet() works';
is $wrapped, 'greet', 'wrapper sees lexical from time of wrap (greet)';
undefine $wrapped;
ok ! $wrapped.defined, 'wrapper test variable is undefined';
is meet('masak'), 'meet masak', 'wrapped meet() works';
is $wrapped, 'meet', 'wrapper sees lexical from time of wrap (meet)';
}
{
sub foo() { 1 }
my $h = &foo.wrap(-> { 1 + callsame });
is foo(), 2, 'wrap worked (sanity)';
$h.restore();
is foo(), 1, 'could unwrap by calling .restore on the handle';
}
# RT #69312
#?rakudo.jvm skip "control operator crossed continuation barrier RT #124655"
{
my @t = gather {
sub triangle { take '=' x 3; }
for reverse ^3 -> $n {
&triangle.wrap({
take '=' x $n;
callsame;
take '=' x $n;
});
}
triangle();
}
is @t.join("\n"), "\n=\n==\n===\n==\n=\n", 'multiple wrappings in a loop';
}
# RT #77472
{
multi multi-to-wrap($x) {
$x * 2;
};
&multi-to-wrap.wrap({
2 * callsame;
});
#?rakudo.jvm todo "?"
is multi-to-wrap(5), 20, 'can wrap a multi';
}
{
my $didfoo;
my role SomeTrait {
method apply_handles($attr: Mu $pkg) {
my $name = $attr.name;
my $accessor = $name.subst(/^../, '');
my $r = sub ($obj, |args) is rw {
my (|c) ::= callsame;
c;
}
$pkg.^find_method($accessor).wrap($r);
}
method foo { $didfoo++ }
}
multi trait_mod:<is>(Attribute $var, :$wtf!) {
die "Must have accessor" unless $var.has-accessor;
$var.set_rw;
$var does SomeTrait;
$var.foo;
}
my class foo {
has $.x is rw is wtf = 16;
}
ok $didfoo, "Did foo, capture return";
my $foo = foo.new; # x = 16;
my $bar = foo.new(x => 32);
#?rakudo.moar 2 todo 'RT #122259'
#?rakudo.jvm 3 skip 'RT #122259'
is $foo.x, 16, "default works with wrapped accessor, capture return";
is $bar.x, 32, "BUILD binding works with wrapped accessor, capture return";
try $bar.x = 64;
is $bar.x, 64, "assignment works with wrapped accessor, capture return";
}
{
my $didfoo;
my role SomeTrait {
method apply_handles($attr: Mu $pkg) {
my $name = $attr.name;
my $accessor = $name.subst(/^../, '');
my $r = sub ($obj, |args) is rw {
return callsame;
}
$pkg.^find_method($accessor).wrap($r);
}
method foo { $didfoo++ }
}
multi trait_mod:<is>(Attribute $var, :$wtf!) {
die "Must have accessor" unless $var.has-accessor;
$var.set_rw;
$var does SomeTrait;
$var.foo;
}
my class foo {
has $.x is rw is wtf = 16;
}
ok $didfoo, "Did foo, return callsame";
my $foo = foo.new; # x = 16;
my $bar = foo.new(x => 32);
is $foo.x, 16, "default works with wrapped accessor, return callsame";
is $bar.x, 32, "BUILD binding works with wrapped accessor, return callsame";
try $bar.x = 64;
#?rakudo todo 'RT #122259'
is $bar.x, 64, "assignment works with wrapped accessor, return callsame";
}
{
my $didfoo;
my role SomeTrait {
method apply_handles($attr: Mu $pkg) {
my $name = $attr.name;
my $accessor = $name.subst(/^../, '');
my $r = sub ($obj, |args) is rw {
callsame;
}
$pkg.^find_method($accessor).wrap($r);
}
method foo { $didfoo++ }
}
multi trait_mod:<is>(Attribute $var, :$wtf!) {
die "Must have accessor" unless $var.has-accessor;
$var.set_rw;
$var does SomeTrait;
$var.foo;
}
my class foo {
has $.x is rw is wtf = 16;
}
ok $didfoo, "Did foo, callsame";
my $foo = foo.new; # x = 16;
my $bar = foo.new(x => 32);
is $foo.x, 16, "default works with wrapped accessor, callsame";
is $bar.x, 32, "BUILD binding works with wrapped accessor, callsame";
try $bar.x = 64;
is $bar.x, 64, "assignment works with wrapped accessor, callsame";
}
{
my $didfoo;
my role SomeTrait {
method apply_handles($attr: Mu $pkg) {
my $name = $attr.name;
my $accessor = $name.subst(/^../, '');
my $r = sub ($obj, |args) is rw {
nextsame;
}
$pkg.^find_method($accessor).wrap($r);
}
method foo { $didfoo++ }
}
multi trait_mod:<is>(Attribute $var, :$wtf!) {
die "Must have accessor" unless $var.has-accessor;
$var.set_rw;
$var does SomeTrait;
$var.foo;
}
my class foo {
has $.x is rw is wtf = 16;
}
ok $didfoo, "Did foo, nextsame";
my $foo = foo.new; # x = 16;
my $bar = foo.new(x => 32);
is $foo.x, 16, "default works with wrapped accessor, nextsame";
is $bar.x, 32, "BUILD binding works with wrapped accessor, nextsame";
try $bar.x = 64;
#?rakudo todo 'RT #122259'
is $bar.x, 64, "assignment works with wrapped accessor, nextsame";
}
# vim: ft=perl6