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