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