5 use Test::More tests => 8;
12 use MooseX::AlwaysCoerce;
13 use Moose::Util::TypeConstraints;
15 subtype 'MyType', as 'Int';
16 coerce 'MyType', from 'Str', via { length $_ };
18 subtype 'Uncoerced', as 'Int';
20 has foo => (is => 'rw', isa => 'MyType');
22 class_has bar => (is => 'rw', isa => 'MyType');
24 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
26 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
28 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
30 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
32 class_has untyped_class_attr => (is => 'rw');
35 ok( (my $instance = MyClass->new), 'instance' );
37 lives_ok { $instance->foo('bar') } 'attribute coercion ran';
39 lives_ok { $instance->bar('baz') } 'class attribute coercion ran';
41 dies_ok { $instance->baz('quux') }
42 'class attribute coercion did not run with coerce => 0';
44 dies_ok { $instance->quux('mtfnpy') }
45 'attribute coercion did not run with coerce => 0';
47 lives_ok { $instance->uncoerced_attr(10) }
48 'set attribute having type with no coercion and no coerce=0';
50 lives_ok { $instance->uncoerced_class_attr(10) }
51 'set class attribute having type with no coercion and no coerce=0';