give the initializer a test
[gitmo/Class-MOP.git] / t / 024_attribute_initializer.t
CommitLineData
0ab65f99 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Scalar::Util 'blessed', 'reftype';
7
8use Test::More tests => 4;
9
10BEGIN {
11 use_ok('Class::MOP');
12}
13
14=pod
15
16This 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 {
ed8563cb 28 my ($self, $value, $name, $callback) = @_;
0ab65f99 29 $callback->($value * 2);
30 },
31 );
32}
33
34can_ok('Foo', 'get_bar');
35can_ok('Foo', 'set_bar');
36
37my $foo = Foo->meta->construct_instance(bar => 10);
38is(
39 $foo->get_bar,
40 20,
41 "initial argument was doubled as expected",
42);
43