5 use Test::More tests => 4;
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);
23 ok( (my $instance = MyClass->new), 'instance' );
25 eval { $instance->foo('bar') };
26 ok( (!$@), 'attribute coercion ran' );
28 eval { $instance->bar('baz') };
29 ok( (!$@), 'class attribute coercion ran' );
31 eval { $instance->baz('quux') };
32 ok( $@, 'class attribute coercion did not run with coerce => 0' );