Fix class_type
[gitmo/Mouse.git] / t / 043-parameterized-type.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More tests => 16;
5 use Test::Exception;
6
7 {
8     {
9         package My::Role;
10         use Mouse::Role;
11
12         package My::Class;
13         use Mouse;
14
15         with 'My::Role';
16
17         package Foo;
18         use Mouse;
19
20         has foo => (
21             is  => 'ro',
22             isa => 'HashRef[Int]',
23         );
24
25         has bar => (
26             is  => 'ro',
27             isa => 'ArrayRef[Int]',
28         );
29
30         has complex => (
31             is  => 'rw',
32             isa => 'ArrayRef[HashRef[Int]]'
33         );
34
35         has my_class => (
36             is  => 'rw',
37             isa => 'ArrayRef[My::Class]',
38         );
39
40         has my_role => (
41             is  => 'rw',
42             isa => 'ArrayRef[My::Role]',
43         );
44     };
45
46     ok(Foo->meta->has_attribute('foo'));
47
48     lives_and {
49         my $hash = { a => 1, b => 2, c => 3 };
50         my $array = [ 1, 2, 3 ];
51         my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
52         my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
53
54         is_deeply($foo->foo(), $hash, "foo is a proper hash");
55         is_deeply($foo->bar(), $array, "bar is a proper array");
56         is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
57
58         $foo->my_class([My::Class->new]);
59         is ref($foo->my_class), 'ARRAY';
60         isa_ok $foo->my_class->[0], 'My::Class';
61
62         $foo->my_role([My::Class->new]);
63         is ref($foo->my_role), 'ARRAY';
64
65     } "Parameterized constraints work";
66
67     # check bad args
68     throws_ok {
69         Foo->new( foo => { a => 'b' });
70     } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' failed with value/, "Bad args for hash throws an exception";
71
72     throws_ok {
73         Foo->new( bar => [ a => 'b' ]);
74     } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' failed with value/, "Bad args for array throws an exception";
75
76     throws_ok {
77         Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
78     } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' failed with value/, "Bad args for complex types throws an exception";
79
80     throws_ok {
81         Foo->new( my_class => [ 10 ] );
82     } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/;
83     throws_ok {
84         Foo->new( my_class => [ {foo => 'bar'} ] );
85     } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/;
86
87
88     throws_ok {
89         Foo->new( my_role => [ 20 ] );
90     } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/;
91     throws_ok {
92         Foo->new( my_role => [ {foo => 'bar'} ] );
93     } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/;
94 }
95
96 {
97     {
98         package Bar;
99         use Mouse;
100         use Mouse::Util::TypeConstraints;
101         
102         subtype 'Bar::List'
103             => as 'ArrayRef[HashRef]'
104         ;
105         coerce 'Bar::List'
106             => from 'ArrayRef[Str]'
107             => via {
108                 [ map { +{ $_ => 1 } } @$_ ]
109             }
110         ;
111         has 'list' => (
112             is => 'ro',
113             isa => 'Bar::List',
114             coerce => 1,
115         );
116     }
117
118     lives_and {
119         my @list = ( {a => 1}, {b => 1}, {c => 1} );
120         my $bar = Bar->new(list => [ qw(a b c) ]);
121
122         is_deeply( $bar->list, \@list, "list is as expected");
123     } "coercion works";
124
125     throws_ok {
126         Bar->new(list => [ { 1 => 2 }, 2, 3 ]);
127     } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error";
128 }
129
130
131