17 __PACKAGE__->meta->add_attribute('bar' => (
28 __PACKAGE__->meta->superclasses('Foo');
30 __PACKAGE__->meta->add_attribute('baz' => (
32 default => sub { 'BAZ' },
41 __PACKAGE__->meta->superclasses('Bar');
43 __PACKAGE__->meta->add_attribute('bah' => (
55 __PACKAGE__->meta->add_attribute('bar' => (
57 predicate => 'has_bar',
58 clearer => 'clear_bar',
61 __PACKAGE__->meta->add_attribute('bah' => (
63 predicate => 'has_bah',
64 clearer => 'clear_bah',
72 is($meta->name, 'Foo', '... checking the Foo metaclass');
75 my $bar_accessor = $meta->get_method('bar');
76 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
77 isa_ok($bar_accessor, 'Class::MOP::Method');
79 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
82 ok(!$meta->is_immutable, '... our class is not immutable');
85 $meta->make_immutable(
86 inline_constructor => 1,
87 inline_accessors => 0,
89 }, undef, '... changed Foo to be immutable' );
91 ok($meta->is_immutable, '... our class is now immutable');
92 isa_ok($meta, 'Class::MOP::Class');
94 # they made a constructor for us :)
100 is($foo->bar, 'BAR', '... got the right default value');
104 my $foo = Foo->new(bar => 'BAZ');
106 is($foo->bar, 'BAZ', '... got the right parameter value');
110 # check that the constructor correctly handles inheritance
112 my $bar = Bar->new();
115 is($bar->bar, 'BAR', '... got the right inherited parameter value');
116 is($bar->baz, 'BAZ', '... got the right inherited parameter value');
119 # check out accessors too
121 my $bar_accessor = $meta->get_method('bar');
122 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
123 isa_ok($bar_accessor, 'Class::MOP::Method');
125 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
130 my $meta = Bar->meta;
131 is($meta->name, 'Bar', '... checking the Bar metaclass');
134 my $bar_accessor = $meta->find_method_by_name('bar');
135 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
136 isa_ok($bar_accessor, 'Class::MOP::Method');
138 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
140 my $baz_accessor = $meta->get_method('baz');
141 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
142 isa_ok($baz_accessor, 'Class::MOP::Method');
144 ok(!$baz_accessor->is_inline, '... the baz accessor is not inlined');
147 ok(!$meta->is_immutable, '... our class is not immutable');
150 $meta->make_immutable(
151 inline_constructor => 1,
152 inline_accessors => 1,
154 }, undef, '... changed Bar to be immutable' );
156 ok($meta->is_immutable, '... our class is now immutable');
157 isa_ok($meta, 'Class::MOP::Class');
159 # they made a constructor for us :)
160 can_ok('Bar', 'new');
165 is($bar->bar, 'BAR', '... got the right default value');
166 is($bar->baz, 'BAZ', '... got the right default value');
170 my $bar = Bar->new(bar => 'BAZ!', baz => 'BAR!');
172 is($bar->bar, 'BAZ!', '... got the right parameter value');
173 is($bar->baz, 'BAR!', '... got the right parameter value');
176 # check out accessors too
178 my $bar_accessor = $meta->find_method_by_name('bar');
179 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
180 isa_ok($bar_accessor, 'Class::MOP::Method');
182 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
184 my $baz_accessor = $meta->get_method('baz');
185 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
186 isa_ok($baz_accessor, 'Class::MOP::Method');
188 ok($baz_accessor->is_inline, '... the baz accessor is not inlined');
193 my $meta = Baz->meta;
194 is($meta->name, 'Baz', '... checking the Bar metaclass');
197 my $bar_accessor = $meta->find_method_by_name('bar');
198 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
199 isa_ok($bar_accessor, 'Class::MOP::Method');
201 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
203 my $baz_accessor = $meta->find_method_by_name('baz');
204 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
205 isa_ok($baz_accessor, 'Class::MOP::Method');
207 ok($baz_accessor->is_inline, '... the baz accessor is inlined');
209 my $bah_accessor = $meta->get_method('bah');
210 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
211 isa_ok($bah_accessor, 'Class::MOP::Method');
213 ok(!$bah_accessor->is_inline, '... the baz accessor is not inlined');
216 ok(!$meta->is_immutable, '... our class is not immutable');
219 $meta->make_immutable(
220 inline_constructor => 0,
221 inline_accessors => 1,
223 }, undef, '... changed Bar to be immutable' );
225 ok($meta->is_immutable, '... our class is now immutable');
226 isa_ok($meta, 'Class::MOP::Class');
228 ok(!Baz->meta->has_method('new'), '... no constructor was made');
231 my $baz = Baz->meta->new_object;
233 is($baz->bar, 'BAR', '... got the right default value');
234 is($baz->baz, 'BAZ', '... got the right default value');
238 my $baz = Baz->meta->new_object(bar => 'BAZ!', baz => 'BAR!', bah => 'BAH!');
240 is($baz->bar, 'BAZ!', '... got the right parameter value');
241 is($baz->baz, 'BAR!', '... got the right parameter value');
242 is($baz->bah, 'BAH!', '... got the right parameter value');
245 # check out accessors too
247 my $bar_accessor = $meta->find_method_by_name('bar');
248 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
249 isa_ok($bar_accessor, 'Class::MOP::Method');
251 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
253 my $baz_accessor = $meta->find_method_by_name('baz');
254 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
255 isa_ok($baz_accessor, 'Class::MOP::Method');
257 ok($baz_accessor->is_inline, '... the baz accessor is not inlined');
259 my $bah_accessor = $meta->get_method('bah');
260 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
261 isa_ok($bah_accessor, 'Class::MOP::Method');
263 ok($bah_accessor->is_inline, '... the baz accessor is not inlined');
270 ::is( ::exception { $buzz = Buzz->meta->new_object }, undef, '...Buzz instantiated successfully' );
271 ::ok(!$buzz->has_bar, '...bar is not set');
272 ::is($buzz->bar, undef, '...bar returns undef');
273 ::ok(!$buzz->has_bar, '...bar was not autovivified');
276 ::ok($buzz->has_bar, '...bar is set');
277 ::is($buzz->bar, undef, '...bar is undef');
279 ::ok(!$buzz->has_bar, '...bar is no longerset');
282 ::is( ::exception { $buzz2 = Buzz->meta->new_object('bar' => undef) }, undef, '...Buzz instantiated successfully' );
283 ::ok($buzz2->has_bar, '...bar is set');
284 ::is($buzz2->bar, undef, '...bar is undef');
290 ::is( ::exception { $buzz = Buzz->meta->new_object }, undef, '...Buzz instantiated successfully' );
291 ::ok($buzz->has_bah, '...bah is set');
292 ::is($buzz->bah, 'BAH', '...bah returns "BAH"' );
295 ::is( ::exception { $buzz2 = Buzz->meta->new_object('bah' => undef) }, undef, '...Buzz instantiated successfully' );
296 ::ok($buzz2->has_bah, '...bah is set');
297 ::is($buzz2->bah, undef, '...bah is undef');