more-package-refactoring
[gitmo/Class-MOP.git] / t / 080_meta_package.t
CommitLineData
6d5355c3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 34;
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
27ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
58d75218 28ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
6d5355c3 29
30{
31 no strict 'refs';
32 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
33 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
34}
35
58d75218 36my $foo = Foo->meta->get_package_symbol('%foo');
6d5355c3 37is_deeply({ one => 1 }, $foo, '... got the right package variable back');
38
39$foo->{two} = 2;
40
41{
42 no strict 'refs';
58d75218 43 is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
6d5355c3 44
45 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
46 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
47}
48
49ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
50
51lives_ok {
58d75218 52 Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
6d5355c3 53} '... created @Foo::bar successfully';
54
55ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
56
57{
58 no strict 'refs';
59 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
60 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
61}
62
63# now without initial value
64
65ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
66
67lives_ok {
58d75218 68 Foo->meta->add_package_symbol('%baz');
6d5355c3 69} '... created %Foo::baz successfully';
70
71ok(defined($Foo::{baz}), '... the %baz slot was created successfully');
72
73{
74 no strict 'refs';
75 ${'Foo::baz'}{one} = 1;
76
77 ok(exists ${'Foo::baz'}{one}, '... our %baz was initialized correctly');
78 is(${'Foo::baz'}{one}, 1, '... our %baz was initialized correctly');
79}
80
81ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet');
82
83lives_ok {
58d75218 84 Foo->meta->add_package_symbol('@bling');
6d5355c3 85} '... created @Foo::bling successfully';
86
87ok(defined($Foo::{bling}), '... the @bling slot was created successfully');
88
89{
90 no strict 'refs';
91 is(scalar @{'Foo::bling'}, 0, '... our @bling was initialized correctly');
92 ${'Foo::bling'}[1] = 2;
93 is(${'Foo::bling'}[1], 2, '... our @bling was assigned too correctly');
94}
95
96lives_ok {
58d75218 97 Foo->meta->remove_package_symbol('%foo');
6d5355c3 98} '... removed %Foo::foo successfully';
99
58d75218 100ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
6d5355c3 101
102# check some errors
103
104dies_ok {
58d75218 105 Foo->meta->add_package_symbol('bar');
6d5355c3 106} '... no sigil for bar';
107
108dies_ok {
58d75218 109 Foo->meta->remove_package_symbol('bar');
6d5355c3 110} '... no sigil for bar';
111
112dies_ok {
58d75218 113 Foo->meta->get_package_symbol('bar');
6d5355c3 114} '... no sigil for bar';
115
116dies_ok {
58d75218 117 Foo->meta->has_package_symbol('bar');
6d5355c3 118} '... no sigil for bar';
119
120
121#dies_ok {
58d75218 122# Foo->meta->get_package_symbol('@.....bar');
6d5355c3 123#} '... could not fetch variable';