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