One last tweak to make sure our Sub::Name-using tests _do_ run when we
[gitmo/Class-MOP.git] / t / 024_attribute_initializer.t
CommitLineData
0ab65f99 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Scalar::Util 'blessed', 'reftype';
7
8ee74136 8use Test::More tests => 10;
0ab65f99 9
10BEGIN {
11 use_ok('Class::MOP');
12}
13
14=pod
15
16This 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
38can_ok('Foo', 'get_bar');
39can_ok('Foo', 'set_bar');
40
41my $foo = Foo->meta->construct_instance(bar => 10);
8ee74136 42is($foo->get_bar, 20, "... initial argument was doubled as expected");
43
44$foo->set_bar(30);
45
46is($foo->get_bar, 30, "... and setter works correctly");
47
48# meta tests ...
49
50my $bar = Foo->meta->get_attribute('bar');
51isa_ok($bar, 'Class::MOP::Attribute');
52
53ok($bar->has_initializer, '... bar has an initializer');
54is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref');
55
56
57
58
59
60
61
62
0ab65f99 63