X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F04-get.t;h=4f0eb6a1154e5eefe636f4045f1f9c68c4837ba0;hb=1f5e1baa547ee18317e7a3c4208a6081b4d2dbda;hp=3176ed1ef7358b7563d5ab8962b462fe25a0ad37;hpb=5d3589c862fc8593a95ac572b8eb20e97a593a0c;p=gitmo%2FPackage-Stash.git diff --git a/t/04-get.t b/t/04-get.t index 3176ed1..4f0eb6a 100644 --- a/t/04-get.t +++ b/t/04-get.t @@ -1,14 +1,16 @@ #!/usr/bin/env perl use strict; use warnings; +use lib 't/lib'; use Test::More; use Package::Stash; +use Scalar::Util; { BEGIN { my $stash = Package::Stash->new('Hash'); - my $val = $stash->get_package_symbol('%foo'); + my $val = $stash->get_symbol('%foo'); is($val, undef, "got nothing yet"); } { @@ -17,10 +19,10 @@ use Package::Stash; } BEGIN { my $stash = Package::Stash->new('Hash'); - my $val = $stash->get_package_symbol('%foo'); + my $val = $stash->get_symbol('%foo'); is(ref($val), 'HASH', "got something"); $val->{bar} = 1; - is_deeply($stash->get_package_symbol('%foo'), {bar => 1}, + is_deeply($stash->get_symbol('%foo'), {bar => 1}, "got the right variable"); is_deeply(\%Hash::foo, {bar => 1}, "stash has the right variable"); @@ -30,7 +32,7 @@ use Package::Stash; { BEGIN { my $stash = Package::Stash->new('Array'); - my $val = $stash->get_package_symbol('@foo'); + my $val = $stash->get_symbol('@foo'); is($val, undef, "got nothing yet"); } { @@ -39,10 +41,10 @@ use Package::Stash; } BEGIN { my $stash = Package::Stash->new('Array'); - my $val = $stash->get_package_symbol('@foo'); + my $val = $stash->get_symbol('@foo'); is(ref($val), 'ARRAY', "got something"); push @$val, 1; - is_deeply($stash->get_package_symbol('@foo'), [1], + is_deeply($stash->get_symbol('@foo'), [1], "got the right variable"); is_deeply(\@Array::foo, [1], "stash has the right variable"); @@ -52,7 +54,7 @@ use Package::Stash; { BEGIN { my $stash = Package::Stash->new('Scalar'); - my $val = $stash->get_package_symbol('$foo'); + my $val = $stash->get_symbol('$foo'); is($val, undef, "got nothing yet"); } { @@ -61,10 +63,10 @@ use Package::Stash; } BEGIN { my $stash = Package::Stash->new('Scalar'); - my $val = $stash->get_package_symbol('$foo'); + my $val = $stash->get_symbol('$foo'); is(ref($val), 'SCALAR', "got something"); $$val = 1; - is_deeply($stash->get_package_symbol('$foo'), \1, + is_deeply($stash->get_symbol('$foo'), \1, "got the right variable"); is($Scalar::foo, 1, "stash has the right variable"); @@ -73,8 +75,33 @@ use Package::Stash; { BEGIN { + my $stash = Package::Stash->new('Code'); + my $val = $stash->get_symbol('&foo'); + is($val, undef, "got nothing yet"); + } + { + no warnings 'void', 'once'; + sub Code::foo { } + } + BEGIN { + my $stash = Package::Stash->new('Code'); + my $val = $stash->get_symbol('&foo'); + is(ref($val), 'CODE', "got something"); + is(prototype($val), undef, "got the right variable"); + &Scalar::Util::set_prototype($val, '&'); + is($stash->get_symbol('&foo'), $val, + "got the right variable"); + is(prototype($stash->get_symbol('&foo')), '&', + "got the right variable"); + is(prototype(\&Code::foo), '&', + "stash has the right variable"); + } +} + +{ + BEGIN { my $stash = Package::Stash->new('Io'); - my $val = $stash->get_package_symbol('FOO'); + my $val = $stash->get_symbol('FOO'); is($val, undef, "got nothing yet"); } { @@ -84,13 +111,13 @@ use Package::Stash; } BEGIN { my $stash = Package::Stash->new('Io'); - my $val = $stash->get_package_symbol('FOO'); + my $val = $stash->get_symbol('FOO'); isa_ok($val, 'IO'); my $str = "foo"; open $val, '<', \$str; - is(readline($stash->get_package_symbol('FOO')), "foo", + is(readline($stash->get_symbol('FOO')), "foo", "got the right variable"); - seek($stash->get_package_symbol('FOO'), 0, 0); + seek($stash->get_symbol('FOO'), 0, 0); { package Io; ::isa_ok(*FOO{IO}, 'IO'); @@ -100,53 +127,12 @@ use Package::Stash; } } -TODO: { - # making TODO tests at a mixture of BEGIN and runtime is irritating - my $_TODO; - BEGIN { $_TODO = "obviously I don't understand this well enough"; } - BEGIN { $TODO = $_TODO; } - $TODO = $_TODO; - BEGIN { - my $stash = Package::Stash->new('Code'); - my $val = $stash->get_package_symbol('&foo'); - is($val, undef, "got nothing yet"); - } - { - no warnings 'void', 'once'; - \&Code::foo; - } - BEGIN { - my $stash = Package::Stash->new('Code'); - my $val = $stash->get_package_symbol('&foo'); - undef $TODO; - is(ref($val), 'CODE', "got something"); - $TODO = $_TODO; - SKIP: { - eval "require PadWalker" - or skip "needs PadWalker", 1; - # avoid padwalker segfault - if (!defined($val)) { - fail("got the right variable"); - } - else { - PadWalker::set_closed_over($val, {'$x' => 1}); - is_deeply({PadWalker::closed_over($stash->get_package_symbol('&foo'))}, {'$x' => 1}, - "got the right variable"); - is_deeply({PadWalker::closed_over(\&Code::foo)}, {'$x' => 1}, - "stash has the right variable"); - } - } - } - BEGIN { undef $TODO; } - undef $TODO; -} - { my $stash = Package::Stash->new('Hash::Vivify'); - my $val = $stash->get_or_add_package_symbol('%foo'); + my $val = $stash->get_or_add_symbol('%foo'); is(ref($val), 'HASH', "got something"); $val->{bar} = 1; - is_deeply($stash->get_or_add_package_symbol('%foo'), {bar => 1}, + is_deeply($stash->get_or_add_symbol('%foo'), {bar => 1}, "got the right variable"); no warnings 'once'; is_deeply(\%Hash::Vivify::foo, {bar => 1}, @@ -155,10 +141,10 @@ TODO: { { my $stash = Package::Stash->new('Array::Vivify'); - my $val = $stash->get_or_add_package_symbol('@foo'); + my $val = $stash->get_or_add_symbol('@foo'); is(ref($val), 'ARRAY', "got something"); push @$val, 1; - is_deeply($stash->get_or_add_package_symbol('@foo'), [1], + is_deeply($stash->get_or_add_symbol('@foo'), [1], "got the right variable"); no warnings 'once'; is_deeply(\@Array::Vivify::foo, [1], @@ -167,10 +153,10 @@ TODO: { { my $stash = Package::Stash->new('Scalar::Vivify'); - my $val = $stash->get_or_add_package_symbol('$foo'); + my $val = $stash->get_or_add_symbol('$foo'); is(ref($val), 'SCALAR', "got something"); $$val = 1; - is_deeply($stash->get_or_add_package_symbol('$foo'), \1, + is_deeply($stash->get_or_add_symbol('$foo'), \1, "got the right variable"); no warnings 'once'; is($Scalar::Vivify::foo, 1, @@ -180,13 +166,13 @@ TODO: { { BEGIN { my $stash = Package::Stash->new('Io::Vivify'); - my $val = $stash->get_or_add_package_symbol('FOO'); + my $val = $stash->get_or_add_symbol('FOO'); isa_ok($val, 'IO'); my $str = "foo"; open $val, '<', \$str; - is(readline($stash->get_package_symbol('FOO')), "foo", + is(readline($stash->get_symbol('FOO')), "foo", "got the right variable"); - seek($stash->get_package_symbol('FOO'), 0, 0); + seek($stash->get_symbol('FOO'), 0, 0); } { package Io::Vivify;