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