add Build.PL; update MANIFEST
[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 => 50;
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 is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
25 is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
26 is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
27
28 {
29     package Foo;
30     use metaclass;
31
32     my $meta = Foo->meta;
33     ::lives_ok {
34         $meta->add_attribute($FOO_ATTR);
35     } '... we added an attribute to Foo successfully';
36     ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
37     ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
38     
39     ::ok(!$meta->has_method('foo'), '... no accessor created');
40     
41     ::lives_ok {
42         $meta->add_attribute($BAR_ATTR_2);
43     } '... we added an attribute to Foo successfully';
44     ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
45     ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo'); 
46
47     ::ok(!$meta->has_method('bar'), '... no accessor created');
48 }
49 {
50     package Bar;
51     our @ISA = ('Foo');
52     
53     my $meta = Bar->meta;
54     ::lives_ok {
55         $meta->add_attribute($BAR_ATTR);
56     } '... we added an attribute to Bar successfully';
57     ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
58     ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
59
60     my $attr = $meta->get_attribute('$bar');
61     ::is($attr->get_read_method,  'bar', '... got the right read method for Bar');
62     ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
63
64     ::ok($meta->has_method('bar'), '... an accessor has been created');
65     ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');      
66 }
67 {
68     package Baz;
69     our @ISA = ('Bar');
70     
71     my $meta = Baz->meta;
72     ::lives_ok {
73         $meta->add_attribute($BAZ_ATTR);
74     } '... we added an attribute to Baz successfully';
75     ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');    
76     ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
77
78     my $attr = $meta->get_attribute('$baz');
79     ::is($attr->get_read_method,  'get_baz', '... got the right read method for Baz');
80     ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
81
82     ::ok($meta->has_method('get_baz'), '... a reader has been created');
83     ::ok($meta->has_method('set_baz'), '... a writer has been created');
84
85     ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
86     ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
87 }
88
89 {
90     my $meta = Baz->meta;
91     isa_ok($meta, 'Class::MOP::Class');
92     
93     is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
94     is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');    
95     is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');    
96     
97     is_deeply(
98         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
99         [ 
100             $BAR_ATTR,
101             $BAZ_ATTR,
102             $FOO_ATTR,                        
103         ],
104         '... got the right list of applicable attributes for Baz');
105         
106     is_deeply(
107         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
108         [ Bar->meta, Baz->meta, Foo->meta ],
109         '... got the right list of associated classes from the applicable attributes for Baz');        
110     
111     my $attr;
112     lives_ok {
113         $attr = $meta->remove_attribute('$baz');
114     } '... removed the $baz attribute successfully';
115     is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');           
116     
117     ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); 
118     is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');     
119
120     ok(!$meta->has_method('get_baz'), '... a reader has been removed');
121     ok(!$meta->has_method('set_baz'), '... a writer has been removed');
122
123     is_deeply(
124         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
125         [ 
126             $BAR_ATTR,
127             $FOO_ATTR,                        
128         ],
129         '... got the right list of applicable attributes for Baz');
130
131     is_deeply(
132         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
133         [ Bar->meta, Foo->meta ],
134         '... got the right list of associated classes from the applicable attributes for Baz');
135
136      {
137          my $attr;
138          lives_ok {
139              $attr = Bar->meta->remove_attribute('$bar');
140          } '... removed the $bar attribute successfully';
141          is($attr, $BAR_ATTR, '... got the right attribute back for Bar');           
142
143          ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); 
144
145          ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
146      }
147
148      is_deeply(
149          [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
150          [ 
151              $BAR_ATTR_2,
152              $FOO_ATTR,                        
153          ],
154          '... got the right list of applicable attributes for Baz');
155
156      is_deeply(
157          [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
158          [ Foo->meta, Foo->meta ],
159          '... got the right list of associated classes from the applicable attributes for Baz');
160
161     # remove attribute which is not there
162     my $val;
163     lives_ok {
164         $val = $meta->remove_attribute('$blammo');
165     } '... attempted to remove the non-existent $blammo attribute';
166     is($val, undef, '... got the right value back (undef)');
167
168 }