do not let warnings kill an install
[gitmo/MooseX-AlwaysCoerce.git] / t / 04-parameterized-roles.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Fatal;
5
6 use Test::Requires {
7     'MooseX::Role::Parameterized' => 0.25,
8 };
9
10 plan tests => 12;
11 use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
12
13 eval <<'EOF';
14     package Role;
15     use MooseX::Role::Parameterized 0.25;
16     use MooseX::ClassAttribute 0.24;
17     use MooseX::AlwaysCoerce;
18     use Moose::Util::TypeConstraints;
19
20     role {
21         subtype 'MyType', as 'Int';
22         coerce 'MyType', from 'Str', via { length $_ };
23
24         subtype 'Uncoerced', as 'Int';
25
26         has foo => (is => 'rw', isa => 'MyType');
27
28         class_has bar => (is => 'rw', isa => 'MyType');
29
30         class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
31
32         has quux => (is => 'rw', isa => 'MyType', coerce => 0);
33
34         has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
35
36         class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
37
38         has untyped_attr => (is => 'rw');
39
40         class_has untyped_class_attr => (is => 'rw');
41     };
42
43     package MyClass;
44     use Moose;
45     with 'Role';
46 EOF
47
48
49
50 ok( (my $instance = MyClass->new), 'instance' );
51
52 {
53     local $TODO = 'waiting on Moose changes for role support, and ClassAttribute changes for paramterized role support';
54
55     is( exception {
56         $instance->foo('bar');
57         is $instance->foo, 3;
58     }, undef, 'attribute coercion ran' );
59
60     is( exception {
61         $instance->bar('baz');
62         is $instance->bar, 3;
63     }, undef, 'class attribute coercion ran' );
64
65     isnt( exception { $instance->baz('quux') }, undef, 'class attribute coercion did not run with coerce => 0' );
66
67     isnt( exception { $instance->quux('mtfnpy') }, undef, 'attribute coercion did not run with coerce => 0' );
68
69     is( exception {
70         $instance->uncoerced_attr(10);
71         is $instance->uncoerced_attr(10), 10;
72     }, undef, 'set attribute having type with no coercion and no coerce=0' );
73
74     is( exception {
75         $instance->uncoerced_class_attr(10);
76         is $instance->uncoerced_class_attr(10), 10;
77     }, undef, 'set class attribute having type with no coercion and no coerce=0' );
78
79     is( exception {
80         $instance->untyped_attr(10);
81         is $instance->untyped_attr, 10;
82     }, undef, 'set untyped attribute' );
83
84     is( exception {
85         $instance->untyped_class_attr(10);
86         is $instance->untyped_class_attr, 10;
87     }, undef, 'set untyped class attribute' );
88 }