6 use Test::More tests => 77;
11 use_ok('Class::MOP::Immutable');
21 __PACKAGE__->meta->add_attribute('bar' => (
32 __PACKAGE__->meta->superclasses('Foo');
34 __PACKAGE__->meta->add_attribute('baz' => (
36 default => sub { 'BAZ' },
45 __PACKAGE__->meta->superclasses('Bar');
47 __PACKAGE__->meta->add_attribute('bah' => (
55 is($meta->name, 'Foo', '... checking the Foo metaclass');
58 my $bar_accessor = $meta->get_method('bar');
59 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
60 isa_ok($bar_accessor, 'Class::MOP::Method');
62 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
65 ok(!$meta->is_immutable, '... our class is not immutable');
68 $meta->make_immutable(
69 inline_constructor => 1,
70 inline_accessors => 0,
72 } '... changed Foo to be immutable';
74 ok($meta->is_immutable, '... our class is now immutable');
75 isa_ok($meta, 'Class::MOP::Class');
77 # they made a constructor for us :)
83 is($foo->bar, 'BAR', '... got the right default value');
87 my $foo = Foo->new(bar => 'BAZ');
89 is($foo->bar, 'BAZ', '... got the right parameter value');
93 # check that the constructor correctly handles inheritance
98 is($bar->bar, 'BAR', '... got the right inherited parameter value');
99 is($bar->baz, 'BAZ', '... got the right inherited parameter value');
102 # check out accessors too
104 my $bar_accessor = $meta->get_method('bar');
105 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
106 isa_ok($bar_accessor, 'Class::MOP::Method');
108 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
113 my $meta = Bar->meta;
114 is($meta->name, 'Bar', '... checking the Bar metaclass');
117 my $bar_accessor = $meta->find_method_by_name('bar');
118 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
119 isa_ok($bar_accessor, 'Class::MOP::Method');
121 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
123 my $baz_accessor = $meta->get_method('baz');
124 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
125 isa_ok($baz_accessor, 'Class::MOP::Method');
127 ok(!$baz_accessor->is_inline, '... the baz accessor is not inlined');
130 ok(!$meta->is_immutable, '... our class is not immutable');
133 $meta->make_immutable(
134 inline_constructor => 1,
135 inline_accessors => 1,
137 } '... changed Bar to be immutable';
139 ok($meta->is_immutable, '... our class is now immutable');
140 isa_ok($meta, 'Class::MOP::Class');
142 # they made a constructor for us :)
143 can_ok('Bar', 'new');
148 is($bar->bar, 'BAR', '... got the right default value');
149 is($bar->baz, 'BAZ', '... got the right default value');
153 my $bar = Bar->new(bar => 'BAZ!', baz => 'BAR!');
155 is($bar->bar, 'BAZ!', '... got the right parameter value');
156 is($bar->baz, 'BAR!', '... got the right parameter value');
159 # check out accessors too
161 my $bar_accessor = $meta->find_method_by_name('bar');
162 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
163 isa_ok($bar_accessor, 'Class::MOP::Method');
165 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
167 my $baz_accessor = $meta->get_method('baz');
168 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
169 isa_ok($baz_accessor, 'Class::MOP::Method');
171 ok($baz_accessor->is_inline, '... the baz accessor is not inlined');
176 my $meta = Baz->meta;
177 is($meta->name, 'Baz', '... checking the Bar metaclass');
180 my $bar_accessor = $meta->find_method_by_name('bar');
181 isa_ok($bar_accessor, 'Class::MOP::Method::Accessor');
182 isa_ok($bar_accessor, 'Class::MOP::Method');
184 ok(!$bar_accessor->is_inline, '... the bar accessor is not inlined');
186 my $baz_accessor = $meta->find_method_by_name('baz');
187 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
188 isa_ok($baz_accessor, 'Class::MOP::Method');
190 ok($baz_accessor->is_inline, '... the baz accessor is inlined');
192 my $bah_accessor = $meta->get_method('bah');
193 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
194 isa_ok($bah_accessor, 'Class::MOP::Method');
196 ok(!$bah_accessor->is_inline, '... the baz accessor is not inlined');
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->construct_instance;
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->construct_instance(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 ok(!$bar_accessor->is_inline, '... the bar accessor is still not inlined');
236 my $baz_accessor = $meta->find_method_by_name('baz');
237 isa_ok($baz_accessor, 'Class::MOP::Method::Accessor');
238 isa_ok($baz_accessor, 'Class::MOP::Method');
240 ok($baz_accessor->is_inline, '... the baz accessor is not inlined');
242 my $bah_accessor = $meta->get_method('bah');
243 isa_ok($bah_accessor, 'Class::MOP::Method::Accessor');
244 isa_ok($bah_accessor, 'Class::MOP::Method');
246 ok($bah_accessor->is_inline, '... the baz accessor is not inlined');