cf39b7aff7e86e18093164a8ef47fbdd815e5caf
[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 => 4;
9
10 BEGIN {
11     use_ok('Class::MOP');
12 }
13
14 =pod
15
16 This checks that the initializer is used to set the initial value.
17
18 =cut
19
20 {
21     package Foo;
22     use metaclass;
23     
24     Foo->meta->add_attribute('bar' => 
25         reader => 'get_bar',
26         writer => 'set_bar',
27         initializer => sub {
28           my ($self, $value, $name, $callback) = @_;
29           $callback->($value * 2);
30         },
31     );  
32 }
33
34 can_ok('Foo', 'get_bar');
35 can_ok('Foo', 'set_bar');    
36
37 my $foo = Foo->meta->construct_instance(bar => 10);
38 is(
39   $foo->get_bar,
40   20,
41   "initial argument was doubled as expected",
42 );
43