770991d94cda49037a479f9cb2bcdd7a1d53465a
[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 => 70;
7 use Test::Exception;
8
9 use Class::MOP;
10
11 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
12 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
13     accessor => 'bar'
14 ));
15 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
16     reader => 'get_baz',
17     writer => 'set_baz',
18 ));
19
20 my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
21
22 my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => (
23     accessor => 'foo',
24     builder => 'build_foo'
25 ));
26
27 is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
28 is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
29 is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
30
31 {
32     package Foo;
33     use metaclass;
34
35     my $meta = Foo->meta;
36     ::lives_ok {
37         $meta->add_attribute($FOO_ATTR);
38     } '... we added an attribute to Foo successfully';
39     ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
40     ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
41
42     ::ok(!$meta->has_method('foo'), '... no accessor created');
43
44     ::lives_ok {
45         $meta->add_attribute($BAR_ATTR_2);
46     } '... we added an attribute to Foo successfully';
47     ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
48     ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
49
50     ::ok(!$meta->has_method('bar'), '... no accessor created');
51 }
52 {
53     package Bar;
54     our @ISA = ('Foo');
55
56     my $meta = Bar->meta;
57     ::lives_ok {
58         $meta->add_attribute($BAR_ATTR);
59     } '... we added an attribute to Bar successfully';
60     ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
61     ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
62
63     my $attr = $meta->get_attribute('$bar');
64     ::is($attr->get_read_method,  'bar', '... got the right read method for Bar');
65     ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
66
67     ::ok($meta->has_method('bar'), '... an accessor has been created');
68     ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
69 }
70 {
71     package Baz;
72     our @ISA = ('Bar');
73
74     my $meta = Baz->meta;
75     ::lives_ok {
76         $meta->add_attribute($BAZ_ATTR);
77     } '... we added an attribute to Baz successfully';
78     ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
79     ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
80
81     my $attr = $meta->get_attribute('$baz');
82     ::is($attr->get_read_method,  'get_baz', '... got the right read method for Baz');
83     ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
84
85     ::ok($meta->has_method('get_baz'), '... a reader has been created');
86     ::ok($meta->has_method('set_baz'), '... a writer has been created');
87
88     ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
89     ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
90 }
91
92 {
93     my $meta = Baz->meta;
94     isa_ok($meta, 'Class::MOP::Class');
95
96     is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
97     is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');
98     is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');
99
100     is_deeply(
101         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
102         [
103             $BAR_ATTR,
104             $BAZ_ATTR,
105             $FOO_ATTR,
106         ],
107         '... got the right list of applicable attributes for Baz');
108
109     is_deeply(
110         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
111         [ Bar->meta, Baz->meta, Foo->meta ],
112         '... got the right list of associated classes from the applicable attributes for Baz');
113
114     my $attr;
115     lives_ok {
116         $attr = $meta->remove_attribute('$baz');
117     } '... removed the $baz attribute successfully';
118     is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
119
120     ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute');
121     is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');
122
123     ok(!$meta->has_method('get_baz'), '... a reader has been removed');
124     ok(!$meta->has_method('set_baz'), '... a writer has been removed');
125
126     is_deeply(
127         [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
128         [
129             $BAR_ATTR,
130             $FOO_ATTR,
131         ],
132         '... got the right list of applicable attributes for Baz');
133
134     is_deeply(
135         [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
136         [ Bar->meta, Foo->meta ],
137         '... got the right list of associated classes from the applicable attributes for Baz');
138
139      {
140          my $attr;
141          lives_ok {
142              $attr = Bar->meta->remove_attribute('$bar');
143          } '... removed the $bar attribute successfully';
144          is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
145
146          ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
147
148          ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
149      }
150
151      is_deeply(
152          [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
153          [
154              $BAR_ATTR_2,
155              $FOO_ATTR,
156          ],
157          '... got the right list of applicable attributes for Baz');
158
159      is_deeply(
160          [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
161          [ Foo->meta, Foo->meta ],
162          '... got the right list of associated classes from the applicable attributes for Baz');
163
164     # remove attribute which is not there
165     my $val;
166     lives_ok {
167         $val = $meta->remove_attribute('$blammo');
168     } '... attempted to remove the non-existent $blammo attribute';
169     is($val, undef, '... got the right value back (undef)');
170
171 }
172
173 {
174     package Buzz;
175     use metaclass;
176     use Scalar::Util qw/blessed/;
177
178     my $meta = Buzz->meta;
179     ::lives_ok {
180         $meta->add_attribute($FOO_ATTR_2);
181     } '... we added an attribute to Buzz successfully';
182
183     ::lives_ok {
184         $meta->add_attribute(
185             Class::MOP::Attribute->new(
186                  '$bar' => (
187                             accessor  => 'bar',
188                             predicate => 'has_bar',
189                             clearer   => 'clear_bar',
190                            )
191                 )
192         );
193     } '... we added an attribute to Buzz successfully';
194
195     ::lives_ok {
196         $meta->add_attribute(
197             Class::MOP::Attribute->new(
198                  '$bah' => (
199                             accessor  => 'bah',
200                             predicate => 'has_bah',
201                             clearer   => 'clear_bah',
202                             default   => 'BAH',
203                            )
204                 )
205         );
206     } '... we added an attribute to Buzz successfully';
207
208     ::lives_ok {
209         $meta->add_method(build_foo => sub{ blessed shift; });
210     } '... we added a method to Buzz successfully';
211 }
212
213 {
214   my $buzz;
215   ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
216   ::is($buzz->foo, 'Buzz', '...foo builder works as expected');
217   ::ok(!$buzz->has_bar, '...bar is not set');
218   ::is($buzz->bar, undef, '...bar returns undef');
219   ::ok(!$buzz->has_bar, '...bar was not autovivified');
220
221   $buzz->bar(undef);
222   ::ok($buzz->has_bar, '...bar is set');
223   ::is($buzz->bar, undef, '...bar is undef');
224   $buzz->clear_bar;
225   ::ok(!$buzz->has_bar, '...bar is no longerset');
226
227   my $buzz2;
228   ::lives_ok { $buzz2 = Buzz->meta->new_object('$bar' => undef) } '...Buzz instantiated successfully';
229   ::ok($buzz2->has_bar, '...bar is set');
230   ::is($buzz2->bar, undef, '...bar is undef');
231
232 }
233
234 {
235   my $buzz;
236   ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
237   ::ok($buzz->has_bah, '...bah is set');
238   ::is($buzz->bah, 'BAH', '...bah returns "BAH" ');
239
240   my $buzz2;
241   ::lives_ok { $buzz2 = Buzz->meta->new_object('$bah' => undef) } '...Buzz instantiated successfully';
242   ::ok($buzz2->has_bah, '...bah is set');
243   ::is($buzz2->bah, undef, '...bah is undef');
244
245 }