Commit | Line | Data |
38bf2a25 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Scalar::Util 'blessed', 'reftype'; |
5 | |
6 | use Test::More; |
7 | |
8 | use Class::MOP; |
9 | |
10 | =pod |
11 | |
12 | This 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 | |
34 | can_ok('Foo', 'get_bar'); |
35 | can_ok('Foo', 'set_bar'); |
36 | |
37 | my $foo = Foo->meta->new_object(bar => 10); |
38 | is($foo->get_bar, 20, "... initial argument was doubled as expected"); |
39 | |
40 | $foo->set_bar(30); |
41 | |
42 | is($foo->get_bar, 30, "... and setter works correctly"); |
43 | |
44 | # meta tests ... |
45 | |
46 | my $bar = Foo->meta->get_attribute('bar'); |
47 | isa_ok($bar, 'Class::MOP::Attribute'); |
48 | |
49 | ok($bar->has_initializer, '... bar has an initializer'); |
50 | is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref'); |
51 | |
52 | done_testing; |