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