6 use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
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');
31 has untyped_attr => (is => 'rw');
33 class_has untyped_class_attr => (is => 'rw');
36 ok( (my $instance = MyClass->new), 'instance' );
40 $instance->foo('bar');
44 'attribute coercion ran',
49 $instance->bar('baz');
53 'class attribute coercion ran',
57 exception { $instance->baz('quux') },
59 'class attribute coercion did not run with coerce => 0',
63 exception{ $instance->quux('mtfnpy') },
65 'attribute coercion did not run with coerce => 0',
70 $instance->uncoerced_attr(10);
71 is $instance->uncoerced_attr(10), 10;
74 'set attribute having type with no coercion and no coerce=0',
79 $instance->uncoerced_class_attr(10);
80 is $instance->uncoerced_class_attr(10), 10;
83 'set class attribute having type with no coercion and no coerce=0',
88 $instance->untyped_attr(10);
89 is $instance->untyped_attr, 10;
92 'set untyped attribute',
97 $instance->untyped_class_attr(10);
98 is $instance->untyped_class_attr, 10;
101 'set untyped class attribute',