finish get_attribute_values etc
[gitmo/Class-MOP.git] / t / 023_attribute_get_read_write.t
CommitLineData
d14f6cbe 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Scalar::Util 'blessed', 'reftype';
7
ad074154 8use Test::More tests => 38;
d14f6cbe 9
10BEGIN {
11 use_ok('Class::MOP');
12}
13
14=pod
15
16This checks the get_read/write_method
17and 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 );
ad074154 37
38 package Bar;
39 use metaclass;
40 Bar->meta->superclasses('Foo');
41
42 Bar->meta->add_attribute('quux' =>
43 accessor => 'quux',
44 );
d14f6cbe 45}
46
47can_ok('Foo', 'get_bar');
48can_ok('Foo', 'set_bar');
49can_ok('Foo', 'baz');
50can_ok('Foo', 'get_gorch');
51
52ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar');
53ok(Foo->meta->has_attribute('baz'), '... Foo has the attribute baz');
54ok(Foo->meta->has_attribute('gorch'), '... Foo has the attribute gorch');
55
56my $bar_attr = Foo->meta->get_attribute('bar');
57my $baz_attr = Foo->meta->get_attribute('baz');
58my $gorch_attr = Foo->meta->get_attribute('gorch');
59
60is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar');
61is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar');
62is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
63
64is($bar_attr->get_read_method, 'get_bar', '... $attr does have an read method');
65is($bar_attr->get_write_method, 'set_bar', '... $attr does have an write method');
66
67{
68 my $reader = $bar_attr->get_read_method_ref;
69 my $writer = $bar_attr->get_write_method_ref;
70
71 isa_ok($reader, 'Class::MOP::Method');
72 isa_ok($writer, 'Class::MOP::Method');
73
74 is($reader->fully_qualified_name, 'Foo::get_bar', '... it is the sub we are looking for');
75 is($writer->fully_qualified_name, 'Foo::set_bar', '... it is the sub we are looking for');
76
77 is(reftype($reader->body), 'CODE', '... it is a plain old sub');
78 is(reftype($writer->body), 'CODE', '... it is a plain old sub');
79}
80
81is($baz_attr->accessor, 'baz', '... the bar attribute has the accessor baz');
82is($baz_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
83
84is($baz_attr->get_read_method, 'baz', '... $attr does have an read method');
85is($baz_attr->get_write_method, 'baz', '... $attr does have an write method');
86
87{
88 my $reader = $baz_attr->get_read_method_ref;
89 my $writer = $baz_attr->get_write_method_ref;
90
91 isa_ok($reader, 'Class::MOP::Method');
92 isa_ok($writer, 'Class::MOP::Method');
93
94 is($reader, $writer, '... they are the same method');
95
96 is($reader->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for');
97 is($writer->fully_qualified_name, 'Foo::baz', '... it is the sub we are looking for');
98}
99
100is(ref($gorch_attr->reader), 'HASH', '... the gorch attribute has the reader get_gorch (HASH ref)');
101is($gorch_attr->associated_class, Foo->meta, '... and the gorch attribute is associated with Foo->meta');
102
103is($gorch_attr->get_read_method, 'get_gorch', '... $attr does have an read method');
104ok(!$gorch_attr->get_write_method, '... $attr does not have an write method');
105
106{
107 my $reader = $gorch_attr->get_read_method_ref;
108 my $writer = $gorch_attr->get_write_method_ref;
109
110 isa_ok($reader, 'Class::MOP::Method');
111 ok(!blessed($writer), '... it is not a plain old sub');
112
113 is($reader->fully_qualified_name, 'Foo::get_gorch', '... it is the sub we are looking for');
114}
ad074154 115
116my $foo = bless {}, 'Foo';
117$foo->set_bar(1);
118$foo->baz(10);
119
361c5bc4 120is_deeply($foo->meta->get_attribute_values($foo), {
ad074154 121 bar => 1,
122 baz => 10,
123});
124
125my $bar = bless {}, 'Bar';
126$bar->set_bar(99);
127
361c5bc4 128is_deeply($bar->meta->get_attribute_values($bar), {
ad074154 129 bar => 99,
130});
131
132$bar->quux(1337);
133
361c5bc4 134is_deeply($bar->meta->get_attribute_values($bar), {
ad074154 135 bar => 99,
136 quux => 1337,
137});
138