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