got rid of all the use_ok junk except for 000_load.t
[gitmo/Class-MOP.git] / t / 005_attributes.t
CommitLineData
2eb717d5 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
efd3d14c 6use Test::More tests => 70;
2eb717d5 7use Test::Exception;
8
efd3d14c 9use Class::MOP;
2eb717d5 10
11my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
12my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
13 accessor => 'bar'
14));
15my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
16 reader => 'get_baz',
1d68af04 17 writer => 'set_baz',
2eb717d5 18));
19
22286063 20my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
21
1d68af04 22my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => (
23 accessor => 'foo',
24 builder => 'build_foo'
25));
26
aef6c672 27is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
28is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
29is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
30
2eb717d5 31{
32 package Foo;
aa448b16 33 use metaclass;
2eb717d5 34
727919c5 35 my $meta = Foo->meta;
2eb717d5 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');
1d68af04 41
2eb717d5 42 ::ok(!$meta->has_method('foo'), '... no accessor created');
1d68af04 43
22286063 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');
1d68af04 48 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
22286063 49
50 ::ok(!$meta->has_method('bar'), '... no accessor created');
2eb717d5 51}
52{
53 package Bar;
54 our @ISA = ('Foo');
1d68af04 55
727919c5 56 my $meta = Bar->meta;
2eb717d5 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
b25109b1 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
2eb717d5 67 ::ok($meta->has_method('bar'), '... an accessor has been created');
1d68af04 68 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
2eb717d5 69}
70{
71 package Baz;
72 our @ISA = ('Bar');
1d68af04 73
727919c5 74 my $meta = Baz->meta;
2eb717d5 75 ::lives_ok {
76 $meta->add_attribute($BAZ_ATTR);
77 } '... we added an attribute to Baz successfully';
1d68af04 78 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
2eb717d5 79 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
80
b25109b1 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
2eb717d5 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
ba38bf08 88 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
89 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
2eb717d5 90}
91
92{
93 my $meta = Baz->meta;
94 isa_ok($meta, 'Class::MOP::Class');
1d68af04 95
058c1cf5 96 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
1d68af04 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
2eb717d5 100 is_deeply(
c9e77dbb 101 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
1d68af04 102 [
c9e77dbb 103 $BAR_ATTR,
104 $BAZ_ATTR,
1d68af04 105 $FOO_ATTR,
2eb717d5 106 ],
107 '... got the right list of applicable attributes for Baz');
1d68af04 108
c9e77dbb 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 ],
1d68af04 112 '... got the right list of associated classes from the applicable attributes for Baz');
113
cbd9f942 114 my $attr;
115 lives_ok {
116 $attr = $meta->remove_attribute('$baz');
117 } '... removed the $baz attribute successfully';
1d68af04 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');
cbd9f942 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(
c9e77dbb 127 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
1d68af04 128 [
c9e77dbb 129 $BAR_ATTR,
1d68af04 130 $FOO_ATTR,
cbd9f942 131 ],
132 '... got the right list of applicable attributes for Baz');
133
c9e77dbb 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
cbd9f942 139 {
140 my $attr;
141 lives_ok {
142 $attr = Bar->meta->remove_attribute('$bar');
143 } '... removed the $bar attribute successfully';
1d68af04 144 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
2eb717d5 145
1d68af04 146 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
2eb717d5 147
cbd9f942 148 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
149 }
150
151 is_deeply(
c9e77dbb 152 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
1d68af04 153 [
22286063 154 $BAR_ATTR_2,
1d68af04 155 $FOO_ATTR,
cbd9f942 156 ],
157 '... got the right list of applicable attributes for Baz');
158
c9e77dbb 159 is_deeply(
160 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
22286063 161 [ Foo->meta, Foo->meta ],
c9e77dbb 162 '... got the right list of associated classes from the applicable attributes for Baz');
163
22286063 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
cbd9f942 171}
1d68af04 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 {
8768ecf3 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 {
1d68af04 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';
8768ecf3 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
1d68af04 245}