Commit | Line | Data |
38bf2a25 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Scalar::Util; |
5 | |
6 | use Test::More; |
7 | |
8 | use Class::MOP; |
9 | |
10 | =pod |
11 | |
12 | This tests that when an attribute of the same name |
13 | is added to a class, that it will remove the old |
14 | one first. |
15 | |
16 | =cut |
17 | |
18 | { |
19 | package Foo; |
20 | use metaclass; |
21 | |
22 | Foo->meta->add_attribute('bar' => |
23 | reader => 'get_bar', |
24 | writer => 'set_bar', |
25 | ); |
26 | |
27 | ::can_ok('Foo', 'get_bar'); |
28 | ::can_ok('Foo', 'set_bar'); |
29 | ::ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar'); |
30 | |
31 | my $bar_attr = Foo->meta->get_attribute('bar'); |
32 | |
33 | ::is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar'); |
34 | ::is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar'); |
35 | ::is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta'); |
36 | |
37 | Foo->meta->add_attribute('bar' => |
38 | reader => 'assign_bar' |
39 | ); |
40 | |
41 | ::ok(!Foo->can('get_bar'), '... Foo no longer has the get_bar method'); |
42 | ::ok(!Foo->can('set_bar'), '... Foo no longer has the set_bar method'); |
43 | ::can_ok('Foo', 'assign_bar'); |
44 | ::ok(Foo->meta->has_attribute('bar'), '... Foo still has the attribute bar'); |
45 | |
46 | my $bar_attr2 = Foo->meta->get_attribute('bar'); |
47 | |
48 | ::isnt($bar_attr, $bar_attr2, '... this is a new bar attribute'); |
49 | ::isnt($bar_attr->associated_class, Foo->meta, '... and the old bar attribute is no longer associated with Foo->meta'); |
50 | |
51 | ::is($bar_attr2->associated_class, Foo->meta, '... and the new bar attribute *is* associated with Foo->meta'); |
52 | |
53 | ::isnt($bar_attr2->reader, 'get_bar', '... the bar attribute no longer has the reader get_bar'); |
54 | ::isnt($bar_attr2->reader, 'set_bar', '... the bar attribute no longer has the reader set_bar'); |
55 | ::is($bar_attr2->reader, 'assign_bar', '... the bar attribute now has the reader assign_bar'); |
56 | } |
57 | |
58 | done_testing; |