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