Commit | Line | Data |
a193e05d |
1 | use strict; |
2 | use warnings; |
a193e05d |
3 | use Test::More; |
dfab7c8b |
4 | use Test::Fatal; |
a193e05d |
5 | |
73760f48 |
6 | use Test::Requires { |
3d261da4 |
7 | 'MooseX::Role::Parameterized' => 0.25, |
73760f48 |
8 | }; |
a193e05d |
9 | |
9381ff3c |
10 | plan tests => 12; |
fc553705 |
11 | use if $ENV{AUTHOR_TESTING}, 'Test::Warnings'; |
9381ff3c |
12 | |
13005566 |
13 | eval <<'EOF'; |
a193e05d |
14 | package Role; |
3d261da4 |
15 | use MooseX::Role::Parameterized 0.25; |
16 | use MooseX::ClassAttribute 0.24; |
a193e05d |
17 | use MooseX::AlwaysCoerce; |
18 | use Moose::Util::TypeConstraints; |
19 | |
f2dec73d |
20 | role { |
21 | subtype 'MyType', as 'Int'; |
22 | coerce 'MyType', from 'Str', via { length $_ }; |
a193e05d |
23 | |
f2dec73d |
24 | subtype 'Uncoerced', as 'Int'; |
a193e05d |
25 | |
f2dec73d |
26 | has foo => (is => 'rw', isa => 'MyType'); |
a193e05d |
27 | |
f2dec73d |
28 | class_has bar => (is => 'rw', isa => 'MyType'); |
a193e05d |
29 | |
f2dec73d |
30 | class_has baz => (is => 'rw', isa => 'MyType', coerce => 0); |
a193e05d |
31 | |
f2dec73d |
32 | has quux => (is => 'rw', isa => 'MyType', coerce => 0); |
a193e05d |
33 | |
f2dec73d |
34 | has uncoerced_attr => (is => 'rw', isa => 'Uncoerced'); |
a193e05d |
35 | |
f2dec73d |
36 | class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced'); |
a193e05d |
37 | |
f2dec73d |
38 | has untyped_attr => (is => 'rw'); |
a193e05d |
39 | |
f2dec73d |
40 | class_has untyped_class_attr => (is => 'rw'); |
41 | }; |
bb7cca58 |
42 | |
f2dec73d |
43 | package MyClass; |
a193e05d |
44 | use Moose; |
45 | with 'Role'; |
13005566 |
46 | EOF |
47 | |
a193e05d |
48 | |
a193e05d |
49 | |
50 | ok( (my $instance = MyClass->new), 'instance' ); |
51 | |
0a64dac6 |
52 | { |
53 | local $TODO = 'waiting on Moose changes for role support, and ClassAttribute changes for paramterized role support'; |
54 | |
dfab7c8b |
55 | is( exception { |
bb7cca58 |
56 | $instance->foo('bar'); |
57 | is $instance->foo, 3; |
dfab7c8b |
58 | }, undef, 'attribute coercion ran' ); |
a193e05d |
59 | |
dfab7c8b |
60 | is( exception { |
f2dec73d |
61 | $instance->bar('baz'); |
62 | is $instance->bar, 3; |
dfab7c8b |
63 | }, undef, 'class attribute coercion ran' ); |
a193e05d |
64 | |
dfab7c8b |
65 | isnt( exception { $instance->baz('quux') }, undef, 'class attribute coercion did not run with coerce => 0' ); |
a193e05d |
66 | |
dfab7c8b |
67 | isnt( exception { $instance->quux('mtfnpy') }, undef, 'attribute coercion did not run with coerce => 0' ); |
a193e05d |
68 | |
dfab7c8b |
69 | is( exception { |
f2dec73d |
70 | $instance->uncoerced_attr(10); |
71 | is $instance->uncoerced_attr(10), 10; |
dfab7c8b |
72 | }, undef, 'set attribute having type with no coercion and no coerce=0' ); |
bb7cca58 |
73 | |
dfab7c8b |
74 | is( exception { |
f2dec73d |
75 | $instance->uncoerced_class_attr(10); |
76 | is $instance->uncoerced_class_attr(10), 10; |
dfab7c8b |
77 | }, undef, 'set class attribute having type with no coercion and no coerce=0' ); |
bb7cca58 |
78 | |
dfab7c8b |
79 | is( exception { |
f2dec73d |
80 | $instance->untyped_attr(10); |
81 | is $instance->untyped_attr, 10; |
dfab7c8b |
82 | }, undef, 'set untyped attribute' ); |
bb7cca58 |
83 | |
dfab7c8b |
84 | is( exception { |
f2dec73d |
85 | $instance->untyped_class_attr(10); |
86 | is $instance->untyped_class_attr, 10; |
dfab7c8b |
87 | }, undef, 'set untyped class attribute' ); |
f2dec73d |
88 | } |