initial test for coerce
[gitmo/Role-Tiny.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 {
18   package Foo;
19
20   use Moo;
21
22   has plus_three => (
23     is => 'rw',
24     coerce => sub { $_[0] + 3 }
25   );
26 }
27
28 run_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
45 run_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
65 run_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
83 like exception { Biff->new(plus_three => 1) }, 'could not add three!', 'Exception properly thrown';
84
85 done_testing;