update repo to point to github
[gitmo/Moo.git] / t / accessor-coerce.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4
5 sub run_for {
6   my $class = shift;
7
8   my $obj = $class->new(plus_three => 1);
9
10   is($obj->plus_three, 4, "initial value set (${class})");
11
12   $obj->plus_three(4);
13
14   is($obj->plus_three, 7, 'Value changes after set');
15 }
16
17 sub run_with_default_for {
18   my $class = shift;
19
20   my $obj = $class->new();
21
22   is($obj->plus_three, 4, "initial value set (${class})");
23
24   $obj->plus_three(4);
25
26   is($obj->plus_three, 7, 'Value changes after set');
27 }
28
29
30
31 {
32   package Foo;
33
34   use Moo;
35
36   has plus_three => (
37     is => 'rw',
38     coerce => sub { $_[0] + 3 }
39   );
40 }
41
42 run_for 'Foo';
43
44 {
45   package Bar;
46
47   use Sub::Quote;
48   use Moo;
49
50   has plus_three => (
51     is => 'rw',
52     coerce => quote_sub q{
53       my ($x) = @_;
54       $x + 3
55     }
56   );
57 }
58
59 run_for 'Bar';
60
61 {
62   package Baz;
63
64   use Sub::Quote;
65   use Moo;
66
67   has plus_three => (
68     is => 'rw',
69     coerce => quote_sub(
70       q{
71         my ($value) = @_;
72         $value + $plus
73       },
74       { '$plus' => \3 }
75     )
76   );
77 }
78
79 run_for 'Baz';
80
81 {
82   package Biff;
83
84   use Sub::Quote;
85   use Moo;
86
87   has plus_three => (
88     is => 'rw',
89     coerce => quote_sub(
90       q{
91         die 'could not add three!'
92       },
93     )
94   );
95 }
96
97 like exception { Biff->new(plus_three => 1) }, qr/coercion for "plus_three" failed: could not add three!/, 'Exception properly thrown';
98
99 {
100   package Foo2;
101
102   use Moo;
103
104   has plus_three => (
105     is => 'rw',
106     default => sub { 1 },
107     coerce => sub { $_[0] + 3 }
108   );
109 }
110
111 run_with_default_for 'Foo2';
112
113 {
114   package Bar2;
115
116   use Sub::Quote;
117   use Moo;
118
119   has plus_three => (
120     is => 'rw',
121     default => sub { 1 },
122     coerce => quote_sub q{
123       my ($x) = @_;
124       $x + 3
125     }
126   );
127 }
128
129 run_with_default_for 'Bar2';
130
131 {
132   package Baz2;
133
134   use Sub::Quote;
135   use Moo;
136
137   has plus_three => (
138     is => 'rw',
139     default => sub { 1 },
140     coerce => quote_sub(
141       q{
142         my ($value) = @_;
143         $value + $plus
144       },
145       { '$plus' => \3 }
146     )
147   );
148 }
149
150 run_with_default_for 'Baz2';
151
152 {
153   package Biff2;
154
155   use Sub::Quote;
156   use Moo;
157
158   has plus_three => (
159     is => 'rw',
160     default => sub { 1 },
161     coerce => quote_sub(
162       q{
163         die 'could not add three!'
164       },
165     )
166   );
167 }
168
169 like exception { Biff2->new() }, qr/could not add three!/, 'Exception properly thrown';
170
171 {
172   package Foo3;
173
174   use Moo;
175
176   has plus_three => (
177     is => 'rw',
178     default => sub { 1 },
179     coerce => sub { $_[0] + 3 },
180     lazy => 1,
181   );
182 }
183
184 run_with_default_for 'Foo3';
185
186 {
187   package Bar3;
188
189   use Sub::Quote;
190   use Moo;
191
192   has plus_three => (
193     is => 'rw',
194     default => sub { 1 },
195     coerce => quote_sub(q{
196       my ($x) = @_;
197       $x + 3
198     }),
199     lazy => 1,
200   );
201 }
202
203 run_with_default_for 'Bar3';
204
205 done_testing;