Commit | Line | Data |
efb592ef |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
9736bf12 |
5 | use Test::Exception; |
efb592ef |
6 | |
ce19c70b |
7 | use Eval::Closure; |
efb592ef |
8 | |
9736bf12 |
9 | { |
10 | my $code = eval_closure( |
11 | source => 'sub { die "called\n" }', |
12 | ); |
13 | ok($code, "got something"); |
efb592ef |
14 | |
9736bf12 |
15 | throws_ok { $code->() } qr/^called$/, "got the right thing"; |
16 | } |
efb592ef |
17 | |
9736bf12 |
18 | { |
19 | my $foo = []; |
efb592ef |
20 | |
9736bf12 |
21 | my $code = eval_closure( |
22 | source => 'sub { push @$bar, @_ }', |
23 | environment => { |
24 | '$bar' => \$foo, |
25 | }, |
26 | name => 'test', |
27 | ); |
28 | ok($code, "got something"); |
29 | |
30 | $code->(1); |
31 | |
32 | is_deeply($foo, [1], "got the right thing"); |
33 | } |
34 | |
35 | { |
36 | my $foo = [1, 2, 3]; |
37 | |
38 | my $code = eval_closure( |
39 | # not sure if strict leaking into evals is intended, i think i remember |
40 | # it being changed in newer perls |
41 | source => 'do { no strict; sub { $foo } }', |
42 | ); |
43 | |
44 | ok($code, "got something"); |
45 | |
46 | ok(!$code->(), "environment is clean"); |
47 | } |
efb592ef |
48 | |
49 | done_testing; |