getting ready for a 0.07 release
[gitmo/Class-MOP.git] / t / 005_attributes.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 40;
7 use Test::Exception;
8
9 BEGIN { 
10     use_ok('Class::MOP'); 
11 }
12
13 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
14 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
15     accessor => 'bar'
16 ));
17 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
18     reader => 'get_baz',
19     writer => 'set_baz',    
20 ));
21
22 my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
23
24 {
25     package Foo;
26     use metaclass;
27
28     my $meta = Foo->meta;
29     ::lives_ok {
30         $meta->add_attribute($FOO_ATTR);
31     } '... we added an attribute to Foo successfully';
32     ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
33     ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
34     
35     ::ok(!$meta->has_method('foo'), '... no accessor created');
36     
37     ::lives_ok {
38         $meta->add_attribute($BAR_ATTR_2);
39     } '... we added an attribute to Foo successfully';
40     ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
41     ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo'); 
42
43     ::ok(!$meta->has_method('bar'), '... no accessor created');
44 }
45 {
46     package Bar;
47     our @ISA = ('Foo');
48     
49     my $meta = Bar->meta;
50     ::lives_ok {
51         $meta->add_attribute($BAR_ATTR);
52     } '... we added an attribute to Bar successfully';
53     ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
54     ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
55
56     ::ok($meta->has_method('bar'), '... an accessor has been created');
57     ::isa_ok($meta->get_method('bar'), 'Class::MOP::Attribute::Accessor');      
58 }
59 {
60     package Baz;
61     our @ISA = ('Bar');
62     
63     my $meta = Baz->meta;
64     ::lives_ok {
65         $meta->add_attribute($BAZ_ATTR);
66     } '... we added an attribute to Baz successfully';
67     ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');    
68     ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
69
70     ::ok($meta->has_method('get_baz'), '... a reader has been created');
71     ::ok($meta->has_method('set_baz'), '... a writer has been created');
72
73     ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Attribute::Accessor');
74     ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Attribute::Accessor');
75 }
76
77 {
78     my $meta = Baz->meta;
79     isa_ok($meta, 'Class::MOP::Class');
80     
81     is_deeply(
82         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
83         [ 
84             $BAR_ATTR,
85             $BAZ_ATTR,
86             $FOO_ATTR,                        
87         ],
88         '... got the right list of applicable attributes for Baz');
89         
90     is_deeply(
91         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
92         [ Bar->meta, Baz->meta, Foo->meta ],
93         '... got the right list of associated classes from the applicable attributes for Baz');        
94     
95     my $attr;
96     lives_ok {
97         $attr = $meta->remove_attribute('$baz');
98     } '... removed the $baz attribute successfully';
99     is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');           
100     
101     ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); 
102     is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');     
103
104     ok(!$meta->has_method('get_baz'), '... a reader has been removed');
105     ok(!$meta->has_method('set_baz'), '... a writer has been removed');
106
107     is_deeply(
108         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
109         [ 
110             $BAR_ATTR,
111             $FOO_ATTR,                        
112         ],
113         '... got the right list of applicable attributes for Baz');
114
115     is_deeply(
116         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
117         [ Bar->meta, Foo->meta ],
118         '... got the right list of associated classes from the applicable attributes for Baz');
119
120      {
121          my $attr;
122          lives_ok {
123              $attr = Bar->meta->remove_attribute('$bar');
124          } '... removed the $bar attribute successfully';
125          is($attr, $BAR_ATTR, '... got the right attribute back for Bar');           
126
127          ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); 
128
129          ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
130      }
131
132      is_deeply(
133          [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
134          [ 
135              $BAR_ATTR_2,
136              $FOO_ATTR,                        
137          ],
138          '... got the right list of applicable attributes for Baz');
139
140      is_deeply(
141          [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
142          [ Foo->meta, Foo->meta ],
143          '... got the right list of associated classes from the applicable attributes for Baz');
144
145     # remove attribute which is not there
146     my $val;
147     lives_ok {
148         $val = $meta->remove_attribute('$blammo');
149     } '... attempted to remove the non-existent $blammo attribute';
150     is($val, undef, '... got the right value back (undef)');
151
152 }