X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F043-parameterized-type.t;h=8c204119a6205b607cb17cf0f045fc3c846e8fa4;hb=f6ac0f1c3bc93a7123ea6de1344db97970fcf670;hp=7bc3edb492b2a3ac5a3500a1f11b5dce010e80be;hpb=315b97c2ab833293683baad8c1773fd0d48ab5a4;p=gitmo%2FMouse.git diff --git a/t/043-parameterized-type.t b/t/043-parameterized-type.t index 7bc3edb..8c20411 100644 --- a/t/043-parameterized-type.t +++ b/t/043-parameterized-type.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 7; +use Test::More tests => 9; use Test::Exception; { @@ -52,5 +52,39 @@ use Test::Exception; } 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"; } +{ + { + 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"; +} +