Class::MOP - getting there
[gitmo/Class-MOP.git] / t / 005_attributes.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More no_plan => 1;
7 use Test::Exception;
8
9 BEGIN { 
10     use_ok('Class::MOP', ':universal'); 
11 }
12
13 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
14 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
15     accessor => 'bar'
16 ));
17 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
18     reader => 'get_baz',
19     writer => 'set_baz',    
20 ));
21
22 {
23     package Foo;
24
25     my $meta = __PACKAGE__->meta;
26     ::lives_ok {
27         $meta->add_attribute($FOO_ATTR);
28     } '... we added an attribute to Foo successfully';
29     ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
30     ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
31     
32     ::ok(!$meta->has_method('foo'), '... no accessor created');
33 }
34 {
35     package Bar;
36     our @ISA = ('Foo');
37     
38     my $meta = __PACKAGE__->meta;
39     ::lives_ok {
40         $meta->add_attribute($BAR_ATTR);
41     } '... we added an attribute to Bar successfully';
42     ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
43     ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
44
45     ::ok($meta->has_method('bar'), '... an accessor has been created');
46     ::isa_ok($meta->get_method('bar'), 'Class::MOP::Attribute::Accessor');    
47 }
48 {
49     package Baz;
50     our @ISA = ('Bar');
51     
52     my $meta = __PACKAGE__->meta;
53     ::lives_ok {
54         $meta->add_attribute($BAZ_ATTR);
55     } '... we added an attribute to Baz successfully';
56     ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');    
57     ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
58
59     ::ok($meta->has_method('get_baz'), '... a reader has been created');
60     ::ok($meta->has_method('set_baz'), '... a writer has been created');
61
62     ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Attribute::Accessor');
63     ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Attribute::Accessor');
64 }
65
66 {
67     my $meta = Baz->meta;
68     isa_ok($meta, 'Class::MOP::Class');
69     
70     is_deeply(
71         [ sort { $a->{name} cmp $b->{name} } $meta->compute_all_applicable_attributes() ],
72         [ 
73             {
74                 name      => '$bar',
75                 class     => 'Bar',
76                 attribute => $BAR_ATTR
77             },
78             {
79                 name      => '$baz',
80                 class     => 'Baz',
81                 attribute => $BAZ_ATTR
82             },
83             {
84                 name      => '$foo',
85                 class     => 'Foo',
86                 attribute => $FOO_ATTR
87             },                        
88         ],
89         '... got the right list of applicable attributes for Baz');
90 }
91
92