31e4c8cfac5575cd7c1f80909c6d23b6c6c5fe15
[gitmo/MooseX-Types.git] / t / 21_coerce_parameterized_types.t
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;
10     use MooseX::Types -declare => [qw/
11         MyChar MyDigit ArrayRefOfMyCharOrDigit
12     /];
13     use MooseX::Types::Moose qw/ArrayRef Str Int/;
14
15     subtype MyChar, as Str, where {
16         length == 1
17     };
18
19     subtype MyDigit, as Int, where {
20         length == 1
21     };
22
23     coerce ArrayRef[MyChar|MyDigit], from Str, via {
24         [split //]
25     };
26
27 # same thing with an explicit subtype
28     subtype ArrayRefOfMyCharOrDigit, as ArrayRef[MyChar|MyDigit];
29
30     coerce ArrayRefOfMyCharOrDigit, from Str, via {
31         [split //]
32     };
33 }
34 {
35     package AClass;
36     use Moose;
37     BEGIN { TypeLib->import(qw/
38         MyChar MyDigit ArrayRefOfMyCharOrDigit/
39     ) };
40     use MooseX::Types::Moose 'ArrayRef';
41
42     has parameterized => (is => 'rw', isa => ArrayRef[MyChar|MyDigit], coerce => 1);
43     has subtype_parameterized => (is => 'rw', isa => ArrayRefOfMyCharOrDigit, coerce => 1);
44 }
45
46 my $instance = AClass->new;
47
48 lives_ok { $instance->parameterized('foo') }
49     'coercion applied to parameterized type';
50
51 lives_ok { $instance->subtype_parameterized('foo') }
52     'coercion applied to subtype';