Commit | Line | Data |
ad1917d7 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | |
bb7cca58 |
5 | use Test::More tests => 10; |
df491f72 |
6 | use Test::Exception; |
0d42c8e8 |
7 | use Test::NoWarnings; |
ad1917d7 |
8 | |
9 | { |
10 | package MyClass; |
11 | use Moose; |
ad1917d7 |
12 | use MooseX::AlwaysCoerce; |
13 | use Moose::Util::TypeConstraints; |
14 | |
15 | subtype 'MyType', as 'Int'; |
16 | coerce 'MyType', from 'Str', via { length $_ }; |
17 | |
6b46d35c |
18 | subtype 'Uncoerced', as 'Int'; |
19 | |
ad1917d7 |
20 | has foo => (is => 'rw', isa => 'MyType'); |
21 | |
22 | class_has bar => (is => 'rw', isa => 'MyType'); |
44b44091 |
23 | |
24 | class_has baz => (is => 'rw', isa => 'MyType', coerce => 0); |
f327aa7a |
25 | |
26 | has quux => (is => 'rw', isa => 'MyType', coerce => 0); |
6b46d35c |
27 | |
28 | has uncoerced_attr => (is => 'rw', isa => 'Uncoerced'); |
29 | |
30 | class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced'); |
57d1fb14 |
31 | |
bb7cca58 |
32 | has untyped_attr => (is => 'rw'); |
33 | |
57d1fb14 |
34 | class_has untyped_class_attr => (is => 'rw'); |
ad1917d7 |
35 | } |
36 | |
37 | ok( (my $instance = MyClass->new), 'instance' ); |
38 | |
bb7cca58 |
39 | lives_and { |
40 | $instance->foo('bar'); |
41 | is $instance->foo, 3; |
42 | } 'attribute coercion ran'; |
ad1917d7 |
43 | |
bb7cca58 |
44 | lives_and { |
45 | $instance->bar('baz'); |
46 | is $instance->bar, 3; |
47 | } 'class attribute coercion ran'; |
44b44091 |
48 | |
df491f72 |
49 | dies_ok { $instance->baz('quux') } |
50 | 'class attribute coercion did not run with coerce => 0'; |
f327aa7a |
51 | |
df491f72 |
52 | dies_ok { $instance->quux('mtfnpy') } |
53 | 'attribute coercion did not run with coerce => 0'; |
f327aa7a |
54 | |
bb7cca58 |
55 | lives_and { |
56 | $instance->uncoerced_attr(10); |
57 | is $instance->uncoerced_attr(10), 10; |
58 | } 'set attribute having type with no coercion and no coerce=0'; |
59 | |
60 | lives_and { |
61 | $instance->uncoerced_class_attr(10); |
62 | is $instance->uncoerced_class_attr(10), 10; |
63 | } 'set class attribute having type with no coercion and no coerce=0'; |
64 | |
65 | lives_and { |
66 | $instance->untyped_attr(10); |
67 | is $instance->untyped_attr, 10; |
68 | } 'set untyped attribute'; |
69 | |
70 | lives_and { |
71 | $instance->untyped_class_attr(10); |
72 | is $instance->untyped_class_attr, 10; |
73 | } 'set untyped class attribute'; |