5 use Test::More tests => 7;
10 use MooseX::AlwaysCoerce;
11 use Moose::Util::TypeConstraints;
13 subtype 'MyType', as 'Int';
14 coerce 'MyType', from 'Str', via { length $_ };
16 subtype 'Uncoerced', as 'Int';
18 has foo => (is => 'rw', isa => 'MyType');
20 class_has bar => (is => 'rw', isa => 'MyType');
22 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
24 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
26 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
28 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
31 ok( (my $instance = MyClass->new), 'instance' );
33 eval { $instance->foo('bar') };
34 ok( (!$@), 'attribute coercion ran' );
36 eval { $instance->bar('baz') };
37 ok( (!$@), 'class attribute coercion ran' );
39 eval { $instance->baz('quux') };
40 ok( $@, 'class attribute coercion did not run with coerce => 0' );
44 eval { $instance->quux('mtfnpy') };
45 ok( $@, 'attribute coercion did not run with coerce => 0' );
47 eval { $instance->uncoerced_attr(10) };
48 is $@, "", 'set attribute having type with no coercion and no coerce=0';
50 eval { $instance->uncoerced_class_attr(10) };
51 is $@, "", 'set class attribute having type with no coercion and no coerce=0';