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');
42 ok( (my $instance = MyClass->new), 'instance' );
45 local $TODO = (Moose->VERSION < 1.9900 ? 'waiting on Moose changes for role support' : undef);
48 $instance->foo('bar');
49 }, undef, 'attribute coercion ran' );
50 is($instance->foo, 3);
54 $instance->bar('baz');
56 }, undef, 'class attribute coercion ran' );
58 isnt( exception { $instance->baz('quux') }, undef, 'class attribute coercion did not run with coerce => 0' );
60 isnt( exception { $instance->quux('mtfnpy') }, undef, 'attribute coercion did not run with coerce => 0' );
63 $instance->uncoerced_attr(10);
64 is $instance->uncoerced_attr(10), 10;
65 }, undef, 'set attribute having type with no coercion and no coerce=0' );
68 $instance->uncoerced_class_attr(10);
69 is $instance->uncoerced_class_attr(10), 10;
70 }, undef, 'set class attribute having type with no coercion and no coerce=0' );
73 $instance->untyped_attr(10);
74 is $instance->untyped_attr, 10;
75 }, undef, 'set untyped attribute' );
78 $instance->untyped_class_attr(10);
79 is $instance->untyped_class_attr, 10;
80 }, undef, 'set untyped class attribute' );