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