none of this use_ok nonsense
[gitmo/Class-MOP.git] / t / 081_meta_package_extension.t
CommitLineData
a5e51f0b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 16;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Class::MOP');
11}
12
13{
14 package My::Meta::Package;
15
16 use strict;
17 use warnings;
18
19 use Carp 'confess';
20 use Symbol 'gensym';
21
22 use base 'Class::MOP::Package';
23
24 __PACKAGE__->meta->add_attribute(
1aeb4c53 25 'namespace' => (
56dcfc1a 26 reader => 'namespace',
a5e51f0b 27 default => sub { {} }
28 )
29 );
30
31 sub add_package_symbol {
32 my ($self, $variable, $initial_value) = @_;
33
34 my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable);
35
36 my $glob = gensym();
37 *{$glob} = $initial_value if defined $initial_value;
38 $self->namespace->{$name} = $glob;
39 }
40}
41
42# No actually package Foo exists :)
a5e51f0b 43my $meta = My::Meta::Package->initialize('Foo');
44
45isa_ok($meta, 'My::Meta::Package');
46isa_ok($meta, 'Class::MOP::Package');
47
48ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
49ok(!$meta->has_package_symbol('%foo'), '... the meta agrees');
50
51lives_ok {
52 $meta->add_package_symbol('%foo' => { one => 1 });
53} '... the %foo symbol is created succcessfully';
54
55ok(!defined($Foo::{foo}), '... the %foo slot has not been created in the actual Foo package');
56ok($meta->has_package_symbol('%foo'), '... the meta agrees');
57
58my $foo = $meta->get_package_symbol('%foo');
59is_deeply({ one => 1 }, $foo, '... got the right package variable back');
60
61$foo->{two} = 2;
62
63is($foo, $meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
64
65ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
66
67lives_ok {
68 $meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
69} '... created @Foo::bar successfully';
70
71ok(!defined($Foo::{bar}), '... the @bar slot has still not been created');
72
73ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
74
75lives_ok {
76 $meta->add_package_symbol('%baz');
77} '... created %Foo::baz successfully';
78
79ok(!defined($Foo::{baz}), '... the %baz slot has still not been created');
80