5 use Test::More tests => 7;
11 use MooseX::AlwaysCoerce;
12 use Moose::Util::TypeConstraints;
14 subtype 'MyType', as 'Int';
15 coerce 'MyType', from 'Str', via { length $_ };
17 subtype 'Uncoerced', as 'Int';
19 has foo => (is => 'rw', isa => 'MyType');
21 class_has bar => (is => 'rw', isa => 'MyType');
23 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
25 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
27 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
29 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
32 ok( (my $instance = MyClass->new), 'instance' );
34 lives_ok { $instance->foo('bar') } 'attribute coercion ran';
36 lives_ok { $instance->bar('baz') } 'class attribute coercion ran';
38 dies_ok { $instance->baz('quux') }
39 'class attribute coercion did not run with coerce => 0';
41 dies_ok { $instance->quux('mtfnpy') }
42 'attribute coercion did not run with coerce => 0';
44 lives_ok { $instance->uncoerced_attr(10) }
45 'set attribute having type with no coercion and no coerce=0';
47 lives_ok { $instance->uncoerced_class_attr(10) }
48 'set class attribute having type with no coercion and no coerce=0';