add Build.PL; update MANIFEST
[gitmo/Class-MOP.git] / t / 005_attributes.t
CommitLineData
2eb717d5 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
aef6c672 6use Test::More tests => 50;
2eb717d5 7use Test::Exception;
8
9BEGIN {
aa448b16 10 use_ok('Class::MOP');
2eb717d5 11}
12
13my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
14my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
15 accessor => 'bar'
16));
17my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
18 reader => 'get_baz',
19 writer => 'set_baz',
20));
21
22286063 22my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
23
aef6c672 24is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
25is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
26is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
27
2eb717d5 28{
29 package Foo;
aa448b16 30 use metaclass;
2eb717d5 31
727919c5 32 my $meta = Foo->meta;
2eb717d5 33 ::lives_ok {
34 $meta->add_attribute($FOO_ATTR);
35 } '... we added an attribute to Foo successfully';
36 ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
37 ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
38
39 ::ok(!$meta->has_method('foo'), '... no accessor created');
22286063 40
41 ::lives_ok {
42 $meta->add_attribute($BAR_ATTR_2);
43 } '... we added an attribute to Foo successfully';
44 ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
45 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
46
47 ::ok(!$meta->has_method('bar'), '... no accessor created');
2eb717d5 48}
49{
50 package Bar;
51 our @ISA = ('Foo');
52
727919c5 53 my $meta = Bar->meta;
2eb717d5 54 ::lives_ok {
55 $meta->add_attribute($BAR_ATTR);
56 } '... we added an attribute to Bar successfully';
57 ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
58 ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
59
b25109b1 60 my $attr = $meta->get_attribute('$bar');
61 ::is($attr->get_read_method, 'bar', '... got the right read method for Bar');
62 ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
63
2eb717d5 64 ::ok($meta->has_method('bar'), '... an accessor has been created');
ba38bf08 65 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
2eb717d5 66}
67{
68 package Baz;
69 our @ISA = ('Bar');
70
727919c5 71 my $meta = Baz->meta;
2eb717d5 72 ::lives_ok {
73 $meta->add_attribute($BAZ_ATTR);
74 } '... we added an attribute to Baz successfully';
75 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
76 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
77
b25109b1 78 my $attr = $meta->get_attribute('$baz');
79 ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz');
80 ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
81
2eb717d5 82 ::ok($meta->has_method('get_baz'), '... a reader has been created');
83 ::ok($meta->has_method('set_baz'), '... a writer has been created');
84
ba38bf08 85 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
86 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
2eb717d5 87}
88
89{
90 my $meta = Baz->meta;
91 isa_ok($meta, 'Class::MOP::Class');
92
058c1cf5 93 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
94 is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');
95 is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');
96
2eb717d5 97 is_deeply(
c9e77dbb 98 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
2eb717d5 99 [
c9e77dbb 100 $BAR_ATTR,
101 $BAZ_ATTR,
102 $FOO_ATTR,
2eb717d5 103 ],
104 '... got the right list of applicable attributes for Baz');
c9e77dbb 105
106 is_deeply(
107 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
108 [ Bar->meta, Baz->meta, Foo->meta ],
109 '... got the right list of associated classes from the applicable attributes for Baz');
cbd9f942 110
111 my $attr;
112 lives_ok {
113 $attr = $meta->remove_attribute('$baz');
114 } '... removed the $baz attribute successfully';
115 is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
116
117 ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute');
22286063 118 is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');
cbd9f942 119
120 ok(!$meta->has_method('get_baz'), '... a reader has been removed');
121 ok(!$meta->has_method('set_baz'), '... a writer has been removed');
122
123 is_deeply(
c9e77dbb 124 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
cbd9f942 125 [
c9e77dbb 126 $BAR_ATTR,
127 $FOO_ATTR,
cbd9f942 128 ],
129 '... got the right list of applicable attributes for Baz');
130
c9e77dbb 131 is_deeply(
132 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
133 [ Bar->meta, Foo->meta ],
134 '... got the right list of associated classes from the applicable attributes for Baz');
135
cbd9f942 136 {
137 my $attr;
138 lives_ok {
139 $attr = Bar->meta->remove_attribute('$bar');
140 } '... removed the $bar attribute successfully';
141 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
2eb717d5 142
cbd9f942 143 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
2eb717d5 144
cbd9f942 145 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
146 }
147
148 is_deeply(
c9e77dbb 149 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
cbd9f942 150 [
22286063 151 $BAR_ATTR_2,
c9e77dbb 152 $FOO_ATTR,
cbd9f942 153 ],
154 '... got the right list of applicable attributes for Baz');
155
c9e77dbb 156 is_deeply(
157 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
22286063 158 [ Foo->meta, Foo->meta ],
c9e77dbb 159 '... got the right list of associated classes from the applicable attributes for Baz');
160
22286063 161 # remove attribute which is not there
162 my $val;
163 lives_ok {
164 $val = $meta->remove_attribute('$blammo');
165 } '... attempted to remove the non-existent $blammo attribute';
166 is($val, undef, '... got the right value back (undef)');
167
cbd9f942 168}