5 use Test::More tests => 16;
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 has untyped_attr => (is => 'rw');
34 class_has untyped_class_attr => (is => 'rw');
37 ok( (my $instance = MyClass->new), 'instance' );
41 $instance->foo('bar');
45 'attribute coercion ran',
50 $instance->bar('baz');
54 'class attribute coercion ran',
58 exception { $instance->baz('quux') },
60 'class attribute coercion did not run with coerce => 0',
64 exception{ $instance->quux('mtfnpy') },
66 'attribute coercion did not run with coerce => 0',
71 $instance->uncoerced_attr(10);
72 is $instance->uncoerced_attr(10), 10;
75 'set attribute having type with no coercion and no coerce=0',
80 $instance->uncoerced_class_attr(10);
81 is $instance->uncoerced_class_attr(10), 10;
84 'set class attribute having type with no coercion and no coerce=0',
89 $instance->untyped_attr(10);
90 is $instance->untyped_attr, 10;
93 'set untyped attribute',
98 $instance->untyped_class_attr(10);
99 is $instance->untyped_class_attr, 10;
102 'set untyped class attribute',