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