24 isa => 'HashRef[Int]',
29 isa => 'ArrayRef[Int]',
34 isa => 'ArrayRef[HashRef[Int]]'
39 isa => 'ArrayRef[My::Class]',
44 isa => 'ArrayRef[My::Role]',
48 ok(Foo->meta->has_attribute('foo'));
51 my $hash = { a => 1, b => 2, c => 3 };
52 my $array = [ 1, 2, 3 ];
53 my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
54 my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
56 is_deeply($foo->foo(), $hash, "foo is a proper hash");
57 is_deeply($foo->bar(), $array, "bar is a proper array");
58 is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
60 $foo->my_class([My::Class->new]);
61 is ref($foo->my_class), 'ARRAY';
62 isa_ok $foo->my_class->[0], 'My::Class';
64 $foo->my_role([My::Class->new]);
65 is ref($foo->my_role), 'ARRAY';
67 } "Parameterized constraints work";
71 Foo->new( foo => { a => 'b' });
72 } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' with value/, "Bad args for hash throws an exception";
75 Foo->new( bar => [ a => 'b' ]);
76 } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' with value/, "Bad args for array throws an exception";
79 Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
80 } 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";
83 Foo->new( my_class => [ 10 ] );
84 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/;
86 Foo->new( my_class => [ {foo => 'bar'} ] );
87 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/;
91 Foo->new( my_role => [ 20 ] );
92 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/;
94 Foo->new( my_role => [ {foo => 'bar'} ] );
95 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/;
102 use Mouse::Util::TypeConstraints;
105 => as 'ArrayRef[HashRef]'
108 => from 'ArrayRef[Str]'
110 [ map { +{ $_ => 1 } } @$_ ]
121 my @list = ( {a => 1}, {b => 1}, {c => 1} );
122 my $bar = Bar->new(list => [ qw(a b c) ]);
124 is_deeply( $bar->list, \@list, "list is as expected");
126 or diag( Mouse::Util::TypeConstraints::find_type_constraint("Bar::List")->dump );
129 Bar->new(list => [ { 1 => 2 }, 2, 3 ]);
130 } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' with value/, "Bad coercion parameter throws an error";
133 use Mouse::Util::TypeConstraints;
135 my $t = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]');
136 ok $t->is_a_type_of($t), "$t is a type of $t";
137 ok $t->is_a_type_of('Maybe'), "$t is a type of Maybe";
139 # XXX: how about 'MaybeInt[ Int ]'?
140 ok $t->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
142 ok!$t->is_a_type_of('Int');
148 my $u = subtype 'MaybeInt', as 'Maybe[Int]';
149 ok $u->is_a_type_of($t), "$t is a type of $t";
150 ok $u->is_a_type_of('Maybe'), "$t is a type of Maybe";
152 # XXX: how about 'MaybeInt[ Int ]'?
153 ok $u->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]";
155 ok!$u->is_a_type_of('Int');
161 # XXX: undefined hehaviour
162 # ok $t->is_a_type_of($u);
163 # ok $u->is_a_type_of($t);
165 my $w = subtype as 'Maybe[ ArrayRef | HashRef ]';
172 ok $w->is_a_type_of('Maybe');
173 ok $w->is_a_type_of('Maybe[ArrayRef|HashRef]');
174 ok!$w->is_a_type_of('ArrayRef');
176 my $x = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[ ArrayRef[ Int | Undef ] ]');
178 ok $x->is_a_type_of('ArrayRef');
179 ok $x->is_a_type_of('ArrayRef[ArrayRef[Int|Undef]]');
180 ok!$x->is_a_type_of('ArrayRef[ArrayRef[Str]]');
184 ok $x->check([[10]]);
185 ok $x->check([[10, undef]]);
186 ok!$x->check([[10, 3.14]]);
189 $x = tie my @ta, 'Tie::StdArray';
191 my $array_of_int = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]');
194 ok $array_of_int->check(\@ta), 'magical array';
197 ok !$array_of_int->check(\@ta);
199 $x = tie my %th, 'Tie::StdHash';
201 my $hash_of_int = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]');
203 %$x = (foo => 1, bar => 3, baz => 5);
204 ok $hash_of_int->check(\%th), 'magical hash';
207 ok!$hash_of_int->check(\%th);
210 while(my($k, $v) = each %th){
214 is( $hash_of_int->type_parameter, 'Int' );
216 if('Mouse' eq ('Mo' . 'use')){ # under Mouse
217 ok $hash_of_int->__is_parameterized();
218 ok!$hash_of_int->type_parameter->__is_parameterized();
221 ok $hash_of_int->can('type_parameter');
222 ok!$hash_of_int->type_parameter->can('type_parameter');
225 is_deeply \%th_clone, \%th, 'the hash iterator is initialized';
228 my $myhashref = subtype 'MyHashRef',
230 where { keys %$_ > 1 };
232 ok $myhashref->is_a_type_of('HashRef'), "$myhashref";
233 ok $myhashref->check({ a => 43, b => 100 });
234 ok $myhashref->check({ a => 43, b => 3.14 });
235 ok !$myhashref->check({});
236 ok !$myhashref->check({ a => 42, b => [] });
238 is $myhashref->type_parameter, 'Value';
240 $myhashref = subtype 'H', as 'MyHashRef[Int]';
242 ok $myhashref->is_a_type_of('HashRef'), "$myhashref";
243 ok $myhashref->check({ a => 43, b => 100 });
244 ok !$myhashref->check({ a => 43, b => 3.14 });
245 ok !$myhashref->check({});
246 ok !$myhashref->check({ a => 42, b => [] });
248 is $myhashref->type_parameter, 'Int';