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