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