3 use Test::More tests => 13;
6 package Response::Headers;
8 has 'foo' => ( is => 'rw' );
11 package Request::Headers;
13 has 'foo' => ( is => 'rw' );
19 use Mouse::Util::TypeConstraints;
21 subtype 'Headers' => as 'Object', where { $_->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(%{ $_ });
102 my $req = Request->new(headers => { foo => 'bar' });
103 isa_ok($req->headers, 'Response::Headers');
104 is($req->headers->foo, 'bar');
105 $req->headers({foo => 'yay'});
106 isa_ok($req->headers, 'Response::Headers');
107 is($req->headers->foo, 'yay');
109 my $res = Response->new(headers => { foo => 'bar' });
110 isa_ok($res->headers, 'Response::Headers');
111 is($res->headers->foo, 'bar');
112 $res->headers({foo => 'yay'});
113 isa_ok($res->headers, 'Response::Headers');
114 is($res->headers->foo, 'yay');