4 use Test::More tests => 79;
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');
80 ok(!$meta->is_immutable, '... our class is not immutable');
83 $meta->make_immutable(
84 inline_constructor => 1,
85 inline_accessors => 0,
87 } '... changed Foo to be immutable';
89 ok($meta->is_immutable, '... our class is now immutable');
90 isa_ok($meta, 'Class::MOP::Class');
92 # they made a constructor for us :)
98 is($foo->bar, 'BAR', '... got the right default value');
102 my $foo = Foo->new(bar => 'BAZ');
104 is($foo->bar, 'BAZ', '... got the right parameter value');
108 # check that the constructor correctly handles inheritance
110 my $bar = Bar->new();
113 is($bar->bar, 'BAR', '... got the right inherited parameter value');
114 is($bar->baz, 'BAZ', '... got the right inherited parameter value');
117 # check out accessors too
119 my $bar_accessor = $meta->get_method('bar');
120 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
121 isa_ok($bar_accessor, 'Class::MOP::Method');
126 my $meta = Bar->meta;
127 is($meta->name, 'Bar', '... checking the Bar metaclass');
130 my $bar_accessor = $meta->find_method_by_name('bar');
131 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
132 isa_ok($bar_accessor, 'Class::MOP::Method');
134 my $baz_accessor = $meta->get_method('baz');
135 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
136 isa_ok($baz_accessor, 'Class::MOP::Method');
139 ok(!$meta->is_immutable, '... our class is not immutable');
142 $meta->make_immutable(
143 inline_constructor => 1,
144 inline_accessors => 1,
146 } '... changed Bar to be immutable';
148 ok($meta->is_immutable, '... our class is now immutable');
149 isa_ok($meta, 'Class::MOP::Class');
151 # they made a constructor for us :)
152 can_ok('Bar', 'new');
157 is($bar->bar, 'BAR', '... got the right default value');
158 is($bar->baz, 'BAZ', '... got the right default value');
162 my $bar = Bar->new(bar => 'BAZ!', baz => 'BAR!');
164 is($bar->bar, 'BAZ!', '... got the right parameter value');
165 is($bar->baz, 'BAR!', '... got the right parameter value');
168 # check out accessors too
170 my $bar_accessor = $meta->find_method_by_name('bar');
171 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
172 isa_ok($bar_accessor, 'Class::MOP::Method');
174 my $baz_accessor = $meta->get_method('baz');
175 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
176 isa_ok($baz_accessor, 'Class::MOP::Method');
182 my $meta = Baz->meta;
183 is($meta->name, 'Baz', '... checking the Bar metaclass');
186 my $bar_accessor = $meta->find_method_by_name('bar');
187 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
188 isa_ok($bar_accessor, 'Class::MOP::Method');
190 my $baz_accessor = $meta->find_method_by_name('baz');
191 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
192 isa_ok($baz_accessor, 'Class::MOP::Method');
194 my $bah_accessor = $meta->get_method('bah');
195 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
196 isa_ok($bah_accessor, 'Class::MOP::Method');
199 ok(!$meta->is_immutable, '... our class is not immutable');
202 $meta->make_immutable(
203 inline_constructor => 0,
204 inline_accessors => 1,
206 } '... changed Bar to be immutable';
208 ok($meta->is_immutable, '... our class is now immutable');
209 isa_ok($meta, 'Class::MOP::Class');
211 ok(!Baz->meta->has_method('new'), '... no constructor was made');
214 my $baz = Baz->meta->new_object;
216 is($baz->bar, 'BAR', '... got the right default value');
217 is($baz->baz, 'BAZ', '... got the right default value');
221 my $baz = Baz->meta->new_object(bar => 'BAZ!', baz => 'BAR!', bah => 'BAH!');
223 is($baz->bar, 'BAZ!', '... got the right parameter value');
224 is($baz->baz, 'BAR!', '... got the right parameter value');
225 is($baz->bah, 'BAH!', '... got the right parameter value');
228 # check out accessors too
230 my $bar_accessor = $meta->find_method_by_name('bar');
231 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
232 isa_ok($bar_accessor, 'Class::MOP::Method');
234 my $baz_accessor = $meta->find_method_by_name('baz');
235 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
236 isa_ok($baz_accessor, 'Class::MOP::Method');
238 my $bah_accessor = $meta->get_method('bah');
239 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
240 isa_ok($bah_accessor, 'Class::MOP::Method');
248 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
249 ::ok(!$buzz->has_bar, '...bar is not set');
250 ::is($buzz->bar, undef, '...bar returns undef');
251 ::ok(!$buzz->has_bar, '...bar was not autovivified');
254 ::ok($buzz->has_bar, '...bar is set');
255 ::is($buzz->bar, undef, '...bar is undef');
257 ::ok(!$buzz->has_bar, '...bar is no longerset');
260 ::lives_ok { $buzz2 = Buzz->meta->new_object('bar' => undef) } '...Buzz instantiated successfully';
261 ::ok($buzz2->has_bar, '...bar is set');
262 ::is($buzz2->bar, undef, '...bar is undef');
268 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
269 ::ok($buzz->has_bah, '...bah is set');
270 ::is($buzz->bah, 'BAH', '...bah returns "BAH"' );
273 ::lives_ok { $buzz2 = Buzz->meta->new_object('bah' => undef) } '...Buzz instantiated successfully';
274 ::ok($buzz2->has_bah, '...bah is set');
275 ::is($buzz2->bah, undef, '...bah is undef');