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