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