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