3 use Test::More tests => 14;
6 package Response::Headers;
8 has 'foo' => ( is => 'rw' );
11 package Request::Headers;
13 has 'foo' => ( is => 'rw' );
19 use Mouse::Util::TypeConstraints;
21 type 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } };
23 from 'HashRef' => via {
24 Response::Headers->new(%{ $_ });
37 use Mouse::Util::TypeConstraints;
39 type 'Headers' => where { defined $_ && eval { $_->isa('Request::Headers') } };
41 like $@, qr/The type constraint 'Headers' has already been created in Response and cannot be created again in Request/;
45 use Mouse::Util::TypeConstraints;
48 from 'HashRef' => via {
49 Request::Headers->new(%{ $_ });
53 like $@, qr/Cannot find type 'TooBad', perhaps you forgot to load it./;
57 use Mouse::Util::TypeConstraints;
60 from 'HashRef' => via {
61 Request::Headers->new(%{ $_ });
65 like $@, qr/A coercion action already exists for 'HashRef'/;
69 use Mouse::Util::TypeConstraints;
77 like $@, qr/Could not find the type constraint \(XXX\) to coerce from/;
81 use Mouse::Util::TypeConstraints;
84 from 'ArrayRef' => via {
85 Request::Headers->new(%{ $_ });
93 type 'Headers' => where {
94 eval { $_->isa('Response::Headers') }
97 ok(!$@, "You can redefine types in their original package");
111 my $req = Request->new(headers => { foo => 'bar' });
112 isa_ok($req->headers, 'Response::Headers');
113 is($req->headers->foo, 'bar');
114 $req->headers({foo => 'yay'});
115 isa_ok($req->headers, 'Response::Headers');
116 is($req->headers->foo, 'yay');
118 my $res = Response->new(headers => { foo => 'bar' });
119 isa_ok($res->headers, 'Response::Headers');
120 is($res->headers->foo, 'bar');
121 $res->headers({foo => 'yay'});
122 isa_ok($res->headers, 'Response::Headers');
123 is($res->headers->foo, 'yay');