use consistent sorting, so memoization works properly
[gitmo/Eval-Closure.git] / t / 02-close-over.t
CommitLineData
460a4d15 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More;
f3c27658 5use Test::Exception;
460a4d15 6
7use Eval::Closure;
8
9use 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
f3c27658 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";
460a4d15 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
51done_testing;