use consistent sorting, so memoization works properly
[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 use Test::Exception;
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 {
37     my $foo = [];
38     my $env = { '$foo' => \$foo };
39
40     throws_ok {
41         my $code = eval_closure(
42             source      => 'sub { push @$foo, @_; return $__captures }',
43             environment => $env,
44         );
45     } qr/Global symbol "\$__captures/, "we don't close over \$__captures";
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;