broken-tests
[gitmo/Class-MOP.git] / t / 080_meta_package.t
CommitLineData
6d5355c3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c20522bd 6use Test::More tests => 43;
6d5355c3 7use Test::Exception;
8
9BEGIN {
10 use_ok('Class::MOP');
11 use_ok('Class::MOP::Package');
12}
13
14{
15 package Foo;
16
17 sub meta { Class::MOP::Package->initialize('Foo') }
18}
19
20ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
58d75218 21ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
6d5355c3 22
23lives_ok {
58d75218 24 Foo->meta->add_package_symbol('%foo' => { one => 1 });
6d5355c3 25} '... created %Foo::foo successfully';
26
c20522bd 27ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
28
6d5355c3 29ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
58d75218 30ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
6d5355c3 31
32{
33 no strict 'refs';
c20522bd 34 ok(defined(*{"Foo::foo"}{HASH}), '... the %foo (HASH) slot was created successfully');
35
36 ok(!defined(*{"Foo::foo"}{SCALAR}), '... but the $foo slot was not created');
37 ok(!Foo->meta->has_package_symbol('$foo'), '... and the meta agrees');
38
39 ok(!defined(*{"Foo::foo"}{ARRAY}), '... but the @foo slot was not created');
40 ok(!Foo->meta->has_package_symbol('@foo'), '... and the meta agrees');
41
42 ok(!defined(*{"Foo::foo"}{CODE}), '... but the &foo slot was not created');
43 ok(!Foo->meta->has_package_symbol('&foo'), '... and the meta agrees');
44}
45
46{
47 no strict 'refs';
6d5355c3 48 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
49 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
50}
51
58d75218 52my $foo = Foo->meta->get_package_symbol('%foo');
6d5355c3 53is_deeply({ one => 1 }, $foo, '... got the right package variable back');
54
55$foo->{two} = 2;
56
57{
58 no strict 'refs';
58d75218 59 is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
6d5355c3 60
61 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
62 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
63}
64
65ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
66
67lives_ok {
58d75218 68 Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
6d5355c3 69} '... created @Foo::bar successfully';
70
71ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
72
73{
74 no strict 'refs';
75 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
76 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
77}
78
79# now without initial value
80
81ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
82
83lives_ok {
58d75218 84 Foo->meta->add_package_symbol('%baz');
6d5355c3 85} '... created %Foo::baz successfully';
86
87ok(defined($Foo::{baz}), '... the %baz slot was created successfully');
88
89{
90 no strict 'refs';
91 ${'Foo::baz'}{one} = 1;
92
93 ok(exists ${'Foo::baz'}{one}, '... our %baz was initialized correctly');
94 is(${'Foo::baz'}{one}, 1, '... our %baz was initialized correctly');
95}
96
97ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet');
98
99lives_ok {
58d75218 100 Foo->meta->add_package_symbol('@bling');
6d5355c3 101} '... created @Foo::bling successfully';
102
103ok(defined($Foo::{bling}), '... the @bling slot was created successfully');
104
105{
106 no strict 'refs';
107 is(scalar @{'Foo::bling'}, 0, '... our @bling was initialized correctly');
108 ${'Foo::bling'}[1] = 2;
109 is(${'Foo::bling'}[1], 2, '... our @bling was assigned too correctly');
110}
111
112lives_ok {
58d75218 113 Foo->meta->remove_package_symbol('%foo');
6d5355c3 114} '... removed %Foo::foo successfully';
115
58d75218 116ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
6d5355c3 117
c20522bd 118{
119 no strict 'refs';
120 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
121}
122
6d5355c3 123# check some errors
124
125dies_ok {
58d75218 126 Foo->meta->add_package_symbol('bar');
6d5355c3 127} '... no sigil for bar';
128
129dies_ok {
58d75218 130 Foo->meta->remove_package_symbol('bar');
6d5355c3 131} '... no sigil for bar';
132
133dies_ok {
58d75218 134 Foo->meta->get_package_symbol('bar');
6d5355c3 135} '... no sigil for bar';
136
137dies_ok {
58d75218 138 Foo->meta->has_package_symbol('bar');
6d5355c3 139} '... no sigil for bar';