X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F800_shikabased%2F002-coerce_multi_class.t;h=0e2d9039e6ed8bb03afbda5486040e8ebfbbe3c2;hb=7f408f4aa3fc94662299107b1e1cc78818121ae4;hp=915a144dee7414fbfc7e0c6dbd549aaf3cf691d9;hpb=b6ffa107ada7a537575481d277818e207ed219fb;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..0e2d903 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 => 13; { package Response::Headers; @@ -16,14 +16,14 @@ use Test::More tests => 8; { package Response; use Mouse; - use Mouse::TypeRegistry; + use Mouse::Util::TypeConstraints; - subtype 'Headers' => sub { defined $_ && eval { $_->isa('Response::Headers') } }; - coerce 'Headers' => +{ - HashRef => sub { + type 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } }; + coerce 'Headers' => + from 'HashRef' => via { Response::Headers->new(%{ $_ }); }, - }; + ; has headers => ( is => 'rw', @@ -32,17 +32,65 @@ use Test::More tests => 8; ); } -{ +eval { package Request; - use Mouse; - use Mouse::TypeRegistry; + use Mouse::Util::TypeConstraints; + + type 'Headers' => where { defined $_ && eval { $_->isa('Request::Headers') } }; +}; +like $@, qr/The type constraint 'Headers' has already been created in Response and cannot be created again in Request/; + +eval { + package Request; + use Mouse::Util::TypeConstraints; + + 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::Util::TypeConstraints; + + coerce 'Headers' => + from 'HashRef' => via { + Request::Headers->new(%{ $_ }); + }, + ; +}; +like $@, qr/A coercion action already exists for 'HashRef'/; + +eval { + package Request; + use Mouse::Util::TypeConstraints; - subtype 'Headers' => sub { defined $_ && eval { $_->isa('Request::Headers') } }; - coerce 'Headers' => +{ - HashRef => sub { + coerce 'Int' => + from 'XXX' => via { + 1 + }, + ; +}; +like $@, qr/Could not find the type constraint \(XXX\) to coerce from/; + +eval { + package Request; + use Mouse::Util::TypeConstraints; + + coerce 'Headers' => + from 'ArrayRef' => via { Request::Headers->new(%{ $_ }); }, - }; + ; +}; +ok !$@; + +{ + package Request; + use Mouse; has headers => ( is => 'rw', @@ -51,21 +99,11 @@ 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' }); @@ -74,3 +112,4 @@ is($res->headers->foo, 'bar'); $res->headers({foo => 'yay'}); isa_ok($res->headers, 'Response::Headers'); is($res->headers->foo, 'yay'); +