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