sync tests
[gitmo/Package-Stash.git] / t / edge-cases.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use lib 't/lib';
5 use Test::More;
6 use Test::Fatal;
7
8 use Package::Stash;
9
10 {
11     package Foo;
12     use constant FOO => 1;
13     use constant BAR => \1;
14     use constant BAZ => [];
15     use constant QUUX => {};
16     use constant QUUUX => sub { };
17     sub normal { }
18     sub stub;
19     sub normal_with_proto () { }
20     sub stub_with_proto ();
21
22     our $SCALAR;
23     our $SCALAR_WITH_VALUE = 1;
24     our @ARRAY;
25     our %HASH;
26 }
27
28 my $stash = Package::Stash->new('Foo');
29 { local $TODO = $] < 5.010
30       ? "undef scalars aren't visible on 5.8"
31       : undef;
32 ok($stash->has_symbol('$SCALAR'), '$SCALAR');
33 }
34 ok($stash->has_symbol('$SCALAR_WITH_VALUE'), '$SCALAR_WITH_VALUE');
35 ok($stash->has_symbol('@ARRAY'), '@ARRAY');
36 ok($stash->has_symbol('%HASH'), '%HASH');
37 is_deeply(
38     [sort $stash->list_all_symbols('CODE')],
39     [qw(BAR BAZ FOO QUUUX QUUX normal normal_with_proto stub stub_with_proto)],
40     "can see all code symbols"
41 );
42
43 $stash->add_symbol('%added', {});
44 ok(!$stash->has_symbol('$added'), '$added');
45 ok(!$stash->has_symbol('@added'), '@added');
46 ok($stash->has_symbol('%added'), '%added');
47
48 my $constant = $stash->get_symbol('&FOO');
49 is(ref($constant), 'CODE', "expanded a constant into a coderef");
50
51 # ensure get doesn't prevent subsequent vivification (not sure what the deal
52 # was here)
53 is(ref($stash->get_symbol('$glob')), '', "nothing yet");
54 is(ref($stash->get_or_add_symbol('$glob')), 'SCALAR', "got an empty scalar");
55
56 SKIP: {
57     skip "PP doesn't support anon stashes before 5.14", 4
58         if $] < 5.014 && $Package::Stash::IMPLEMENTATION eq 'PP';
59     local $TODO = "don't know how to properly inflate a stash entry";
60
61     my $anon = {}; # not using Package::Anon
62     $anon->{foo} = -1;     # stub
63     $anon->{bar} = '$&';   # stub with prototype
64     $anon->{baz} = \"foo"; # constant
65
66     my $stash = Package::Stash->new($anon);
67     is(
68         exception {
69             is(ref($stash->get_symbol('&foo')), 'CODE',
70                "stub expanded into a glob");
71             is(ref($stash->get_symbol('&bar')), 'CODE',
72                "stub with prototype expanded into a glob");
73             is(ref($stash->get_symbol('&baz')), 'CODE',
74                "constant expanded into a glob");
75         },
76         undef,
77         "can call get_symbol on weird stash entries"
78     );
79 }
80
81 {
82     my $warning;
83     local $SIG{__WARN__} = sub { $warning = $_[0] };
84     my $stash = Package::Stash->new('Bar');
85     $stash->add_symbol('&foo' => sub { });
86     $stash->add_symbol('&foo' => sub { });
87     is($warning, undef, "no redefinition warnings");
88 }
89
90 done_testing;