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