broken-tests
[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');
58d75218 19ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
52e8a34c 20
21lives_ok {
58d75218 22 Foo->meta->add_package_symbol('%foo' => { one => 1 });
52e8a34c 23} '... created %Foo::foo successfully';
24
25ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
58d75218 26ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
52e8a34c 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
58d75218 34my $foo = Foo->meta->get_package_symbol('%foo');
52e8a34c 35is_deeply({ one => 1 }, $foo, '... got the right package variable back');
36
37$foo->{two} = 2;
38
39{
40 no strict 'refs';
58d75218 41 is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
52e8a34c 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 {
58d75218 50 Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
52e8a34c 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 {
58d75218 66 Foo->meta->add_package_symbol('%baz');
52e8a34c 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 {
58d75218 82 Foo->meta->add_package_symbol('@bling');
52e8a34c 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 {
58d75218 95 Foo->meta->remove_package_symbol('%foo');
52e8a34c 96} '... removed %Foo::foo successfully';
97
58d75218 98ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
52e8a34c 99
100# check some errors
101
102dies_ok {
58d75218 103 Foo->meta->add_package_symbol('bar');
52e8a34c 104} '... no sigil for bar';
105
106dies_ok {
58d75218 107 Foo->meta->remove_package_symbol('bar');
52e8a34c 108} '... no sigil for bar';
109
110dies_ok {
58d75218 111 Foo->meta->get_package_symbol('bar');
52e8a34c 112} '... no sigil for bar';
113
114dies_ok {
58d75218 115 Foo->meta->has_package_symbol('bar');
52e8a34c 116} '... no sigil for bar';
117
118
6d5355c3 119#dies_ok {
58d75218 120# Foo->meta->get_package_symbol('@.....bar');
6d5355c3 121#} '... could not fetch variable';