Commit | Line | Data |
ad1917d7 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | |
6b46d35c |
5 | use Test::More tests => 7; |
df491f72 |
6 | use Test::Exception; |
ad1917d7 |
7 | |
8 | { |
9 | package MyClass; |
10 | use Moose; |
ad1917d7 |
11 | use MooseX::AlwaysCoerce; |
12 | use Moose::Util::TypeConstraints; |
13 | |
14 | subtype 'MyType', as 'Int'; |
15 | coerce 'MyType', from 'Str', via { length $_ }; |
16 | |
6b46d35c |
17 | subtype 'Uncoerced', as 'Int'; |
18 | |
ad1917d7 |
19 | has foo => (is => 'rw', isa => 'MyType'); |
20 | |
21 | class_has bar => (is => 'rw', isa => 'MyType'); |
44b44091 |
22 | |
23 | class_has baz => (is => 'rw', isa => 'MyType', coerce => 0); |
f327aa7a |
24 | |
25 | has quux => (is => 'rw', isa => 'MyType', coerce => 0); |
6b46d35c |
26 | |
27 | has uncoerced_attr => (is => 'rw', isa => 'Uncoerced'); |
28 | |
29 | class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced'); |
ad1917d7 |
30 | } |
31 | |
32 | ok( (my $instance = MyClass->new), 'instance' ); |
33 | |
df491f72 |
34 | lives_ok { $instance->foo('bar') } 'attribute coercion ran'; |
ad1917d7 |
35 | |
df491f72 |
36 | lives_ok { $instance->bar('baz') } 'class attribute coercion ran'; |
44b44091 |
37 | |
df491f72 |
38 | dies_ok { $instance->baz('quux') } |
39 | 'class attribute coercion did not run with coerce => 0'; |
f327aa7a |
40 | |
df491f72 |
41 | dies_ok { $instance->quux('mtfnpy') } |
42 | 'attribute coercion did not run with coerce => 0'; |
f327aa7a |
43 | |
df491f72 |
44 | lives_ok { $instance->uncoerced_attr(10) } |
45 | 'set attribute having type with no coercion and no coerce=0'; |
6b46d35c |
46 | |
df491f72 |
47 | lives_ok { $instance->uncoerced_class_attr(10) } |
48 | 'set class attribute having type with no coercion and no coerce=0'; |