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
|
use strict;
use warnings;
use Scalar::Util 'blessed', 'reftype';
use Test::More;
use Class::MOP;
=pod
This checks the get_read/write_method
and get_read/write_method_ref methods
=cut
{
package Foo;
use metaclass;
Foo->meta->add_attribute('bar' =>
reader => 'get_bar',
writer => 'set_bar',
);
Foo->meta->add_attribute('baz' =>
accessor => 'baz',
);
Foo->meta->add_attribute('gorch' =>
reader => { 'get_gorch', => sub { (shift)->{gorch} } }
);
package Bar;
use metaclass;
Bar->meta->superclasses('Foo');
Bar->meta->add_attribute('quux' =>
accessor => 'quux',
);
}
can_ok('Foo', 'get_bar');
can_ok('Foo', 'set_bar');
can_ok('Foo', 'baz');
can_ok('Foo', 'get_gorch');
ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar');
ok(Foo->meta->has_attribute('baz'), '... Foo has the attribute baz');
ok(Foo->meta->has_attribute('gorch'), '... Foo has the attribute gorch');
my $bar_attr = Foo->meta->get_attribute('bar');
my $baz_attr = Foo->meta->get_attribute('baz');
my $gorch_attr = Foo->meta->get_attribute('gorch');
is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar');
is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar');
is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
is($bar_attr->get_read_method, 'get_bar', '... $attr does have an read method');
is($bar_attr->get_write_method, 'set_bar', '... $attr does have an write method');
{
my $reader = $bar_attr->get_read_method_ref;
my $writer = $bar_attr->get_write_method_ref;
isa_ok($reader, 'Class::MOP::Method');
isa_ok($writer, 'Class::MOP::Method');
is($reader->fully_qualified_name, 'Foo::get_bar', '... it is the sub we are looking for');
is($writer->fully_qualified_name, 'Foo::set_bar', '... it is the sub we are looking for');
is(reftype($reader->body), 'CODE', '... it is a plain old sub');
is(reftype($writer->body), 'CODE', '... it is a plain old sub');
}
is($baz_attr->accessor, 'baz', '... the bar attribute has the accessor baz');
is($baz_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
is($baz_attr->get_read_method, 'baz', '... $attr does have an read method');
is($baz_attr->get_write_method, 'baz', '... $attr does have an write method');
{
my $reader = $baz_attr->get_read_method_ref;
my $writer = $baz_attr->get_write_method_ref;
isa_ok($reader, 'Class::MOP::Method');
isa_ok($writer, 'Class::MOP::Method');
is($reader, $writer, '... they are the same method');
is($reader->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for');
is($writer->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for');
}
is(ref($gorch_attr->reader), 'HASH', '... the gorch attribute has the reader get_gorch (HASH ref)');
is($gorch_attr->associated_class, Foo->meta, '... and the gorch attribute is associated with Foo->meta');
is($gorch_attr->get_read_method, 'get_gorch', '... $attr does have an read method');
ok(!$gorch_attr->get_write_method, '... $attr does not have an write method');
{
my $reader = $gorch_attr->get_read_method_ref;
my $writer = $gorch_attr->get_write_method_ref;
isa_ok($reader, 'Class::MOP::Method');
ok(blessed($writer), '... it is not a plain old sub');
isa_ok($writer, 'Class::MOP::Method');
is($reader->fully_qualified_name, 'Foo::get_gorch', '... it is the sub we are looking for');
is($writer->fully_qualified_name, 'Foo::__ANON__', '... it is the sub we are looking for');
}
done_testing;
|