5 use Test::More tests => 5;
10 use MooseX::AlwaysCoerce;
11 use Moose::Util::TypeConstraints;
13 subtype 'MyType', as 'Int';
14 coerce 'MyType', from 'Str', via { length $_ };
16 has foo => (is => 'rw', isa => 'MyType');
18 class_has bar => (is => 'rw', isa => 'MyType');
20 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
22 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
25 ok( (my $instance = MyClass->new), 'instance' );
27 eval { $instance->foo('bar') };
28 ok( (!$@), 'attribute coercion ran' );
30 eval { $instance->bar('baz') };
31 ok( (!$@), 'class attribute coercion ran' );
33 eval { $instance->baz('quux') };
34 ok( $@, 'class attribute coercion did not run with coerce => 0' );
38 eval { $instance->quux('mtfnpy') };
39 ok( $@, 'attribute coercion did not run with coerce => 0' );