changelog and version bump
[gitmo/Package-Stash-PP.git] / t / 002-extension.t
CommitLineData
f10f6217 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7{
8 package My::Stash::Manip;
9 use strict;
10 use warnings;
11
12 use base 'Stash::Manip';
13
14 use Symbol 'gensym';
15
16 sub namespace {
17 $_[0]->{namespace} ||= {}
18 }
19
20 sub add_package_symbol {
21 my ($self, $variable, $initial_value) = @_;
22
23 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
24
25 my $glob = gensym();
26 *{$glob} = $initial_value if defined $initial_value;
27 $self->namespace->{$name} = *{$glob};
28 }
29}
30
31# No actually package Foo exists :)
32my $foo_stash = My::Stash::Manip->new('Foo');
33
34isa_ok($foo_stash, 'My::Stash::Manip');
35isa_ok($foo_stash, 'Stash::Manip');
36
37ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
38ok(!$foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees');
39
40lives_ok {
41 $foo_stash->add_package_symbol('%foo' => { one => 1 });
42} '... the %foo symbol is created succcessfully';
43
44ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
45ok($foo_stash->has_package_symbol('%foo'), '... the foo_stash agrees');
46
47my $foo = $foo_stash->get_package_symbol('%foo');
48is_deeply({ one => 1 }, $foo, '... got the right package variable back');
49
50$foo->{two} = 2;
51
52is($foo, $foo_stash->get_package_symbol('%foo'), '... our %foo is the same as the foo_stashs');
53
54ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
55
56lives_ok {
57 $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]);
58} '... created @Foo::bar successfully';
59
60ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
61
62ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
63
64lives_ok {
65 $foo_stash->add_package_symbol('%baz');
66} '... created %Foo::baz successfully';
67
68ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');
69
70done_testing;