document get_all_symbols
[gitmo/Package-Stash-XS.git] / t / 04-get.t
CommitLineData
d2d3faf4 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More;
5
e94260da 6use Package::Stash;
fca4ed0c 7use Scalar::Util;
d2d3faf4 8
9{
a1c10d3a 10 BEGIN {
dc378b60 11 my $stash = Package::Stash->new('Hash');
15c104e2 12 my $val = $stash->get_symbol('%foo');
a1c10d3a 13 is($val, undef, "got nothing yet");
14 }
15 {
16 no warnings 'void', 'once';
dc378b60 17 %Hash::foo;
a1c10d3a 18 }
19 BEGIN {
dc378b60 20 my $stash = Package::Stash->new('Hash');
15c104e2 21 my $val = $stash->get_symbol('%foo');
a1c10d3a 22 is(ref($val), 'HASH', "got something");
23 $val->{bar} = 1;
15c104e2 24 is_deeply($stash->get_symbol('%foo'), {bar => 1},
dc378b60 25 "got the right variable");
26 is_deeply(\%Hash::foo, {bar => 1},
27 "stash has the right variable");
a1c10d3a 28 }
d2d3faf4 29}
30
31{
a1c10d3a 32 BEGIN {
dc378b60 33 my $stash = Package::Stash->new('Array');
15c104e2 34 my $val = $stash->get_symbol('@foo');
dc378b60 35 is($val, undef, "got nothing yet");
a1c10d3a 36 }
37 {
38 no warnings 'void', 'once';
dc378b60 39 @Array::foo;
a1c10d3a 40 }
41 BEGIN {
dc378b60 42 my $stash = Package::Stash->new('Array');
15c104e2 43 my $val = $stash->get_symbol('@foo');
a1c10d3a 44 is(ref($val), 'ARRAY', "got something");
45 push @$val, 1;
15c104e2 46 is_deeply($stash->get_symbol('@foo'), [1],
dc378b60 47 "got the right variable");
48 is_deeply(\@Array::foo, [1],
49 "stash has the right variable");
50 }
51}
52
53{
54 BEGIN {
55 my $stash = Package::Stash->new('Scalar');
15c104e2 56 my $val = $stash->get_symbol('$foo');
dc378b60 57 is($val, undef, "got nothing yet");
58 }
59 {
60 no warnings 'void', 'once';
61 $Scalar::foo;
62 }
63 BEGIN {
64 my $stash = Package::Stash->new('Scalar');
15c104e2 65 my $val = $stash->get_symbol('$foo');
dc378b60 66 is(ref($val), 'SCALAR', "got something");
67 $$val = 1;
15c104e2 68 is_deeply($stash->get_symbol('$foo'), \1,
dc378b60 69 "got the right variable");
70 is($Scalar::foo, 1,
71 "stash has the right variable");
72 }
73}
74
75{
76 BEGIN {
77 my $stash = Package::Stash->new('Code');
15c104e2 78 my $val = $stash->get_symbol('&foo');
dc378b60 79 is($val, undef, "got nothing yet");
80 }
81 {
82 no warnings 'void', 'once';
83 sub Code::foo { }
84 }
85 BEGIN {
86 my $stash = Package::Stash->new('Code');
15c104e2 87 my $val = $stash->get_symbol('&foo');
dc378b60 88 is(ref($val), 'CODE', "got something");
89 is(prototype($val), undef, "got the right variable");
90 &Scalar::Util::set_prototype($val, '&');
15c104e2 91 is($stash->get_symbol('&foo'), $val,
dc378b60 92 "got the right variable");
15c104e2 93 is(prototype($stash->get_symbol('&foo')), '&',
dc378b60 94 "got the right variable");
95 is(prototype(\&Code::foo), '&',
96 "stash has the right variable");
97 }
98}
99
100{
101 BEGIN {
102 my $stash = Package::Stash->new('Io');
15c104e2 103 my $val = $stash->get_symbol('FOO');
dc378b60 104 is($val, undef, "got nothing yet");
105 }
106 {
107 no warnings 'void', 'once';
108 package Io;
109 fileno(FOO);
110 }
111 BEGIN {
112 my $stash = Package::Stash->new('Io');
15c104e2 113 my $val = $stash->get_symbol('FOO');
dc378b60 114 isa_ok($val, 'IO');
115 my $str = "foo";
116 open $val, '<', \$str;
15c104e2 117 is(readline($stash->get_symbol('FOO')), "foo",
dc378b60 118 "got the right variable");
15c104e2 119 seek($stash->get_symbol('FOO'), 0, 0);
dc378b60 120 {
121 package Io;
122 ::isa_ok(*FOO{IO}, 'IO');
123 ::is(<FOO>, "foo",
124 "stash has the right variable");
125 }
5d3589c8 126 }
127}
128
5d3589c8 129{
dc378b60 130 my $stash = Package::Stash->new('Hash::Vivify');
15c104e2 131 my $val = $stash->get_or_add_symbol('%foo');
e55803fc 132 is(ref($val), 'HASH', "got something");
133 $val->{bar} = 1;
15c104e2 134 is_deeply($stash->get_or_add_symbol('%foo'), {bar => 1},
dc378b60 135 "got the right variable");
136 no warnings 'once';
137 is_deeply(\%Hash::Vivify::foo, {bar => 1},
138 "stash has the right variable");
e55803fc 139}
140
141{
dc378b60 142 my $stash = Package::Stash->new('Array::Vivify');
15c104e2 143 my $val = $stash->get_or_add_symbol('@foo');
e55803fc 144 is(ref($val), 'ARRAY', "got something");
145 push @$val, 1;
15c104e2 146 is_deeply($stash->get_or_add_symbol('@foo'), [1],
dc378b60 147 "got the right variable");
148 no warnings 'once';
149 is_deeply(\@Array::Vivify::foo, [1],
150 "stash has the right variable");
151}
152
153{
154 my $stash = Package::Stash->new('Scalar::Vivify');
15c104e2 155 my $val = $stash->get_or_add_symbol('$foo');
dc378b60 156 is(ref($val), 'SCALAR', "got something");
157 $$val = 1;
15c104e2 158 is_deeply($stash->get_or_add_symbol('$foo'), \1,
dc378b60 159 "got the right variable");
160 no warnings 'once';
161 is($Scalar::Vivify::foo, 1,
162 "stash has the right variable");
163}
164
165{
166 BEGIN {
167 my $stash = Package::Stash->new('Io::Vivify');
15c104e2 168 my $val = $stash->get_or_add_symbol('FOO');
dc378b60 169 isa_ok($val, 'IO');
170 my $str = "foo";
171 open $val, '<', \$str;
15c104e2 172 is(readline($stash->get_symbol('FOO')), "foo",
dc378b60 173 "got the right variable");
15c104e2 174 seek($stash->get_symbol('FOO'), 0, 0);
dc378b60 175 }
176 {
177 package Io::Vivify;
178 no warnings 'once';
179 ::isa_ok(*FOO{IO}, 'IO');
180 ::is(<FOO>, "foo",
181 "stash has the right variable");
182 }
e55803fc 183}
184
d2d3faf4 185done_testing;