summaryrefslogtreecommitdiffstats
path: root/t/extension.t
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2011-08-05 13:08:04 -0500
committerJesse Luehrs <doy@tozt.net>2011-08-05 13:08:04 -0500
commitadda4f43e39cff45cf4d443dd93216644fca3afa (patch)
treee05964d374b3a5a4e363f595f443eb46a058fe44 /t/extension.t
parentc3986e8ee5e47ef7e10896d991b53a3836591ef6 (diff)
downloadpackage-stash-adda4f43e39cff45cf4d443dd93216644fca3afa.tar.gz
package-stash-adda4f43e39cff45cf4d443dd93216644fca3afa.zip
remove test numbers
Diffstat (limited to 't/extension.t')
-rw-r--r--t/extension.t76
1 files changed, 76 insertions, 0 deletions
diff --git a/t/extension.t b/t/extension.t
new file mode 100644
index 0000000..f8e4752
--- /dev/null
+++ b/t/extension.t
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 't/lib';
+use Test::More;
+use Test::Fatal;
+
+{
+ package My::Package::Stash;
+ use strict;
+ use warnings;
+
+ use base 'Package::Stash';
+
+ use Symbol 'gensym';
+
+ sub new {
+ my $class = shift;
+ my $self = $class->SUPER::new(@_);
+ $self->{namespace} = {};
+ return $self;
+ }
+
+ sub namespace { shift->{namespace} }
+
+ sub add_symbol {
+ my ($self, $variable, $initial_value) = @_;
+
+ (my $name = $variable) =~ s/^[\$\@\%\&]//;
+
+ my $glob = gensym();
+ *{$glob} = $initial_value if defined $initial_value;
+ $self->namespace->{$name} = *{$glob};
+ }
+}
+
+# No actually package Foo exists :)
+my $foo_stash = My::Package::Stash->new('Foo');
+
+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_symbol('%foo'), '... the foo_stash agrees');
+
+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_symbol('%foo'), '... the foo_stash agrees');
+
+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_symbol('%foo'), '... our %foo is the same as the foo_stashs');
+
+ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
+
+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');
+
+is(exception {
+ $foo_stash->add_symbol('%baz');
+}, undef, '... created %Foo::baz successfully');
+
+ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');
+
+done_testing;