remove test numbers
[gitmo/Package-Stash.git] / t / extension.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use lib 't/lib';
5 use Test::More;
6 use Test::Fatal;
7
8 {
9     package My::Package::Stash;
10     use strict;
11     use warnings;
12
13     use base 'Package::Stash';
14
15     use Symbol 'gensym';
16
17     sub new {
18         my $class = shift;
19         my $self = $class->SUPER::new(@_);
20         $self->{namespace} = {};
21         return $self;
22     }
23
24     sub namespace { shift->{namespace} }
25
26     sub add_symbol {
27         my ($self, $variable, $initial_value) = @_;
28
29         (my $name = $variable) =~ s/^[\$\@\%\&]//;
30
31         my $glob = gensym();
32         *{$glob} = $initial_value if defined $initial_value;
33         $self->namespace->{$name} = *{$glob};
34     }
35 }
36
37 # No actually package Foo exists :)
38 my $foo_stash = My::Package::Stash->new('Foo');
39
40 isa_ok($foo_stash, 'My::Package::Stash');
41 isa_ok($foo_stash, 'Package::Stash');
42
43 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
44 ok(!$foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
45
46 is(exception {
47     $foo_stash->add_symbol('%foo' => { one => 1 });
48 }, undef, '... the %foo symbol is created succcessfully');
49
50 ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
51 ok($foo_stash->has_symbol('%foo'), '... the foo_stash agrees');
52
53 my $foo = $foo_stash->get_symbol('%foo');
54 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
55
56 $foo->{two} = 2;
57
58 is($foo, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the foo_stashs');
59
60 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
61
62 is(exception {
63     $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
64 }, undef, '... created @Foo::bar successfully');
65
66 ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
67
68 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
69
70 is(exception {
71     $foo_stash->add_symbol('%baz');
72 }, undef, '... created %Foo::baz successfully');
73
74 ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');
75
76 done_testing;