We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / cmop / attribute_initializer.t
CommitLineData
38bf2a25 1use strict;
2use warnings;
3
4use Scalar::Util 'blessed', 'reftype';
5
6use Test::More;
7
8use Class::MOP;
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' =>
21 reader => 'get_bar',
22 writer => 'set_bar',
23 initializer => sub {
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);
30 },
31 );
32}
33
34can_ok('Foo', 'get_bar');
35can_ok('Foo', 'set_bar');
36
37my $foo = Foo->meta->new_object(bar => 10);
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
52done_testing;