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