Commit | Line | Data |
6cfbfdbc |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::Exception; |
5 | |
6 | use Test::More tests => 2; |
7 | |
8 | BEGIN { |
9 | package TypeLib; |
86a2a6b8 |
10 | use MooseX::Types -declare => [qw/ |
11 | MyChar MyDigit ArrayRefOfMyCharOrDigit |
12 | /]; |
d46ddd3c |
13 | use MooseX::Types::Moose qw/ArrayRef Str Int/; |
6cfbfdbc |
14 | |
d46ddd3c |
15 | subtype MyChar, as Str, where { |
6cfbfdbc |
16 | length == 1 |
17 | }; |
18 | |
d46ddd3c |
19 | subtype MyDigit, as Int, where { |
20 | length == 1 |
21 | }; |
22 | |
23 | coerce ArrayRef[MyChar|MyDigit], from Str, via { |
6cfbfdbc |
24 | [split //] |
25 | }; |
26 | |
27 | # same thing with an explicit subtype |
d46ddd3c |
28 | subtype ArrayRefOfMyCharOrDigit, as ArrayRef[MyChar|MyDigit]; |
6cfbfdbc |
29 | |
d46ddd3c |
30 | coerce ArrayRefOfMyCharOrDigit, from Str, via { |
6cfbfdbc |
31 | [split //] |
32 | }; |
33 | } |
34 | { |
35 | package AClass; |
36 | use Moose; |
86a2a6b8 |
37 | BEGIN { TypeLib->import(qw/ |
38 | MyChar MyDigit ArrayRefOfMyCharOrDigit/ |
39 | ) }; |
6cfbfdbc |
40 | use MooseX::Types::Moose 'ArrayRef'; |
41 | |
d46ddd3c |
42 | has parameterized => (is => 'rw', isa => ArrayRef[MyChar|MyDigit], coerce => 1); |
86a2a6b8 |
43 | has subtype_parameterized => (is => 'rw', isa => ArrayRefOfMyCharOrDigit, coerce => 1); |
6cfbfdbc |
44 | } |
45 | |
46 | my $instance = AClass->new; |
47 | |
64f42303 |
48 | { local $TODO = "see comments in MooseX::Types->create_arged_..."; |
6cfbfdbc |
49 | lives_ok { $instance->parameterized('foo') } |
50 | 'coercion applied to parameterized type'; |
64f42303 |
51 | } |
6cfbfdbc |
52 | |
86a2a6b8 |
53 | lives_ok { $instance->subtype_parameterized('foo') } |
54 | 'coercion applied to subtype'; |