fixing tests
[gitmo/Moose.git] / t / 020_attributes / 019_attribute_lazy_initializer.t
CommitLineData
7a9d14bd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9df136d0 6use Test::More tests => 21;
7a9d14bd 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11}
12
13{
14 package Foo;
15 use Moose;
16
17 has 'foo' => (
18 reader => 'get_foo',
19 writer => 'set_foo',
20 initializer => sub {
29ed9dd9 21 my ($self, $value, $callback, $attr) = @_;
9df136d0 22
23 ::isa_ok($attr, 'Moose::Meta::Attribute');
24 ::is($attr->name, 'foo', '... got the right name');
25
7a9d14bd 26 $callback->($value * 2);
27 },
28 );
29
30 has 'lazy_foo' => (
9df136d0 31 reader => 'get_lazy_foo',
32 lazy => 1,
33 default => 10,
7a9d14bd 34 initializer => sub {
29ed9dd9 35 my ($self, $value, $callback, $attr) = @_;
9df136d0 36
37 ::isa_ok($attr, 'Moose::Meta::Attribute');
38 ::is($attr->name, 'lazy_foo', '... got the right name');
39
7a9d14bd 40 $callback->($value * 2);
41 },
42 );
9df136d0 43
44 has 'lazy_foo_w_type' => (
45 reader => 'get_lazy_foo_w_type',
46 isa => 'Int',
47 lazy => 1,
48 default => 20,
49 initializer => sub {
50 my ($self, $value, $callback, $attr) = @_;
51
52 ::isa_ok($attr, 'Moose::Meta::Attribute');
53 ::is($attr->name, 'lazy_foo_w_type', '... got the right name');
54
55 $callback->($value * 2);
56 },
57 );
58
59 has 'lazy_foo_builder' => (
60 reader => 'get_lazy_foo_builder',
61 builder => 'get_foo_builder',
62 initializer => sub {
63 my ($self, $value, $callback, $attr) = @_;
64
65 ::isa_ok($attr, 'Moose::Meta::Attribute');
66 ::is($attr->name, 'lazy_foo_builder', '... got the right name');
67
68 $callback->($value * 2);
69 },
70 );
71
72 has 'lazy_foo_builder_w_type' => (
73 reader => 'get_lazy_foo_builder_w_type',
74 builder => 'get_foo_builder_w_type',
75 initializer => sub {
76 my ($self, $value, $callback, $attr) = @_;
77
78 ::isa_ok($attr, 'Moose::Meta::Attribute');
79 ::is($attr->name, 'lazy_foo_builder_w_type', '... got the right name');
80
81 $callback->($value * 2);
82 },
83 );
84
85 sub get_foo_builder { 100 }
86 sub get_foo_builder_w_type { 1000 }
7a9d14bd 87}
88
89{
90 my $foo = Foo->new(foo => 10);
91 isa_ok($foo, 'Foo');
92
9df136d0 93 is($foo->get_foo, 20, 'initial value set to 2x given value');
94 is($foo->get_lazy_foo, 20, 'initial lazy value set to 2x given value');
95 is($foo->get_lazy_foo_w_type, 40, 'initial lazy value with type set to 2x given value');
96 is($foo->get_lazy_foo_builder, 200, 'initial lazy value with builder set to 2x given value');
97 is($foo->get_lazy_foo_builder_w_type, 2000, 'initial lazy value with builder and type set to 2x given value');
98}
99
100{
101 package Bar;
102 use Moose;
103
104 has 'foo' => (
105 reader => 'get_foo',
106 writer => 'set_foo',
107 initializer => sub {
108 my ($self, $value, $callback, $attr) = @_;
109
110 ::isa_ok($attr, 'Moose::Meta::Attribute');
111 ::is($attr->name, 'foo', '... got the right name');
112
113 $callback->($value * 2);
114 },
115 );
116
117 make_immutable;
7a9d14bd 118}
119
9df136d0 120{
121 my $bar = Bar->new(foo => 10);
122 isa_ok($bar, 'Bar');
123
124 is($bar->get_foo, 20, 'initial value set to 2x given value');
125}
126
127
128
129
7a9d14bd 130
131