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