X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F043-parameterized-type.t;h=6eaedddc864720894321bedba581fa5c0c398819;hb=a15d1371334de8874a74618597d0c1995fdca276;hp=8c204119a6205b607cb17cf0f045fc3c846e8fa4;hpb=310ad28b79e8761d5f8b24e54e06d3ebd3ad59b7;p=gitmo%2FMouse.git diff --git a/t/043-parameterized-type.t b/t/043-parameterized-type.t index 8c20411..6eaeddd 100644 --- a/t/043-parameterized-type.t +++ b/t/043-parameterized-type.t @@ -1,11 +1,19 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 46; use Test::Exception; { { + package My::Role; + use Mouse::Role; + + package My::Class; + use Mouse; + + with 'My::Role'; + package Foo; use Mouse; @@ -19,10 +27,20 @@ use Test::Exception; isa => 'ArrayRef[Int]', ); - has 'complex' => ( - is => 'rw', + has complex => ( + is => 'rw', isa => 'ArrayRef[HashRef[Int]]' ); + + has my_class => ( + is => 'rw', + isa => 'ArrayRef[My::Class]', + ); + + has my_role => ( + is => 'rw', + isa => 'ArrayRef[My::Role]', + ); }; ok(Foo->meta->has_attribute('foo')); @@ -36,6 +54,14 @@ use Test::Exception; is_deeply($foo->foo(), $hash, "foo is a proper hash"); is_deeply($foo->bar(), $array, "bar is a proper array"); is_deeply($foo->complex(), $complex, "complex is a proper ... structure"); + + $foo->my_class([My::Class->new]); + is ref($foo->my_class), 'ARRAY'; + isa_ok $foo->my_class->[0], 'My::Class'; + + $foo->my_role([My::Class->new]); + is ref($foo->my_role), 'ARRAY'; + } "Parameterized constraints work"; # check bad args @@ -50,6 +76,21 @@ use Test::Exception; throws_ok { Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] ) } 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"; + + throws_ok { + Foo->new( my_class => [ 10 ] ); + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + throws_ok { + Foo->new( my_class => [ {foo => 'bar'} ] ); + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + + + throws_ok { + Foo->new( my_role => [ 20 ] ); + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; + throws_ok { + Foo->new( my_role => [ {foo => 'bar'} ] ); + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; } { @@ -57,7 +98,7 @@ use Test::Exception; package Bar; use Mouse; use Mouse::Util::TypeConstraints; - + subtype 'Bar::List' => as 'ArrayRef[HashRef]' ; @@ -86,5 +127,60 @@ use Test::Exception; } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error"; } +use Mouse::Util::TypeConstraints; + +my $t = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]'); +ok $t->is_a_type_of($t), "$t is a type of $t"; +ok $t->is_a_type_of('Maybe'), "$t is a type of Maybe"; + +# XXX: how about 'MaybeInt[ Int ]'? +ok $t->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]"; + +ok!$t->is_a_type_of('Int'); + +ok $t->check(10); +ok $t->check(undef); +ok!$t->check(3.14); + +my $u = subtype 'MaybeInt', as 'Maybe[Int]'; +ok $u->is_a_type_of($t), "$t is a type of $t"; +ok $u->is_a_type_of('Maybe'), "$t is a type of Maybe"; + +# XXX: how about 'MaybeInt[ Int ]'? +ok $u->is_a_type_of('Maybe[Int]'), "$t is a type of Maybe[Int]"; + +ok!$u->is_a_type_of('Int'); + +ok $u->check(10); +ok $u->check(undef); +ok!$u->check(3.14); + +# XXX: undefined hehaviour +# ok $t->is_a_type_of($u); +# ok $u->is_a_type_of($t); + +my $w = subtype as 'Maybe[ ArrayRef | HashRef ]'; + +ok $w->check(undef); +ok $w->check([]); +ok $w->check({}); +ok!$w->check(sub{}); + +ok $w->is_a_type_of('Maybe'); +ok $w->is_a_type_of('Maybe[ArrayRef|HashRef]'); +ok!$w->is_a_type_of('ArrayRef'); + +my $x = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[ ArrayRef[ Int | Undef ] ]'); + +ok $x->is_a_type_of('ArrayRef'); +ok $x->is_a_type_of('ArrayRef[ArrayRef[Int|Undef]]'); +ok!$x->is_a_type_of('ArrayRef[ArrayRef[Str]]'); + +ok $x->check([]); +ok $x->check([[]]); +ok $x->check([[10]]); +ok $x->check([[10, undef]]); +ok!$x->check([[10, 3.14]]); +ok!$x->check({});