summaryrefslogtreecommitdiffstats
path: root/t/002-extension.t
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-05-10 22:01:03 -0500
committerJesse Luehrs <doy@tozt.net>2010-05-10 23:43:21 -0500
commitf10f6217cffc0c5dea63b2962dc33a552ebd8d4f (patch)
tree6d17a16618ba6a49a6874ad38d2c44d90ec76cfa /t/002-extension.t
parentf49795887e93b1ef649e65c9c3e0bd3188400303 (diff)
downloadpackage-stash-f10f6217cffc0c5dea63b2962dc33a552ebd8d4f.tar.gz
package-stash-f10f6217cffc0c5dea63b2962dc33a552ebd8d4f.zip
initial import of code from Class::MOP::Package
Diffstat (limited to 't/002-extension.t')
-rw-r--r--t/002-extension.t70
1 files changed, 70 insertions, 0 deletions
diff --git a/t/002-extension.t b/t/002-extension.t
new file mode 100644
index 0000000..4a9e7c1
--- /dev/null
+++ b/t/002-extension.t
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+
+{
+ package My::Stash::Manip;
+ use strict;
+ use warnings;
+
+ use base 'Stash::Manip';
+
+ use Symbol 'gensym';
+
+ sub namespace {
+ $_[0]->{namespace} ||= {}
+ }
+
+ sub add_package_symbol {
+ my ($self, $variable, $initial_value) = @_;
+
+ my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
+
+ my $glob = gensym();
+ *{$glob} = $initial_value if defined $initial_value;
+ $self->namespace->{$name} = *{$glob};
+ }
+}
+
+# No actually package Foo exists :)
+my $foo_stash = My::Stash::Manip->new('Foo');
+
+isa_ok($foo_stash, 'My::Stash::Manip');
+isa_ok($foo_stash, 'Stash::Manip');
+
+ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
+ok(!$foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees');
+
+lives_ok {
+ $foo_stash->add_package_symbol('%foo' => { one => 1 });
+} '... 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');
+
+my $foo = $foo_stash->get_package_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');
+
+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';
+
+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';
+
+ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');
+
+done_testing;