Commit | Line | Data |
d14f6cbe |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Scalar::Util 'blessed', 'reftype'; |
5 | |
86a4d873 |
6 | use Test::More; |
d14f6cbe |
7 | |
efd3d14c |
8 | use Class::MOP; |
d14f6cbe |
9 | |
10 | =pod |
11 | |
12 | This checks the get_read/write_method |
13 | and get_read/write_method_ref methods |
14 | |
15 | =cut |
16 | |
17 | { |
18 | package Foo; |
19 | use metaclass; |
86a4d873 |
20 | |
21 | Foo->meta->add_attribute('bar' => |
d14f6cbe |
22 | reader => 'get_bar', |
23 | writer => 'set_bar', |
86a4d873 |
24 | ); |
25 | |
26 | Foo->meta->add_attribute('baz' => |
d14f6cbe |
27 | accessor => 'baz', |
86a4d873 |
28 | ); |
29 | |
30 | Foo->meta->add_attribute('gorch' => |
d14f6cbe |
31 | reader => { 'get_gorch', => sub { (shift)->{gorch} } } |
86a4d873 |
32 | ); |
ad074154 |
33 | |
34 | package Bar; |
35 | use metaclass; |
36 | Bar->meta->superclasses('Foo'); |
37 | |
38 | Bar->meta->add_attribute('quux' => |
39 | accessor => 'quux', |
40 | ); |
d14f6cbe |
41 | } |
42 | |
43 | can_ok('Foo', 'get_bar'); |
86a4d873 |
44 | can_ok('Foo', 'set_bar'); |
45 | can_ok('Foo', 'baz'); |
46 | can_ok('Foo', 'get_gorch'); |
d14f6cbe |
47 | |
48 | ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar'); |
49 | ok(Foo->meta->has_attribute('baz'), '... Foo has the attribute baz'); |
50 | ok(Foo->meta->has_attribute('gorch'), '... Foo has the attribute gorch'); |
51 | |
52 | my $bar_attr = Foo->meta->get_attribute('bar'); |
53 | my $baz_attr = Foo->meta->get_attribute('baz'); |
54 | my $gorch_attr = Foo->meta->get_attribute('gorch'); |
55 | |
56 | is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar'); |
86a4d873 |
57 | is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar'); |
d14f6cbe |
58 | is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta'); |
59 | |
60 | is($bar_attr->get_read_method, 'get_bar', '... $attr does have an read method'); |
86a4d873 |
61 | is($bar_attr->get_write_method, 'set_bar', '... $attr does have an write method'); |
d14f6cbe |
62 | |
63 | { |
64 | my $reader = $bar_attr->get_read_method_ref; |
86a4d873 |
65 | my $writer = $bar_attr->get_write_method_ref; |
66 | |
d14f6cbe |
67 | isa_ok($reader, 'Class::MOP::Method'); |
86a4d873 |
68 | isa_ok($writer, 'Class::MOP::Method'); |
69 | |
d14f6cbe |
70 | is($reader->fully_qualified_name, 'Foo::get_bar', '... it is the sub we are looking for'); |
71 | is($writer->fully_qualified_name, 'Foo::set_bar', '... it is the sub we are looking for'); |
86a4d873 |
72 | |
d14f6cbe |
73 | is(reftype($reader->body), 'CODE', '... it is a plain old sub'); |
86a4d873 |
74 | is(reftype($writer->body), 'CODE', '... it is a plain old sub'); |
d14f6cbe |
75 | } |
76 | |
77 | is($baz_attr->accessor, 'baz', '... the bar attribute has the accessor baz'); |
78 | is($baz_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta'); |
79 | |
80 | is($baz_attr->get_read_method, 'baz', '... $attr does have an read method'); |
86a4d873 |
81 | is($baz_attr->get_write_method, 'baz', '... $attr does have an write method'); |
d14f6cbe |
82 | |
83 | { |
84 | my $reader = $baz_attr->get_read_method_ref; |
86a4d873 |
85 | my $writer = $baz_attr->get_write_method_ref; |
86 | |
d14f6cbe |
87 | isa_ok($reader, 'Class::MOP::Method'); |
86a4d873 |
88 | isa_ok($writer, 'Class::MOP::Method'); |
89 | |
90 | is($reader, $writer, '... they are the same method'); |
91 | |
d14f6cbe |
92 | is($reader->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for'); |
86a4d873 |
93 | is($writer->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for'); |
d14f6cbe |
94 | } |
95 | |
96 | is(ref($gorch_attr->reader), 'HASH', '... the gorch attribute has the reader get_gorch (HASH ref)'); |
97 | is($gorch_attr->associated_class, Foo->meta, '... and the gorch attribute is associated with Foo->meta'); |
98 | |
99 | is($gorch_attr->get_read_method, 'get_gorch', '... $attr does have an read method'); |
86a4d873 |
100 | ok(!$gorch_attr->get_write_method, '... $attr does not have an write method'); |
d14f6cbe |
101 | |
102 | { |
103 | my $reader = $gorch_attr->get_read_method_ref; |
86a4d873 |
104 | my $writer = $gorch_attr->get_write_method_ref; |
105 | |
d14f6cbe |
106 | isa_ok($reader, 'Class::MOP::Method'); |
86a4d873 |
107 | ok(blessed($writer), '... it is not a plain old sub'); |
108 | isa_ok($writer, 'Class::MOP::Method'); |
109 | |
d14f6cbe |
110 | is($reader->fully_qualified_name, 'Foo::get_gorch', '... it is the sub we are looking for'); |
86a4d873 |
111 | is($writer->fully_qualified_name, 'Foo::__ANON__', '... it is the sub we are looking for'); |
d14f6cbe |
112 | } |
86a4d873 |
113 | |
114 | done_testing; |