Commit | Line | Data |
ad1917d7 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | |
f327aa7a |
5 | use Test::More tests => 5; |
ad1917d7 |
6 | |
7 | { |
8 | package MyClass; |
9 | use Moose; |
ad1917d7 |
10 | use MooseX::AlwaysCoerce; |
11 | use Moose::Util::TypeConstraints; |
12 | |
13 | subtype 'MyType', as 'Int'; |
14 | coerce 'MyType', from 'Str', via { length $_ }; |
15 | |
16 | has foo => (is => 'rw', isa => 'MyType'); |
17 | |
18 | class_has bar => (is => 'rw', isa => 'MyType'); |
44b44091 |
19 | |
20 | class_has baz => (is => 'rw', isa => 'MyType', coerce => 0); |
f327aa7a |
21 | |
22 | has quux => (is => 'rw', isa => 'MyType', coerce => 0); |
ad1917d7 |
23 | } |
24 | |
25 | ok( (my $instance = MyClass->new), 'instance' ); |
26 | |
27 | eval { $instance->foo('bar') }; |
28 | ok( (!$@), 'attribute coercion ran' ); |
29 | |
30 | eval { $instance->bar('baz') }; |
31 | ok( (!$@), 'class attribute coercion ran' ); |
44b44091 |
32 | |
33 | eval { $instance->baz('quux') }; |
34 | ok( $@, 'class attribute coercion did not run with coerce => 0' ); |
f327aa7a |
35 | |
36 | undef $@; |
37 | |
38 | eval { $instance->quux('mtfnpy') }; |
39 | ok( $@, 'attribute coercion did not run with coerce => 0' ); |