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