File: 022_attribute_duplication.t

package info (click to toggle)
libclass-mop-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,244 kB
  • ctags: 1,272
  • sloc: perl: 5,192; ansic: 241; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,856 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use warnings;

use Scalar::Util;

use Test::More;

use Class::MOP;

=pod

This tests that when an attribute of the same name
is added to a class, that it will remove the old
one first.

=cut

{
    package Foo;
    use metaclass;

    Foo->meta->add_attribute('bar' =>
        reader => 'get_bar',
        writer => 'set_bar',
    );

    ::can_ok('Foo', 'get_bar');
    ::can_ok('Foo', 'set_bar');
    ::ok(Foo->meta->has_attribute('bar'), '... Foo has the attribute bar');

    my $bar_attr = Foo->meta->get_attribute('bar');

    ::is($bar_attr->reader, 'get_bar', '... the bar attribute has the reader get_bar');
    ::is($bar_attr->writer, 'set_bar', '... the bar attribute has the writer set_bar');
    ::is($bar_attr->associated_class, Foo->meta, '... and the bar attribute is associated with Foo->meta');

    Foo->meta->add_attribute('bar' =>
        reader => 'assign_bar'
    );

    ::ok(!Foo->can('get_bar'), '... Foo no longer has the get_bar method');
    ::ok(!Foo->can('set_bar'), '... Foo no longer has the set_bar method');
    ::can_ok('Foo', 'assign_bar');
    ::ok(Foo->meta->has_attribute('bar'), '... Foo still has the attribute bar');

    my $bar_attr2 = Foo->meta->get_attribute('bar');

    ::isnt($bar_attr, $bar_attr2, '... this is a new bar attribute');
    ::isnt($bar_attr->associated_class, Foo->meta, '... and the old bar attribute is no longer associated with Foo->meta');

    ::is($bar_attr2->associated_class, Foo->meta, '... and the new bar attribute *is* associated with Foo->meta');

    ::isnt($bar_attr2->reader, 'get_bar', '... the bar attribute no longer has the reader get_bar');
    ::isnt($bar_attr2->reader, 'set_bar', '... the bar attribute no longer has the reader set_bar');
    ::is($bar_attr2->reader, 'assign_bar', '... the bar attribute now has the reader assign_bar');
}

done_testing;