summaryrefslogtreecommitdiffstats
path: root/t/compiling-package.t
blob: fa27d0ed97f52a1e0b3986540446685ab819bf07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

use Eval::Closure;

{
    my $code = eval_closure(
        source => 'no strict "refs"; sub { keys %{__PACKAGE__ . "::"} }',
    );

    # defining the sub { } creates __ANON__, calling 'no strict' creates BEGIN
    my @stash_keys = grep { $_ ne '__ANON__' && $_ ne 'BEGIN' } $code->();

    is_deeply([@stash_keys], [], "compiled in an empty package");
}

{
    # the more common case where you'd run into this is imported subs
    # for instance, Bread::Board::as vs Moose::Util::TypeConstraints::as
    my $c1 = eval_closure(
        source => 'no strict "vars"; sub { ++$foo }',
    );
    my $c2 = eval_closure(
        source => 'no strict "vars"; sub { --$foo }',
    );
    is($c1->(), 1);
    is($c1->(), 2);
    is($c2->(), -1);
    is($c2->(), -2);
}

{
    my $source = 'no strict "vars"; sub { ++$foo }';
    my $c1 = eval_closure(source => $source);
    my $c2 = eval_closure(source => $source);
    is($c1->(), 1);
    is($c1->(), 2);
    is($c2->(), 1);
    is($c2->(), 2);
}

done_testing;