Commit | Line | Data |
0ab65f99 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Scalar::Util 'blessed', 'reftype'; |
7 | |
efd3d14c |
8 | use Test::More tests => 9; |
0ab65f99 |
9 | |
efd3d14c |
10 | use Class::MOP; |
0ab65f99 |
11 | |
12 | =pod |
13 | |
14 | This 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 | |
36 | can_ok('Foo', 'get_bar'); |
37 | can_ok('Foo', 'set_bar'); |
38 | |
39 | my $foo = Foo->meta->construct_instance(bar => 10); |
8ee74136 |
40 | is($foo->get_bar, 20, "... initial argument was doubled as expected"); |
41 | |
42 | $foo->set_bar(30); |
43 | |
44 | is($foo->get_bar, 30, "... and setter works correctly"); |
45 | |
46 | # meta tests ... |
47 | |
48 | my $bar = Foo->meta->get_attribute('bar'); |
49 | isa_ok($bar, 'Class::MOP::Attribute'); |
50 | |
51 | ok($bar->has_initializer, '... bar has an initializer'); |
52 | is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref'); |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
0ab65f99 |
61 | |