X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F800_shikabased%2F002-coerce_multi_class.t;h=5b9775b6751be5993444758775151043d67b6467;hb=61a02a3addc8542afac0bbd222610d7e050137ce;hp=915a144dee7414fbfc7e0c6dbd549aaf3cf691d9;hpb=b0000b3d2779a5829883471f95f4a8548b486c82;p=gitmo%2FMouse.git diff --git a/t/800_shikabased/002-coerce_multi_class.t b/t/800_shikabased/002-coerce_multi_class.t index 915a144..5b9775b 100644 --- a/t/800_shikabased/002-coerce_multi_class.t +++ b/t/800_shikabased/002-coerce_multi_class.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 8; +use Test::More tests => 14; { package Response::Headers; @@ -18,12 +18,12 @@ use Test::More tests => 8; use Mouse; use Mouse::TypeRegistry; - subtype 'Headers' => sub { defined $_ && eval { $_->isa('Response::Headers') } }; - coerce 'Headers' => +{ - HashRef => sub { + subtype 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } }; + coerce 'Headers' => + from 'HashRef' => via { Response::Headers->new(%{ $_ }); }, - }; + ; has headers => ( is => 'rw', @@ -32,17 +32,71 @@ use Test::More tests => 8; ); } -{ +eval { + package Request; + use Mouse::TypeRegistry; + + subtype 'Headers' => where { defined $_ && eval { $_->isa('Request::Headers') } }; +}; +like $@, qr/The type constraint 'Headers' has already been created, cannot be created again in Request/; + +eval { + package Request; + use Mouse::TypeRegistry; + + coerce 'TooBad' => + from 'HashRef' => via { + Request::Headers->new(%{ $_ }); + }, + ; +}; +like $@, qr/Cannot find type 'TooBad', perhaps you forgot to load it./; + +eval { + package Request; + use Mouse::TypeRegistry; + + coerce 'Headers' => + from 'HashRef' => via { + Request::Headers->new(%{ $_ }); + }, + ; +}; +like $@, qr/A coercion action already exists for 'HashRef'/; + +eval { package Request; - use Mouse; use Mouse::TypeRegistry; - subtype 'Headers' => sub { defined $_ && eval { $_->isa('Request::Headers') } }; - coerce 'Headers' => +{ - HashRef => sub { + coerce 'Headers' => + from 'HashRefa' => via { Request::Headers->new(%{ $_ }); }, - }; + ; +}; +like $@, qr/Could not find the type constraint \(HashRefa\) to coerce from/; + +eval { + package Request; + use Mouse::TypeRegistry; + + coerce 'Headers' => + from 'ArrayRef' => via { + Request::Headers->new(%{ $_ }); + }, + ; +}; +ok !$@; + +eval { + package Response; + subtype 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } }; +}; +like $@, qr/The type constraint 'Headers' has already been created, cannot be created again in Response/; + +{ + package Request; + use Mouse; has headers => ( is => 'rw', @@ -51,21 +105,12 @@ use Test::More tests => 8; ); } -{ - package Response; - subtype 'Headers' => sub { defined $_ && eval { $_->isa('Response::Headers') } }; - coerce 'Headers' => +{ - HashRef => sub { - Response::Headers->new(%{ $_ }); - }, - }; -} my $req = Request->new(headers => { foo => 'bar' }); -isa_ok($req->headers, 'Request::Headers'); +isa_ok($req->headers, 'Response::Headers'); is($req->headers->foo, 'bar'); $req->headers({foo => 'yay'}); -isa_ok($req->headers, 'Request::Headers'); +isa_ok($req->headers, 'Response::Headers'); is($req->headers->foo, 'yay'); my $res = Response->new(headers => { foo => 'bar' });