Moose now warns when you try to load it from the main package. Added a
[gitmo/Moose.git] / t / 000_recipes / basics / 005_coercion.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9     eval "use HTTP::Headers; use Params::Coerce; use URI;";
10     plan skip_all => "HTTP::Headers & Params::Coerce & URI required for this test" if $@;        
11     plan tests => 17;    
12 }
13
14 use Test::Exception;
15
16 {
17         package Request;
18         use Moose;
19     use Moose::Util::TypeConstraints;
20         
21         use HTTP::Headers  ();
22         use Params::Coerce ();
23         use URI            ();
24
25         subtype Header
26             => as Object
27             => where { $_->isa('HTTP::Headers') };
28
29         coerce Header
30             => from ArrayRef
31                 => via { HTTP::Headers->new( @{ $_ } ) }
32             => from HashRef
33                 => via { HTTP::Headers->new( %{ $_ } ) };
34
35         subtype Uri
36             => as Object
37             => where { $_->isa('URI') };
38
39         coerce Uri
40             => from Object
41                 => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ) }
42             => from Str
43                 => via { URI->new( $_, 'http' ) };
44
45         subtype Protocol
46             => as Str
47             => where { /^HTTP\/[0-9]\.[0-9]$/ };
48
49
50         has 'base'     => (is => 'rw', isa => 'Uri', coerce  => 1);
51         has 'url'      => (is => 'rw', isa => 'Uri', coerce  => 1);     
52         has 'method'   => (is => 'rw', isa => 'Str');   
53         has 'protocol' => (is => 'rw', isa => 'Protocol');              
54         has 'headers'  => (
55             is      => 'rw',
56             isa     => 'Header',
57             coerce  => 1,
58             default => sub { HTTP::Headers->new } 
59     );
60     
61     __PACKAGE__->meta->make_immutable(debug => 0);
62 }
63
64 my $r = Request->new;
65 isa_ok($r, 'Request');
66
67 {
68     my $header = $r->headers;
69     isa_ok($header, 'HTTP::Headers');
70
71     is($r->headers->content_type, '', '... got no content type in the header');
72
73     $r->headers( { content_type => 'text/plain' } );
74
75     my $header2 = $r->headers;
76     isa_ok($header2, 'HTTP::Headers');
77     isnt($header, $header2, '... created a new HTTP::Header object');
78
79     is($header2->content_type, 'text/plain', '... got the right content type in the header');
80
81     $r->headers( [ content_type => 'text/html' ] );
82
83     my $header3 = $r->headers;
84     isa_ok($header3, 'HTTP::Headers');
85     isnt($header2, $header3, '... created a new HTTP::Header object');
86
87     is($header3->content_type, 'text/html', '... got the right content type in the header');
88     
89     $r->headers( HTTP::Headers->new(content_type => 'application/pdf') );
90     
91     my $header4 = $r->headers;    
92     isa_ok($header4, 'HTTP::Headers');
93     isnt($header3, $header4, '... created a new HTTP::Header object');
94
95     is($header4->content_type, 'application/pdf', '... got the right content type in the header');    
96     
97     dies_ok {
98         $r->headers('Foo')
99     } '... dies when it gets bad params';
100 }
101
102 {
103     is($r->protocol, undef, '... got nothing by default');
104
105     lives_ok {
106         $r->protocol('HTTP/1.0');
107     } '... set the protocol correctly';
108     is($r->protocol, 'HTTP/1.0', '... got nothing by default');
109             
110     dies_ok {
111         $r->protocol('http/1.0');
112     } '... the protocol died with bar params correctly';            
113 }
114