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