die if the source doesn't return a subroutine reference
[gitmo/Eval-Closure.git] / t / 02-close-over.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5
6 use Eval::Closure;
7
8 use Test::Requires 'PadWalker';
9
10 {
11     my $foo = [];
12     my $env = { '$foo' => \$foo };
13
14     my $code = eval_closure(
15         source      => 'sub { push @$foo, @_ }',
16         environment => $env,
17     );
18     is_deeply(scalar(PadWalker::closed_over($code)), $env,
19               "closed over the right things");
20 }
21
22 {
23     my $foo = {};
24     my $bar = [];
25     my $env = { '$foo' => \$bar, '$bar' => \$foo };
26
27     my $code = eval_closure(
28         source      => 'sub { push @$foo, @_; $bar->{foo} = \@_ }',
29         environment => $env,
30     );
31     is_deeply(scalar(PadWalker::closed_over($code)), $env,
32               "closed over the right things");
33 }
34
35 {
36     local $TODO = "we still have to close over \$__captures";
37     my $foo = [];
38     my $env = { '$foo' => \$foo };
39
40     my $code = eval_closure(
41         source      => 'sub { push @$foo, @_; return $__captures }',
42         environment => $env,
43     );
44     is_deeply(scalar(PadWalker::closed_over($code)), $env,
45               "closed over the right things");
46 }
47
48 # it'd be nice if we could test that closing over other things wasn't possible,
49 # but perl's optimizer gets in the way of that
50
51 done_testing;