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