X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F02-extension.t;h=f8e4752c7a25bbeb5c4ccc5f9f0da0385c54600f;hb=bca9865190a06f363601baceeb803d555f55ff77;hp=2f95f1561f5672c24d83b08d99654a9b15aece8d;hpb=988beb418b12b8cc4821055d79361f807c98aa36;p=gitmo%2FPackage-Stash.git diff --git a/t/02-extension.t b/t/02-extension.t index 2f95f15..f8e4752 100644 --- a/t/02-extension.t +++ b/t/02-extension.t @@ -1,8 +1,9 @@ +#!/usr/bin/env perl use strict; use warnings; - +use lib 't/lib'; use Test::More; -use Test::Exception; +use Test::Fatal; { package My::Package::Stash; @@ -13,14 +14,19 @@ use Test::Exception; use Symbol 'gensym'; - sub namespace { - $_[0]->{namespace} ||= {} + sub new { + my $class = shift; + my $self = $class->SUPER::new(@_); + $self->{namespace} = {}; + return $self; } - sub add_package_symbol { + sub namespace { shift->{namespace} } + + sub add_symbol { my ($self, $variable, $initial_value) = @_; - my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); + (my $name = $variable) =~ s/^[\$\@\%\&]//; my $glob = gensym(); *{$glob} = $initial_value if defined $initial_value; @@ -35,35 +41,35 @@ isa_ok($foo_stash, 'My::Package::Stash'); isa_ok($foo_stash, 'Package::Stash'); ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet'); -ok(!$foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees'); +ok(!$foo_stash->has_symbol('%foo'), '... the foo_stash agrees'); -lives_ok { - $foo_stash->add_package_symbol('%foo' => { one => 1 }); -} '... the %foo symbol is created succcessfully'; +is(exception { + $foo_stash->add_symbol('%foo' => { one => 1 }); +}, undef, '... the %foo symbol is created succcessfully'); ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package'); -ok($foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees'); +ok($foo_stash->has_symbol('%foo'), '... the foo_stash agrees'); -my $foo = $foo_stash->get_package_symbol('%foo'); +my $foo = $foo_stash->get_symbol('%foo'); is_deeply({ one => 1 }, $foo, '... got the right package variable back'); $foo->{two} = 2; -is($foo, $foo_stash->get_package_symbol('%foo'), '... our %foo is the same as the foo_stashs'); +is($foo, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the foo_stashs'); ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet'); -lives_ok { - $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]); -} '... created @Foo::bar successfully'; +is(exception { + $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]); +}, undef, '... created @Foo::bar successfully'); ok(!defined($Foo::{bar}), '... the @bar slot has still not been created'); ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet'); -lives_ok { - $foo_stash->add_package_symbol('%baz'); -} '... created %Foo::baz successfully'; +is(exception { + $foo_stash->add_symbol('%baz'); +}, undef, '... created %Foo::baz successfully'); ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');