Include method name in immutable methods (fixes #49680)
[gitmo/Class-MOP.git] / t / 022_attribute_duplication.t
CommitLineData
b1897d4d 1use strict;
2use warnings;
3
742fb371 4use Scalar::Util;
5
efd3d14c 6use Test::More tests => 16;
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;
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');
d14f6cbe 35 ::is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
742fb371 36
b1897d4d 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
d14f6cbe 46 my $bar_attr2 = Foo->meta->get_attribute('bar');
742fb371 47
b1897d4d 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