more tests
[gitmo/Class-MOP.git] / t / 022_attribute_duplication.t
CommitLineData
b1897d4d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
742fb371 6use Scalar::Util;
7
78d9bb38 8use Test::More tests => 32;
b1897d4d 9
10BEGIN {
11 use_ok('Class::MOP');
12}
13
14=pod
15
16This tests that when an attribute of the same name
17is added to a class, that it will remove the old
18one first.
19
20=cut
21
22{
23 package Foo;
24 use metaclass;
25
26 Foo->meta->add_attribute('bar' =>
27 reader => 'get_bar',
28 writer => 'set_bar',
29 );
30
31 ::can_ok('Foo', 'get_bar');
32 ::can_ok('Foo', 'set_bar');
33 ::ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar');
34
35 my $bar_attr = Foo->meta->get_attribute('bar');
36
37 ::is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar');
38 ::is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar');
39 ::is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');
40
742fb371 41 ::is($bar_attr->get_read_method, 'get_bar', '... $attr does have an read method');
42 ::is($bar_attr->get_write_method, 'set_bar', '... $attr does have an write method');
43
44 {
45 my $reader = $bar_attr->get_read_method_ref;
46 my $writer = $bar_attr->get_write_method_ref;
47
48 ::isa_ok($reader, 'Class::MOP::Method');
49 ::isa_ok($writer, 'Class::MOP::Method');
78d9bb38 50
51 ::is($reader->fully_qualified_name, 'Foo::get_bar', '... it is the sub we are looking for');
52 ::is($writer->fully_qualified_name, 'Foo::set_bar', '... it is the sub we are looking for');
742fb371 53
54 ::is(Scalar::Util::reftype($reader->body), 'CODE', '... it is a plain old sub');
55 ::is(Scalar::Util::reftype($writer->body), 'CODE', '... it is a plain old sub');
56 }
57
b1897d4d 58 Foo->meta->add_attribute('bar' =>
59 reader => 'assign_bar'
60 );
61
62 ::ok(!Foo->can('get_bar'), '... Foo no longer has the get_bar method');
63 ::ok(!Foo->can('set_bar'), '... Foo no longer has the set_bar method');
64 ::can_ok('Foo', 'assign_bar');
65 ::ok(Foo->meta->has_attribute('bar'), '... Foo still has the attribute bar');
66
67 my $bar_attr2 = Foo->meta->get_attribute('bar');
68
742fb371 69 ::is($bar_attr2->get_read_method, 'assign_bar', '... $attr does have an read method');
70 ::ok(!$bar_attr2->get_write_method, '... $attr does have an write method');
71
72 {
73 my $reader = $bar_attr2->get_read_method_ref;
74 my $writer = $bar_attr2->get_write_method_ref;
75
76 ::isa_ok($reader, 'Class::MOP::Method');
78d9bb38 77 ::ok(!Scalar::Util::blessed($writer), '... the writer method is not blessed though');
78
79 ::is($reader->fully_qualified_name, 'Foo::assign_bar', '... it is the sub we are looking for');
742fb371 80
81 ::is(Scalar::Util::reftype($reader->body), 'CODE', '... it is a plain old sub');
82 ::is(Scalar::Util::reftype($writer), 'CODE', '... it is a plain old sub');
83 }
84
b1897d4d 85 ::isnt($bar_attr, $bar_attr2, '... this is a new bar attribute');
86 ::isnt($bar_attr->associated_class, Foo->meta, '... and the old bar attribute is no longer associated with Foo->meta');
87
88 ::is($bar_attr2->associated_class, Foo->meta, '... and the new bar attribute *is* associated with Foo->meta');
89
90 ::isnt($bar_attr2->reader, 'get_bar', '... the bar attribute no longer has the reader get_bar');
91 ::isnt($bar_attr2->reader, 'set_bar', '... the bar attribute no longer has the reader set_bar');
92 ::is($bar_attr2->reader, 'assign_bar', '... the bar attribute now has the reader assign_bar');
93}
94