25 isa => 'HashRef[Int]',
30 isa => 'ArrayRef[Int]',
35 isa => 'ArrayRef[HashRef[Int]]'
40 isa => 'ArrayRef[My::Class]',
45 isa => 'ArrayRef[My::Role]',
49 ok(Foo->meta->has_attribute('foo'));
52 my $hash = { a => 1, b => 2, c => 3 };
53 my $array = [ 1, 2, 3 ];
54 my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
55 my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
57 is_deeply($foo->foo(), $hash, "foo is a proper hash");
58 is_deeply($foo->bar(), $array, "bar is a proper array");
59 is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
61 $foo->my_class([My::Class->new]);
62 is ref($foo->my_class), 'ARRAY';
63 isa_ok $foo->my_class->[0], 'My::Class';
65 $foo->my_role([My::Class->new]);
66 is ref($foo->my_role), 'ARRAY';
68 } "Parameterized constraints work";
72 Foo->new( foo => { a => 'b' });
73 } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' with value/, "Bad args for hash throws an exception";
76 Foo->new( bar => [ a => 'b' ]);
77 } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' with value/, "Bad args for array throws an exception";
80 Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
81 } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' with value/, "Bad args for complex types throws an exception";
84 Foo->new( my_class => [ 10 ] );
85 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/;
87 Foo->new( my_class => [ {foo => 'bar'} ] );
88 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/;
92 Foo->new( my_role => [ 20 ] );
93 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/;
95 Foo->new( my_role => [ {foo => 'bar'} ] );
96 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/;
103 use Mouse::Util::TypeConstraints;
106 => as 'ArrayRef[HashRef]'
109 => from 'ArrayRef[Str]'
111 [ map { +{ $_ => 1 } } @$_ ]
122 my @list = ( {a => 1}, {b => 1}, {c => 1} );
123 my $bar = Bar->new(list => [ qw(a b c) ]);
125 is_deeply( $bar->list, \@list, "list is as expected");
127 or diag( Mouse::Util::TypeConstraints::find_type_constraint("Bar::List")->dump );
130 Bar->new(list => [ { 1 => 2 }, 2, 3 ]);
131 } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' with value/, "Bad coercion parameter throws an error";
134 use Mouse::Util::TypeConstraints;
136 my $t = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
137 ok $t->is_a_type_of($t), "$t is a type of $t";
138 ok $t->is_a_type_of('Maybe'), "$t is a type of Maybe";
140 # XXX: how about 'MaybeInt[ Int ]'?
141 ok $t->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
143 ok!$t->is_a_type_of('Int');
149 my $u = subtype 'MaybeInt', as 'Maybe[Int]';
150 ok $u->is_a_type_of($t), "$t is a type of $t";
151 ok $u->is_a_type_of('Maybe'), "$t is a type of Maybe";
153 # XXX: how about 'MaybeInt[ Int ]'?
154 ok $u->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
156 ok!$u->is_a_type_of('Int');
162 # XXX: undefined hehaviour
163 # ok $t->is_a_type_of($u);
164 # ok $u->is_a_type_of($t);
166 my $w = subtype as 'Maybe[ ArrayRef | HashRef ]';
173 ok $w->is_a_type_of('Maybe');
174 ok $w->is_a_type_of('Maybe[ArrayRef|HashRef]');
175 ok!$w->is_a_type_of('ArrayRef');
177 my $x = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[ ArrayRef[ Int | Undef ] ]');
179 ok $x->is_a_type_of('ArrayRef');
180 ok $x->is_a_type_of('ArrayRef[ArrayRef[Int|Undef]]');
181 ok!$x->is_a_type_of('ArrayRef[ArrayRef[Str]]');
185 ok $x->check([[10]]);
186 ok $x->check([[10, undef]]);
187 ok!$x->check([[10, 3.14]]);
190 $x = tie my @ta, 'Tie::StdArray';
192 my $array_of_int = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]');
195 ok $array_of_int->check(\@ta), 'magical array';
198 ok !$array_of_int->check(\@ta);
200 $x = tie my %th, 'Tie::StdHash';
202 my $hash_of_int = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]');
204 %$x = (foo => 1, bar => 3, baz => 5);
205 ok $hash_of_int->check(\%th), 'magical hash';
208 ok!$hash_of_int->check(\%th);
211 while(my($k, $v) = each %th){
215 is( $hash_of_int->type_parameter, 'Int' );
217 if('Mouse' eq ('Mo' . 'use')){ # under Mouse
218 ok $hash_of_int->__is_parameterized();
219 ok!$hash_of_int->type_parameter->__is_parameterized();
222 ok $hash_of_int->can('type_parameter');
223 ok!$hash_of_int->type_parameter->can('type_parameter');
226 is_deeply \%th_clone, \%th, 'the hash iterator is initialized';
230 diag "derived from parameterized types #$i";
232 my $myhashref = subtype 'MyHashRef',
234 where { keys %$_ > 1 };
236 ok $myhashref->is_a_type_of('HashRef'), "$myhashref";
237 ok $myhashref->check({ a => 43, b => 100 });
238 ok $myhashref->check({ a => 43, b => 3.14 });
239 ok !$myhashref->check({});
240 ok !$myhashref->check({ a => 42, b => [] });
242 is $myhashref->type_parameter, 'Value';
244 $myhashref = subtype 'H', as 'MyHashRef[Int]';
246 ok $myhashref->is_a_type_of('HashRef'), "$myhashref";
247 ok $myhashref->check({ a => 43, b => 100 });
248 ok $myhashref->check({ a => 43, b => 100, c => 0 });
249 ok !$myhashref->check({}), 'empty hash';
250 ok !$myhashref->check({ foo => 42 });
251 ok !$myhashref->check({ a => 43, b => "foo" });
252 ok !$myhashref->check({ a => 42, b => [] });
253 ok !$myhashref->check({ a => 42, b => undef });
254 ok !$myhashref->check([42]);
255 ok !$myhashref->check("foo");
257 is $myhashref->type_parameter, 'Int';