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