Document XS get_method_map
[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
efd3d14c 8use Test::More tests => 9;
0ab65f99 9
efd3d14c 10use Class::MOP;
0ab65f99 11
12=pod
13
14This 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' =>
8ee74136 23 reader => 'get_bar',
24 writer => 'set_bar',
0ab65f99 25 initializer => sub {
8ee74136 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);
0ab65f99 32 },
33 );
34}
35
36can_ok('Foo', 'get_bar');
37can_ok('Foo', 'set_bar');
38
39my $foo = Foo->meta->construct_instance(bar => 10);
8ee74136 40is($foo->get_bar, 20, "... initial argument was doubled as expected");
41
42$foo->set_bar(30);
43
44is($foo->get_bar, 30, "... and setter works correctly");
45
46# meta tests ...
47
48my $bar = Foo->meta->get_attribute('bar');
49isa_ok($bar, 'Class::MOP::Attribute');
50
51ok($bar->has_initializer, '... bar has an initializer');
52is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref');
53
54
55
56
57
58
59
60
0ab65f99 61