add some tests for bare anon stashes
[gitmo/Package-Stash.git] / t / bare-anon.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5 use Test::Fatal;
6 use lib 't/lib';
7
8 use Package::Stash;
9 use Symbol;
10
11 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
12     if Package::Stash::BROKEN_GLOB_ASSIGNMENT;
13
14 my $anon = {};
15 my $stash = Package::Stash->new($anon);
16 # no way to bless something into a hashref yet
17 # my $obj = $anon->bless({});
18
19 {
20     my $code = sub { 'FOO' };
21     $stash->add_symbol('&foo' => $code);
22     is($stash->get_symbol('&foo'), $code);
23     # is($obj->foo, 'FOO');
24 }
25
26 {
27     local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP')
28         ? "can't inflate weird stash entries"
29         : undef;
30     $anon->{bar} = \123;
31
32     is(
33         exception {
34             my $code = $stash->get_symbol('&bar');
35             is(ref($code), 'CODE');
36             is($code->(), 123);
37
38             # is($obj->bar, 123);
39         },
40         undef
41     );
42 }
43
44 {
45     local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP')
46         ? "can't inflate weird stash entries"
47         : undef;
48     $anon->{baz} = -1;
49
50     is(
51         exception {
52             my $code = $stash->get_symbol('&baz');
53             is(ref($code), 'CODE');
54             like(
55                 exception { $code->() },
56                 qr/Undefined subroutine \&__ANON__::baz called/
57             );
58         },
59         undef
60     );
61 }
62
63 done_testing;