make inlining a bit more easily extensible
[gitmo/Class-MOP.git] / t / 049_metaclass_reinitialize.t
CommitLineData
7975280a 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 5use Test::Fatal;
7975280a 6
7{
8 package Foo;
9 use metaclass;
10 sub foo {}
2d09de80 11 Foo->meta->add_attribute('bar');
7975280a 12}
13
14sub check_meta_sanity {
38cf226c 15 my ($meta, $class) = @_;
7975280a 16 isa_ok($meta, 'Class::MOP::Class');
38cf226c 17 is($meta->name, $class);
7975280a 18 ok($meta->has_method('foo'));
38cf226c 19 isa_ok($meta->get_method('foo'), 'Class::MOP::Method');
2d09de80 20 ok($meta->has_attribute('bar'));
38cf226c 21 isa_ok($meta->get_attribute('bar'), 'Class::MOP::Attribute');
7975280a 22}
23
24can_ok('Foo', 'meta');
25
26my $meta = Foo->meta;
38cf226c 27check_meta_sanity($meta, 'Foo');
7975280a 28
871e9eb5 29is( exception {
7975280a 30 $meta = $meta->reinitialize($meta->name);
871e9eb5 31}, undef );
38cf226c 32check_meta_sanity($meta, 'Foo');
7975280a 33
871e9eb5 34is( exception {
7975280a 35 $meta = $meta->reinitialize($meta);
871e9eb5 36}, undef );
38cf226c 37check_meta_sanity($meta, 'Foo');
7975280a 38
871e9eb5 39like( exception {
7975280a 40 $meta->reinitialize('');
871e9eb5 41}, qr/You must pass a package name or an existing Class::MOP::Package instance/ );
7975280a 42
871e9eb5 43like( exception {
7975280a 44 $meta->reinitialize($meta->new_object);
871e9eb5 45}, qr/You must pass a package name or an existing Class::MOP::Package instance/ );
86a4d873 46
38cf226c 47{
48 package Bar::Meta::Method;
49 use base 'Class::MOP::Method';
50 __PACKAGE__->meta->add_attribute('test', accessor => 'test');
51}
52
53{
54 package Bar::Meta::Attribute;
55 use base 'Class::MOP::Attribute';
56 __PACKAGE__->meta->add_attribute('tset', accessor => 'tset');
57}
58
59{
60 package Bar;
61 use metaclass;
62 Bar->meta->add_method('foo' => Bar::Meta::Method->wrap(sub {}, name => 'foo', package_name => 'Bar'));
63 Bar->meta->add_attribute(Bar::Meta::Attribute->new('bar'));
64}
65
66$meta = Bar->meta;
67check_meta_sanity($meta, 'Bar');
68isa_ok(Bar->meta->get_method('foo'), 'Bar::Meta::Method');
69isa_ok(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
871e9eb5 70is( exception {
38cf226c 71 $meta = $meta->reinitialize('Bar');
871e9eb5 72}, undef );
38cf226c 73check_meta_sanity($meta, 'Bar');
74isa_ok(Bar->meta->get_method('foo'), 'Bar::Meta::Method');
75isa_ok(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
76
77Bar->meta->get_method('foo')->test('FOO');
78Bar->meta->get_attribute('bar')->tset('OOF');
79
80is(Bar->meta->get_method('foo')->test, 'FOO');
81is(Bar->meta->get_attribute('bar')->tset, 'OOF');
871e9eb5 82is( exception {
38cf226c 83 $meta = $meta->reinitialize('Bar');
871e9eb5 84}, undef );
38cf226c 85is(Bar->meta->get_method('foo')->test, 'FOO');
86is(Bar->meta->get_attribute('bar')->tset, 'OOF');
87
88{
89 package Baz::Meta::Attribute;
90 use base 'Class::MOP::Attribute';
91}
92
93{
94 package Baz::Meta::Method;
95 use base 'Class::MOP::Method';
96}
97
98{
99 package Baz;
37a46507 100 use metaclass meta_name => undef;
38cf226c 101
102 sub foo {}
0bd3cf1c 103 Class::MOP::class_of('Baz')->add_attribute('bar');
38cf226c 104}
105
0bd3cf1c 106$meta = Class::MOP::class_of('Baz');
38cf226c 107check_meta_sanity($meta, 'Baz');
0bd3cf1c 108ok(!$meta->get_method('foo')->isa('Baz::Meta::Method'));
109ok(!$meta->get_attribute('bar')->isa('Baz::Meta::Attribute'));
871e9eb5 110is( exception {
38cf226c 111 $meta = $meta->reinitialize(
112 'Baz',
113 attribute_metaclass => 'Baz::Meta::Attribute',
114 method_metaclass => 'Baz::Meta::Method'
115 );
871e9eb5 116}, undef );
38cf226c 117check_meta_sanity($meta, 'Baz');
0bd3cf1c 118isa_ok($meta->get_method('foo'), 'Baz::Meta::Method');
119isa_ok($meta->get_attribute('bar'), 'Baz::Meta::Attribute');
38cf226c 120
121{
122 package Quux;
123 use metaclass
124 attribute_metaclass => 'Bar::Meta::Attribute',
125 method_metaclass => 'Bar::Meta::Method';
126
127 sub foo {}
128 Quux->meta->add_attribute('bar');
129}
130
131$meta = Quux->meta;
132check_meta_sanity($meta, 'Quux');
133isa_ok(Quux->meta->get_method('foo'), 'Bar::Meta::Method');
134isa_ok(Quux->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
871e9eb5 135like( exception {
38cf226c 136 $meta = $meta->reinitialize(
137 'Quux',
138 attribute_metaclass => 'Baz::Meta::Attribute',
139 method_metaclass => 'Baz::Meta::Method',
140 );
871e9eb5 141}, qr/compatible/ );
38cf226c 142
b26fab6d 143{
144 package Quuux::Meta::Attribute;
145 use base 'Class::MOP::Attribute';
146
147 sub install_accessors {}
148}
149
150{
151 package Quuux;
152 use metaclass;
153 sub foo {}
154 Quuux->meta->add_attribute('bar', reader => 'bar');
155}
156
157$meta = Quuux->meta;
158check_meta_sanity($meta, 'Quuux');
159ok($meta->has_method('bar'));
871e9eb5 160is( exception {
b26fab6d 161 $meta = $meta->reinitialize(
162 'Quuux',
163 attribute_metaclass => 'Quuux::Meta::Attribute',
164 );
871e9eb5 165}, undef );
b26fab6d 166check_meta_sanity($meta, 'Quuux');
167ok(!$meta->has_method('bar'));
168
394df7e3 169{
170 package Blah::Meta::Method;
171 use base 'Class::MOP::Method';
172
173 __PACKAGE__->meta->add_attribute('foo', reader => 'foo', default => 'TEST');
174}
175
176{
177 package Blah::Meta::Attribute;
178 use base 'Class::MOP::Attribute';
179
180 __PACKAGE__->meta->add_attribute('oof', reader => 'oof', default => 'TSET');
181}
182
183{
184 package Blah;
0bd3cf1c 185 use metaclass no_meta => 1;
394df7e3 186 sub foo {}
0bd3cf1c 187 Class::MOP::class_of('Blah')->add_attribute('bar');
394df7e3 188}
189
0bd3cf1c 190$meta = Class::MOP::class_of('Blah');
394df7e3 191check_meta_sanity($meta, 'Blah');
871e9eb5 192is( exception {
0bd3cf1c 193 $meta = Class::MOP::Class->reinitialize(
194 'Blah',
195 attribute_metaclass => 'Blah::Meta::Attribute',
196 method_metaclass => 'Blah::Meta::Method',
197 );
871e9eb5 198}, undef );
394df7e3 199check_meta_sanity($meta, 'Blah');
0bd3cf1c 200can_ok($meta->get_method('foo'), 'foo');
201is($meta->get_method('foo')->foo, 'TEST');
202can_ok($meta->get_attribute('bar'), 'oof');
203is($meta->get_attribute('bar')->oof, 'TSET');
394df7e3 204
86a4d873 205done_testing;