make CMOP::Class->construct_instance private
[gitmo/Class-MOP.git] / t / 024_attribute_initializer.t
CommitLineData
0ab65f99 1use strict;
2use warnings;
3
4use Scalar::Util 'blessed', 'reftype';
5
efd3d14c 6use Test::More tests => 9;
0ab65f99 7
efd3d14c 8use Class::MOP;
0ab65f99 9
10=pod
11
12This checks that the initializer is used to set the initial value.
13
14=cut
15
16{
17 package Foo;
18 use metaclass;
19
20 Foo->meta->add_attribute('bar' =>
8ee74136 21 reader => 'get_bar',
22 writer => 'set_bar',
0ab65f99 23 initializer => sub {
8ee74136 24 my ($self, $value, $callback, $attr) = @_;
25
26 ::isa_ok($attr, 'Class::MOP::Attribute');
27 ::is($attr->name, 'bar', '... the attribute is our own');
28
29 $callback->($value * 2);
0ab65f99 30 },
31 );
32}
33
34can_ok('Foo', 'get_bar');
35can_ok('Foo', 'set_bar');
36
d69fb6b3 37my $foo = Foo->meta->new_object(bar => 10);
8ee74136 38is($foo->get_bar, 20, "... initial argument was doubled as expected");
39
40$foo->set_bar(30);
41
42is($foo->get_bar, 30, "... and setter works correctly");
43
44# meta tests ...
45
46my $bar = Foo->meta->get_attribute('bar');
47isa_ok($bar, 'Class::MOP::Attribute');
48
49ok($bar->has_initializer, '... bar has an initializer');
50is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref');
51
52
53
54
55
56
57
58
0ab65f99 59