4 use Test::More tests => 46;
22 isa => 'HashRef[Int]',
27 isa => 'ArrayRef[Int]',
32 isa => 'ArrayRef[HashRef[Int]]'
37 isa => 'ArrayRef[My::Class]',
42 isa => 'ArrayRef[My::Role]',
46 ok(Foo->meta->has_attribute('foo'));
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);
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");
58 $foo->my_class([My::Class->new]);
59 is ref($foo->my_class), 'ARRAY';
60 isa_ok $foo->my_class->[0], 'My::Class';
62 $foo->my_role([My::Class->new]);
63 is ref($foo->my_role), 'ARRAY';
65 } "Parameterized constraints work";
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";
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";
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";
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/;
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/;
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/;
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/;
100 use Mouse::Util::TypeConstraints;
103 => as 'ArrayRef[HashRef]'
106 => from 'ArrayRef[Str]'
108 [ map { +{ $_ => 1 } } @$_ ]
119 my @list = ( {a => 1}, {b => 1}, {c => 1} );
120 my $bar = Bar->new(list => [ qw(a b c) ]);
122 is_deeply( $bar->list, \@list, "list is as expected");
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";
130 use Mouse::Util::TypeConstraints;
132 my $t = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
133 ok $t->is_a_type_of($t), "$t is a type of $t";
134 ok $t->is_a_type_of('Maybe'), "$t is a type of Maybe";
136 # XXX: how about 'MaybeInt[ Int ]'?
137 ok $t->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
139 ok!$t->is_a_type_of('Int');
145 my $u = subtype 'MaybeInt', as 'Maybe[Int]';
146 ok $u->is_a_type_of($t), "$t is a type of $t";
147 ok $u->is_a_type_of('Maybe'), "$t is a type of Maybe";
149 # XXX: how about 'MaybeInt[ Int ]'?
150 ok $u->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
152 ok!$u->is_a_type_of('Int');
158 # XXX: undefined hehaviour
159 # ok $t->is_a_type_of($u);
160 # ok $u->is_a_type_of($t);
162 my $w = subtype as 'Maybe[ ArrayRef | HashRef ]';
169 ok $w->is_a_type_of('Maybe');
170 ok $w->is_a_type_of('Maybe[ArrayRef|HashRef]');
171 ok!$w->is_a_type_of('ArrayRef');
173 my $x = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[ ArrayRef[ Int | Undef ] ]');
175 ok $x->is_a_type_of('ArrayRef');
176 ok $x->is_a_type_of('ArrayRef[ArrayRef[Int|Undef]]');
177 ok!$x->is_a_type_of('ArrayRef[ArrayRef[Str]]');
181 ok $x->check([[10]]);
182 ok $x->check([[10, undef]]);
183 ok!$x->check([[10, 3.14]]);