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