Finish renaming Mouse::TypeRegistry to Mouse::Util::TypeConstraints
[gitmo/Mouse.git] / t / 501_moose_coerce_mouse.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 BEGIN {
9     plan skip_all => "Moose required for this test" unless eval { require Moose  && Moose->VERSION('0.59') };
10     plan tests => 5;
11 }
12
13 use Test::Exception;
14
15 {
16     package Headers;
17     use Mouse;
18     has 'foo' => ( is => 'rw' );
19 }
20 {
21     package Response;
22     use Mouse;
23     use Mouse::Util::TypeConstraints;
24
25     subtype 'HeadersType' => where { defined $_ && eval { $_->isa('Headers') } };
26     coerce  'HeadersType' =>
27         from 'HashRef' => via {
28             Headers->new(%{ $_ });
29         },
30     ;
31
32     has headers => (
33         is     => 'rw',
34         isa    => 'HeadersType',
35         coerce => 1,
36     );
37 }
38 {
39     package Mosponse;
40     use Moose;
41     extends qw(Response);
42     ::lives_ok { extends qw(Response) } "extend Mouse class with Moose";
43 }
44
45 {
46     local $TODO = "Doesn't work in the constructor yet?";
47     my $r = Mosponse->new(headers => { foo => 'bar' });
48     isa_ok($r->headers, 'Headers');
49     is(eval{$r->headers->foo}, 'bar');
50 }
51
52 {
53     my $r = Mosponse->new;
54     $r->headers({foo => 'yay'});
55     isa_ok($r->headers, 'Headers');
56     is($r->headers->foo, 'yay');
57 }