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