Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 022_attribute_duplication.t
CommitLineData
b1897d4d 1use strict;
2use warnings;
3
742fb371 4use Scalar::Util;
5
86a4d873 6use Test::More;
b1897d4d 7
efd3d14c 8use Class::MOP;
b1897d4d 9
10=pod
11
12This tests that when an attribute of the same name
13is added to a class, that it will remove the old
14one first.
15
16=cut
17
18{
19 package Foo;
20 use metaclass;
86a4d873 21
22 Foo->meta->add_attribute('bar' =>
b1897d4d 23 reader => 'get_bar',
24 writer => 'set_bar',
25 );
86a4d873 26
b1897d4d 27 ::can_ok('Foo', 'get_bar');
86a4d873 28 ::can_ok('Foo', 'set_bar');
b1897d4d 29 ::ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar');
86a4d873 30
b1897d4d 31 my $bar_attr = Foo->meta->get_attribute('bar');
86a4d873 32
b1897d4d 33 ::is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar');
86a4d873 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' =>
b1897d4d 38 reader => 'assign_bar'
86a4d873 39 );
b1897d4d 40
41 ::ok(!Foo->can('get_bar'), '... Foo no longer has the get_bar method');
86a4d873 42 ::ok(!Foo->can('set_bar'), '... Foo no longer has the set_bar method');
43 ::can_ok('Foo', 'assign_bar');
b1897d4d 44 ::ok(Foo->meta->has_attribute('bar'), '... Foo still has the attribute bar');
86a4d873 45
46 my $bar_attr2 = Foo->meta->get_attribute('bar');
47
b1897d4d 48 ::isnt($bar_attr, $bar_attr2, '... this is a new bar attribute');
86a4d873 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
b1897d4d 53 ::isnt($bar_attr2->reader, 'get_bar', '... the bar attribute no longer has the reader get_bar');
86a4d873 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');
b1897d4d 56}
57
86a4d873 58done_testing;