builder changes. sorry about diff noise, my editor ate trailing whitespace :(
[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 => 73;
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     ok(!$attr->has_builder, '... $attr does not have a builder');
27
28     my $class = Class::MOP::Class->initialize('Foo');
29     isa_ok($class, 'Class::MOP::Class');
30
31     lives_ok {
32         $attr->attach_to_class($class);
33     } '... attached a class successfully';
34
35     is($attr->associated_class, $class, '... the class was associated correctly');
36
37     my $attr_clone = $attr->clone();
38     isa_ok($attr_clone, 'Class::MOP::Attribute');
39     isnt($attr, $attr_clone, '... but they are different instances');
40
41     is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
42     is($attr->associated_class, $class, '... the associated classes are the same though');
43     is($attr_clone->associated_class, $class, '... the associated classes are the same though');
44
45     is_deeply($attr, $attr_clone, '... but they are the same inside');
46 }
47
48 {
49     my $attr = Class::MOP::Attribute->new('$foo', (
50         init_arg => '-foo',
51         default  => 'BAR'
52     ));
53     isa_ok($attr, 'Class::MOP::Attribute');
54
55     is($attr->name, '$foo', '... $attr->name == $foo');
56
57     ok($attr->has_init_arg, '... $attr does have an init_arg');
58     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
59     ok($attr->has_default, '... $attr does have an default');
60     is($attr->default, 'BAR', '... $attr->default == BAR');
61     ok(!$attr->has_builder, '... $attr does not have a builder');
62
63     ok(!$attr->has_accessor, '... $attr does not have an accessor');
64     ok(!$attr->has_reader, '... $attr does not have an reader');
65     ok(!$attr->has_writer, '... $attr does not have an writer');
66
67     my $attr_clone = $attr->clone();
68     isa_ok($attr_clone, 'Class::MOP::Attribute');
69     isnt($attr, $attr_clone, '... but they are different instances');
70
71     is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
72     is($attr->associated_class, undef, '... the associated class is actually undef');
73     is($attr_clone->associated_class, undef, '... the associated class is actually undef');
74
75     is_deeply($attr, $attr_clone, '... but they are the same inside');
76 }
77
78 {
79     my $attr = Class::MOP::Attribute->new('$foo', (
80         accessor => 'foo',
81         init_arg => '-foo',
82         default  => 'BAR'
83     ));
84     isa_ok($attr, 'Class::MOP::Attribute');
85
86     is($attr->name, '$foo', '... $attr->name == $foo');
87
88     ok($attr->has_init_arg, '... $attr does have an init_arg');
89     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
90     ok($attr->has_default, '... $attr does have an default');
91     is($attr->default, 'BAR', '... $attr->default == BAR');
92
93     ok($attr->has_accessor, '... $attr does have an accessor');
94     is($attr->accessor, 'foo', '... $attr->accessor == foo');
95
96     ok(!$attr->has_reader, '... $attr does not have an reader');
97     ok(!$attr->has_writer, '... $attr does not have an writer');
98
99     my $attr_clone = $attr->clone();
100     isa_ok($attr_clone, 'Class::MOP::Attribute');
101     isnt($attr, $attr_clone, '... but they are different instances');
102
103     is_deeply($attr, $attr_clone, '... but they are the same inside');
104 }
105
106 {
107     my $attr = Class::MOP::Attribute->new('$foo', (
108         reader   => 'get_foo',
109         writer   => 'set_foo',
110         init_arg => '-foo',
111         default  => 'BAR'
112     ));
113     isa_ok($attr, 'Class::MOP::Attribute');
114
115     is($attr->name, '$foo', '... $attr->name == $foo');
116
117     ok($attr->has_init_arg, '... $attr does have an init_arg');
118     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
119     ok($attr->has_default, '... $attr does have an default');
120     is($attr->default, 'BAR', '... $attr->default == BAR');
121
122     ok($attr->has_reader, '... $attr does have an reader');
123     is($attr->reader, 'get_foo', '... $attr->reader == get_foo');
124     ok($attr->has_writer, '... $attr does have an writer');
125     is($attr->writer, 'set_foo', '... $attr->writer == set_foo');
126
127     ok(!$attr->has_accessor, '... $attr does not have an accessor');
128
129     my $attr_clone = $attr->clone();
130     isa_ok($attr_clone, 'Class::MOP::Attribute');
131     isnt($attr, $attr_clone, '... but they are different instances');
132
133     is_deeply($attr, $attr_clone, '... but they are the same inside');
134 }
135
136 {
137     my $attr = Class::MOP::Attribute->new('$foo');
138     isa_ok($attr, 'Class::MOP::Attribute');
139
140     my $attr_clone = $attr->clone('name' => '$bar');
141     isa_ok($attr_clone, 'Class::MOP::Attribute');
142     isnt($attr, $attr_clone, '... but they are different instances');
143
144     isnt($attr->name, $attr_clone->name, '... we changes the name parameter');
145
146     is($attr->name, '$foo', '... $attr->name == $foo');
147     is($attr_clone->name, '$bar', '... $attr_clone->name == $bar');
148 }
149
150 {
151     my $attr = Class::MOP::Attribute->new('$foo', (builder => 'foo_builder'));
152     isa_ok($attr, 'Class::MOP::Attribute');
153
154     ok(!$attr->has_default, '... $attr does not have a default');
155     ok($attr->has_builder, '... $attr does have a builder');
156     is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder');
157
158 }