Commit | Line | Data |
0d42c8e8 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Test::More tests => 8; |
6 | use Test::Exception; |
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 | |
33 | { |
34 | package MyClass; |
35 | use Moose; |
36 | with 'MyRole'; |
37 | } |
38 | |
39 | ok( (my $instance = MyClass->new), 'instance' ); |
40 | |
41 | lives_ok { $instance->foo('bar') } 'attribute coercion ran'; |
42 | |
43 | lives_ok { $instance->bar('baz') } 'class attribute coercion ran'; |
44 | |
45 | dies_ok { $instance->baz('quux') } |
46 | 'class attribute coercion did not run with coerce => 0'; |
47 | |
48 | dies_ok { $instance->quux('mtfnpy') } |
49 | 'attribute coercion did not run with coerce => 0'; |
50 | |
51 | lives_ok { $instance->uncoerced_attr(10) } |
52 | 'set attribute having type with no coercion and no coerce=0'; |
53 | |
54 | lives_ok { $instance->uncoerced_class_attr(10) } |
55 | 'set class attribute having type with no coercion and no coerce=0'; |
56 | |