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