initial test for coerce
[gitmo/Moo.git] / t / accessor-coerce.t
CommitLineData
1c9533c7 1use strictures 1;
2use Test::More;
3use Test::Fatal;
4
5sub 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{
18 package Foo;
19
20 use Moo;
21
22 has plus_three => (
23 is => 'rw',
24 coerce => sub { $_[0] + 3 }
25 );
26}
27
28run_for 'Foo';
29
30{
31 package Bar;
32
33 use Sub::Quote;
34 use Moo;
35
36 has plus_three => (
37 is => 'rw',
38 coerce => quote_sub q{
39 my ($x) = @_;
40 $x + 3
41 }
42 );
43}
44
45run_for 'Bar';
46
47{
48 package Baz;
49
50 use Sub::Quote;
51 use Moo;
52
53 has plus_three => (
54 is => 'rw',
55 isa => quote_sub(
56 q{
57 my ($value) = @_;
58 $value + $plus
59 },
60 { '$plus' => \3 }
61 )
62 );
63}
64
65run_for 'Baz';
66
67{
68 package Biff;
69
70 use Sub::Quote;
71 use Moo;
72
73 has dies => (
74 is => 'rw',
75 isa => quote_sub(
76 q{
77 die 'could not add three!'
78 },
79 )
80 );
81}
82
83like exception { Biff->new(plus_three => 1) }, 'could not add three!', 'Exception properly thrown';
84
85done_testing;