4 use Scalar::Util 'reftype', 'blessed';
6 use Test::More tests => 100;
10 use Class::MOP::Attribute;
13 dies_ok { Class::MOP::Attribute->name } q{... can't call name() as a class method};
17 my $attr = Class::MOP::Attribute->new('$foo');
18 isa_ok($attr, 'Class::MOP::Attribute');
20 is($attr->name, '$foo', '... $attr->name == $foo');
21 ok($attr->has_init_arg, '... $attr does have an init_arg');
22 is($attr->init_arg, '$foo', '... $attr init_arg is the name');
24 ok(!$attr->has_accessor, '... $attr does not have an accessor');
25 ok(!$attr->has_reader, '... $attr does not have an reader');
26 ok(!$attr->has_writer, '... $attr does not have an writer');
27 ok(!$attr->has_default, '... $attr does not have an default');
28 ok(!$attr->has_builder, '... $attr does not have a builder');
31 my $reader = $attr->get_read_method_ref;
32 my $writer = $attr->get_write_method_ref;
34 ok(!blessed($reader), '... it is a plain old sub');
35 ok(!blessed($writer), '... it is a plain old sub');
37 is(reftype($reader), 'CODE', '... it is a plain old sub');
38 is(reftype($writer), 'CODE', '... it is a plain old sub');
41 my $class = Class::MOP::Class->initialize('Foo');
42 isa_ok($class, 'Class::MOP::Class');
45 $attr->attach_to_class($class);
46 } '... attached a class successfully';
48 is($attr->associated_class, $class, '... the class was associated correctly');
50 ok(!$attr->get_read_method, '... $attr does not have an read method');
51 ok(!$attr->get_write_method, '... $attr does not have an write method');
54 my $reader = $attr->get_read_method_ref;
55 my $writer = $attr->get_write_method_ref;
57 ok(blessed($reader), '... it is a plain old sub');
58 ok(blessed($writer), '... it is a plain old sub');
60 isa_ok($reader, 'Class::MOP::Method');
61 isa_ok($writer, 'Class::MOP::Method');
64 my $attr_clone = $attr->clone();
65 isa_ok($attr_clone, 'Class::MOP::Attribute');
66 isnt($attr, $attr_clone, '... but they are different instances');
68 is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
69 is($attr->associated_class, $class, '... the associated classes are the same though');
70 is($attr_clone->associated_class, $class, '... the associated classes are the same though');
72 is_deeply($attr, $attr_clone, '... but they are the same inside');
76 my $attr = Class::MOP::Attribute->new('$foo', (
80 isa_ok($attr, 'Class::MOP::Attribute');
82 is($attr->name, '$foo', '... $attr->name == $foo');
84 ok($attr->has_init_arg, '... $attr does have an init_arg');
85 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
86 ok($attr->has_default, '... $attr does have an default');
87 is($attr->default, 'BAR', '... $attr->default == BAR');
88 ok(!$attr->has_builder, '... $attr does not have a builder');
90 ok(!$attr->has_accessor, '... $attr does not have an accessor');
91 ok(!$attr->has_reader, '... $attr does not have an reader');
92 ok(!$attr->has_writer, '... $attr does not have an writer');
94 ok(!$attr->get_read_method, '... $attr does not have an read method');
95 ok(!$attr->get_write_method, '... $attr does not have an write method');
98 my $reader = $attr->get_read_method_ref;
99 my $writer = $attr->get_write_method_ref;
101 ok(!blessed($reader), '... it is a plain old sub');
102 ok(!blessed($writer), '... it is a plain old sub');
104 is(reftype($reader), 'CODE', '... it is a plain old sub');
105 is(reftype($writer), 'CODE', '... it is a plain old sub');
108 my $attr_clone = $attr->clone();
109 isa_ok($attr_clone, 'Class::MOP::Attribute');
110 isnt($attr, $attr_clone, '... but they are different instances');
112 is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
113 is($attr->associated_class, undef, '... the associated class is actually undef');
114 is($attr_clone->associated_class, undef, '... the associated class is actually undef');
116 is_deeply($attr, $attr_clone, '... but they are the same inside');
120 my $attr = Class::MOP::Attribute->new('$foo', (
125 isa_ok($attr, 'Class::MOP::Attribute');
127 is($attr->name, '$foo', '... $attr->name == $foo');
129 ok($attr->has_init_arg, '... $attr does have an init_arg');
130 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
131 ok($attr->has_default, '... $attr does have an default');
132 is($attr->default, 'BAR', '... $attr->default == BAR');
134 ok($attr->has_accessor, '... $attr does have an accessor');
135 is($attr->accessor, 'foo', '... $attr->accessor == foo');
137 ok(!$attr->has_reader, '... $attr does not have an reader');
138 ok(!$attr->has_writer, '... $attr does not have an writer');
140 is($attr->get_read_method, 'foo', '... $attr does not have an read method');
141 is($attr->get_write_method, 'foo', '... $attr does not have an write method');
144 my $reader = $attr->get_read_method_ref;
145 my $writer = $attr->get_write_method_ref;
147 ok(!blessed($reader), '... it is not a plain old sub');
148 ok(!blessed($writer), '... it is not a plain old sub');
150 is(reftype($reader), 'CODE', '... it is a plain old sub');
151 is(reftype($writer), 'CODE', '... it is a plain old sub');
154 my $attr_clone = $attr->clone();
155 isa_ok($attr_clone, 'Class::MOP::Attribute');
156 isnt($attr, $attr_clone, '... but they are different instances');
158 is_deeply($attr, $attr_clone, '... but they are the same inside');
162 my $attr = Class::MOP::Attribute->new('$foo', (
168 isa_ok($attr, 'Class::MOP::Attribute');
170 is($attr->name, '$foo', '... $attr->name == $foo');
172 ok($attr->has_init_arg, '... $attr does have an init_arg');
173 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
174 ok($attr->has_default, '... $attr does have an default');
175 is($attr->default, 'BAR', '... $attr->default == BAR');
177 ok($attr->has_reader, '... $attr does have an reader');
178 is($attr->reader, 'get_foo', '... $attr->reader == get_foo');
179 ok($attr->has_writer, '... $attr does have an writer');
180 is($attr->writer, 'set_foo', '... $attr->writer == set_foo');
182 ok(!$attr->has_accessor, '... $attr does not have an accessor');
184 is($attr->get_read_method, 'get_foo', '... $attr does not have an read method');
185 is($attr->get_write_method, 'set_foo', '... $attr does not have an write method');
188 my $reader = $attr->get_read_method_ref;
189 my $writer = $attr->get_write_method_ref;
191 ok(!blessed($reader), '... it is not a plain old sub');
192 ok(!blessed($writer), '... it is not a plain old sub');
194 is(reftype($reader), 'CODE', '... it is a plain old sub');
195 is(reftype($writer), 'CODE', '... it is a plain old sub');
198 my $attr_clone = $attr->clone();
199 isa_ok($attr_clone, 'Class::MOP::Attribute');
200 isnt($attr, $attr_clone, '... but they are different instances');
202 is_deeply($attr, $attr_clone, '... but they are the same inside');
206 my $attr = Class::MOP::Attribute->new('$foo');
207 isa_ok($attr, 'Class::MOP::Attribute');
209 my $attr_clone = $attr->clone('name' => '$bar');
210 isa_ok($attr_clone, 'Class::MOP::Attribute');
211 isnt($attr, $attr_clone, '... but they are different instances');
213 isnt($attr->name, $attr_clone->name, '... we changes the name parameter');
215 is($attr->name, '$foo', '... $attr->name == $foo');
216 is($attr_clone->name, '$bar', '... $attr_clone->name == $bar');
220 my $attr = Class::MOP::Attribute->new('$foo', (builder => 'foo_builder'));
221 isa_ok($attr, 'Class::MOP::Attribute');
223 ok(!$attr->has_default, '... $attr does not have a default');
224 ok($attr->has_builder, '... $attr does have a builder');
225 is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder');