Commit | Line | Data |
460a4d15 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
01b68b64 |
5 | use Test::Fatal; |
460a4d15 |
6 | |
7 | use Eval::Closure; |
8 | |
9 | use Test::Requires 'PadWalker'; |
10 | |
11 | { |
12 | my $foo = []; |
13 | my $env = { '$foo' => \$foo }; |
14 | |
15 | my $code = eval_closure( |
16 | source => 'sub { push @$foo, @_ }', |
17 | environment => $env, |
18 | ); |
19 | is_deeply(scalar(PadWalker::closed_over($code)), $env, |
20 | "closed over the right things"); |
21 | } |
22 | |
23 | { |
24 | my $foo = {}; |
25 | my $bar = []; |
26 | my $env = { '$foo' => \$bar, '$bar' => \$foo }; |
27 | |
28 | my $code = eval_closure( |
29 | source => 'sub { push @$foo, @_; $bar->{foo} = \@_ }', |
30 | environment => $env, |
31 | ); |
32 | is_deeply(scalar(PadWalker::closed_over($code)), $env, |
33 | "closed over the right things"); |
34 | } |
35 | |
36 | { |
460a4d15 |
37 | my $foo = []; |
38 | my $env = { '$foo' => \$foo }; |
39 | |
01b68b64 |
40 | like( |
41 | exception { |
42 | eval_closure( |
43 | source => 'sub { push @$foo, @_; return $__captures }', |
44 | environment => $env, |
45 | ); |
46 | }, |
47 | qr/Global symbol "\$__captures/, |
48 | "we don't close over \$__captures" |
49 | ); |
460a4d15 |
50 | } |
51 | |
52 | # it'd be nice if we could test that closing over other things wasn't possible, |
53 | # but perl's optimizer gets in the way of that |
54 | |
55 | done_testing; |