File: 024_attribute_initializer.t

package info (click to toggle)
libclass-mop-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,244 kB
  • ctags: 1,272
  • sloc: perl: 5,192; ansic: 241; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,090 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use strict;
use warnings;

use Scalar::Util 'blessed', 'reftype';

use Test::More;

use Class::MOP;

=pod

This checks that the initializer is used to set the initial value.

=cut

{
    package Foo;
    use metaclass;

    Foo->meta->add_attribute('bar' =>
        reader      => 'get_bar',
        writer      => 'set_bar',
        initializer => sub {
            my ($self, $value, $callback, $attr) = @_;

            ::isa_ok($attr, 'Class::MOP::Attribute');
            ::is($attr->name, 'bar', '... the attribute is our own');

            $callback->($value * 2);
        },
    );
}

can_ok('Foo', 'get_bar');
can_ok('Foo', 'set_bar');

my $foo = Foo->meta->new_object(bar => 10);
is($foo->get_bar, 20, "... initial argument was doubled as expected");

$foo->set_bar(30);

is($foo->get_bar, 30, "... and setter works correctly");

# meta tests ...

my $bar = Foo->meta->get_attribute('bar');
isa_ok($bar, 'Class::MOP::Attribute');

ok($bar->has_initializer, '... bar has an initializer');
is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref');

done_testing;