Commit | Line | Data |
0ab65f99 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Scalar::Util 'blessed', 'reftype'; |
5 | |
efd3d14c |
6 | use Test::More tests => 9; |
0ab65f99 |
7 | |
efd3d14c |
8 | use Class::MOP; |
0ab65f99 |
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' => |
8ee74136 |
21 | reader => 'get_bar', |
22 | writer => 'set_bar', |
0ab65f99 |
23 | initializer => sub { |
8ee74136 |
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); |
0ab65f99 |
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); |
8ee74136 |
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 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
0ab65f99 |
59 | |