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