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