Commit | Line | Data |
0ab65f99 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Scalar::Util 'blessed', 'reftype'; |
7 | |
8ee74136 |
8 | use Test::More tests => 10; |
0ab65f99 |
9 | |
10 | BEGIN { |
11 | use_ok('Class::MOP'); |
12 | } |
13 | |
14 | =pod |
15 | |
16 | This checks that the initializer is used to set the initial value. |
17 | |
18 | =cut |
19 | |
20 | { |
21 | package Foo; |
22 | use metaclass; |
23 | |
24 | Foo->meta->add_attribute('bar' => |
8ee74136 |
25 | reader => 'get_bar', |
26 | writer => 'set_bar', |
0ab65f99 |
27 | initializer => sub { |
8ee74136 |
28 | my ($self, $value, $callback, $attr) = @_; |
29 | |
30 | ::isa_ok($attr, 'Class::MOP::Attribute'); |
31 | ::is($attr->name, 'bar', '... the attribute is our own'); |
32 | |
33 | $callback->($value * 2); |
0ab65f99 |
34 | }, |
35 | ); |
36 | } |
37 | |
38 | can_ok('Foo', 'get_bar'); |
39 | can_ok('Foo', 'set_bar'); |
40 | |
41 | my $foo = Foo->meta->construct_instance(bar => 10); |
8ee74136 |
42 | is($foo->get_bar, 20, "... initial argument was doubled as expected"); |
43 | |
44 | $foo->set_bar(30); |
45 | |
46 | is($foo->get_bar, 30, "... and setter works correctly"); |
47 | |
48 | # meta tests ... |
49 | |
50 | my $bar = Foo->meta->get_attribute('bar'); |
51 | isa_ok($bar, 'Class::MOP::Attribute'); |
52 | |
53 | ok($bar->has_initializer, '... bar has an initializer'); |
54 | is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref'); |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
0ab65f99 |
63 | |