more-method-refactoring
[gitmo/Class-MOP.git] / t / 020_attribute.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 67;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');
11     use_ok('Class::MOP::Attribute');
12 }
13
14 {
15     my $attr = Class::MOP::Attribute->new('$foo');
16     isa_ok($attr, 'Class::MOP::Attribute');
17
18     is($attr->name, '$foo', '... $attr->name == $foo');
19     ok($attr->has_init_arg, '... $attr does have an init_arg');
20     is($attr->init_arg, '$foo', '... $attr init_arg is the name');        
21     
22     ok(!$attr->has_accessor, '... $attr does not have an accessor');
23     ok(!$attr->has_reader, '... $attr does not have an reader');
24     ok(!$attr->has_writer, '... $attr does not have an writer');
25     ok(!$attr->has_default, '... $attr does not have an default');  
26     
27     my $class = Class::MOP::Class->initialize('Foo');
28     isa_ok($class, 'Class::MOP::Class');
29     
30     lives_ok {
31         $attr->attach_to_class($class);
32     } '... attached a class successfully';
33     
34     is($attr->associated_class, $class, '... the class was associated correctly');
35     
36     my $attr_clone = $attr->clone();
37     isa_ok($attr_clone, 'Class::MOP::Attribute');
38     isnt($attr, $attr_clone, '... but they are different instances');
39     
40     is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
41     is($attr->associated_class, $class, '... the associated classes are the same though');    
42     is($attr_clone->associated_class, $class, '... the associated classes are the same though');    
43     
44     is_deeply($attr, $attr_clone, '... but they are the same inside');
45 }
46
47 {
48     my $attr = Class::MOP::Attribute->new('$foo', (
49         init_arg => '-foo',
50         default  => 'BAR'
51     ));
52     isa_ok($attr, 'Class::MOP::Attribute');
53
54     is($attr->name, '$foo', '... $attr->name == $foo');
55     
56     ok($attr->has_init_arg, '... $attr does have an init_arg');
57     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
58     ok($attr->has_default, '... $attr does have an default');    
59     is($attr->default, 'BAR', '... $attr->default == BAR');
60     
61     ok(!$attr->has_accessor, '... $attr does not have an accessor');
62     ok(!$attr->has_reader, '... $attr does not have an reader');
63     ok(!$attr->has_writer, '... $attr does not have an writer');   
64     
65     my $attr_clone = $attr->clone();
66     isa_ok($attr_clone, 'Class::MOP::Attribute');
67     isnt($attr, $attr_clone, '... but they are different instances');
68     
69     is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
70     is($attr->associated_class, undef, '... the associated class is actually undef');    
71     is($attr_clone->associated_class, undef, '... the associated class is actually undef');    
72     
73     is_deeply($attr, $attr_clone, '... but they are the same inside');                
74 }
75
76 {
77     my $attr = Class::MOP::Attribute->new('$foo', (
78         accessor => 'foo',
79         init_arg => '-foo',
80         default  => 'BAR'
81     ));
82     isa_ok($attr, 'Class::MOP::Attribute');
83
84     is($attr->name, '$foo', '... $attr->name == $foo');
85     
86     ok($attr->has_init_arg, '... $attr does have an init_arg');
87     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
88     ok($attr->has_default, '... $attr does have an default');    
89     is($attr->default, 'BAR', '... $attr->default == BAR');
90
91     ok($attr->has_accessor, '... $attr does have an accessor');    
92     is($attr->accessor, 'foo', '... $attr->accessor == foo');
93     
94     ok(!$attr->has_reader, '... $attr does not have an reader');
95     ok(!$attr->has_writer, '... $attr does not have an writer');   
96     
97     my $attr_clone = $attr->clone();
98     isa_ok($attr_clone, 'Class::MOP::Attribute');
99     isnt($attr, $attr_clone, '... but they are different instances');
100     
101     is_deeply($attr, $attr_clone, '... but they are the same inside');                
102 }
103
104 {
105     my $attr = Class::MOP::Attribute->new('$foo', (
106         reader   => 'get_foo',
107         writer   => 'set_foo',        
108         init_arg => '-foo',
109         default  => 'BAR'
110     ));
111     isa_ok($attr, 'Class::MOP::Attribute');
112
113     is($attr->name, '$foo', '... $attr->name == $foo');
114     
115     ok($attr->has_init_arg, '... $attr does have an init_arg');
116     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
117     ok($attr->has_default, '... $attr does have an default');    
118     is($attr->default, 'BAR', '... $attr->default == BAR');
119
120     ok($attr->has_reader, '... $attr does have an reader');
121     is($attr->reader, 'get_foo', '... $attr->reader == get_foo');    
122     ok($attr->has_writer, '... $attr does have an writer');
123     is($attr->writer, 'set_foo', '... $attr->writer == set_foo');    
124
125     ok(!$attr->has_accessor, '... $attr does not have an accessor'); 
126     
127     my $attr_clone = $attr->clone();
128     isa_ok($attr_clone, 'Class::MOP::Attribute');
129     isnt($attr, $attr_clone, '... but they are different instances');
130     
131     is_deeply($attr, $attr_clone, '... but they are the same inside');       
132 }
133
134 {
135     my $attr = Class::MOP::Attribute->new('$foo');
136     isa_ok($attr, 'Class::MOP::Attribute');
137     
138     my $attr_clone = $attr->clone('name' => '$bar');
139     isa_ok($attr_clone, 'Class::MOP::Attribute');
140     isnt($attr, $attr_clone, '... but they are different instances');
141     
142     isnt($attr->name, $attr_clone->name, '... we changes the name parameter');
143     
144     is($attr->name, '$foo', '... $attr->name == $foo');
145     is($attr_clone->name, '$bar', '... $attr_clone->name == $bar');    
146 }
147