X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F043-parameterized-type.t;h=a7eae99992eb8c0da47d5094eca6c22fd05c1e44;hb=284ca75b14da9b2685a78b7a0d4228512c8575c6;hp=7bc3edb492b2a3ac5a3500a1f11b5dce010e80be;hpb=315b97c2ab833293683baad8c1773fd0d48ab5a4;p=gitmo%2FMouse.git diff --git a/t/043-parameterized-type.t b/t/043-parameterized-type.t index 7bc3edb..a7eae99 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 => 7; +use Test::More tests => 16; 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,55 @@ 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/; +} + +{ + { + package Bar; + use Mouse; + use Mouse::Util::TypeConstraints; + + subtype 'Bar::List' + => as 'ArrayRef[HashRef]' + ; + coerce 'Bar::List' + => from 'ArrayRef[Str]' + => via { + [ map { +{ $_ => 1 } } @$_ ] + } + ; + has 'list' => ( + is => 'ro', + isa => 'Bar::List', + coerce => 1, + ); + } + + lives_and { + my @list = ( {a => 1}, {b => 1}, {c => 1} ); + my $bar = Bar->new(list => [ qw(a b c) ]); + + is_deeply( $bar->list, \@list, "list is as expected"); + } "coercion works"; + + throws_ok { + Bar->new(list => [ { 1 => 2 }, 2, 3 ]); + } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error"; }