8 if (eval { require MooseX::Role::Parameterized }) {
11 plan skip_all => 'This test needs MooseX::Role::Parameterized';
17 use MooseX::Role::Parameterized;
18 use MooseX::AlwaysCoerce;
19 use Moose::Util::TypeConstraints;
23 use Moose::Util::TypeConstraints;
25 subtype 'MyType', as 'Int';
26 coerce 'MyType', from 'Str', via { length $_ };
28 subtype 'Uncoerced', as 'Int';
30 has foo => (is => 'rw', isa => 'MyType');
32 class_has bar => (is => 'rw', isa => 'MyType');
34 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
36 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
38 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
40 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
53 ok( (my $instance = MyClass->new), 'instance' );
55 lives_ok { $instance->foo('bar') } 'attribute coercion ran';
57 lives_ok { $instance->bar('baz') } 'class attribute coercion ran';
59 dies_ok { $instance->baz('quux') }
60 'class attribute coercion did not run with coerce => 0';
62 dies_ok { $instance->quux('mtfnpy') }
63 'attribute coercion did not run with coerce => 0';
65 lives_ok { $instance->uncoerced_attr(10) }
66 'set attribute having type with no coercion and no coerce=0';
68 lives_ok { $instance->uncoerced_class_attr(10) }
69 'set class attribute having type with no coercion and no coerce=0';