complete re-organization of the test suite
[gitmo/Moose.git] / t / 040_type_constraints / 009_union_types_and_coercions.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 BEGIN {
10     eval "use IO::String; use IO::File;";
11     plan skip_all => "IO::String and IO::File are required for this test" if $@;        
12     plan tests => 29;    
13 }
14
15 BEGIN {
16     use_ok('Moose');           
17 }
18
19 {
20     package Email::Moose;
21     use Moose;
22     use Moose::Util::TypeConstraints;
23
24     use IO::String;
25
26     our $VERSION = '0.01';
27
28     # create subtype for IO::String
29
30     subtype 'IO::String'
31         => as 'Object'
32         => where { $_->isa('IO::String') };
33
34     coerce 'IO::String'
35         => from 'Str'
36             => via { IO::String->new($_) },
37         => from 'ScalarRef',
38             => via { IO::String->new($_) };
39
40     # create subtype for IO::File
41
42     subtype 'IO::File'
43         => as 'Object'
44         => where { $_->isa('IO::File') };
45
46     coerce 'IO::File'
47         => from 'FileHandle'
48             => via { bless $_, 'IO::File' };
49     
50     # attributes
51     
52     has 'raw_body' => (
53         is      => 'rw',
54         isa     => 'IO::String | IO::File',
55         coerce  => 1,
56         default => sub { IO::String->new() },
57     );
58
59     sub as_string {
60         my ($self) = @_;
61         my $fh = $self->raw_body();
62         return do { local $/; <$fh> };
63     }
64 }
65
66 {
67     my $email = Email::Moose->new;
68     isa_ok($email, 'Email::Moose');
69
70     isa_ok($email->raw_body, 'IO::String');
71     
72     is($email->as_string, undef, '... got correct empty string');
73 }
74
75 {
76     my $email = Email::Moose->new(raw_body => '... this is my body ...');
77     isa_ok($email, 'Email::Moose');
78     
79     isa_ok($email->raw_body, 'IO::String');
80     
81     is($email->as_string, '... this is my body ...', '... got correct string'); 
82     
83     lives_ok {
84         $email->raw_body('... this is the next body ...');   
85     } '... this will coerce correctly';
86     
87     isa_ok($email->raw_body, 'IO::String');
88     
89     is($email->as_string, '... this is the next body ...', '... got correct string');    
90 }
91
92 {
93     my $str = '... this is my body (ref) ...';
94     
95     my $email = Email::Moose->new(raw_body => \$str);
96     isa_ok($email, 'Email::Moose');
97     
98     isa_ok($email->raw_body, 'IO::String');
99     
100     is($email->as_string, $str, '... got correct string');    
101     
102     my $str2 = '... this is the next body (ref) ...';    
103     
104     lives_ok {
105         $email->raw_body(\$str2);   
106     } '... this will coerce correctly';
107     
108     isa_ok($email->raw_body, 'IO::String');
109     
110     is($email->as_string, $str2, '... got correct string');    
111 }
112
113 {
114     my $io_str = IO::String->new('... this is my body (IO::String) ...');
115     
116     my $email = Email::Moose->new(raw_body => $io_str);
117     isa_ok($email, 'Email::Moose');
118     
119     isa_ok($email->raw_body, 'IO::String');
120     is($email->raw_body, $io_str, '... and it is the one we expected');
121     
122     is($email->as_string, '... this is my body (IO::String) ...', '... got correct string'); 
123     
124     my $io_str2 = IO::String->new('... this is the next body (IO::String) ...');    
125     
126     lives_ok {
127         $email->raw_body($io_str2);   
128     } '... this will coerce correctly';
129     
130     isa_ok($email->raw_body, 'IO::String');
131     is($email->raw_body, $io_str2, '... and it is the one we expected');
132     
133     is($email->as_string, '... this is the next body (IO::String) ...', '... got correct string');       
134 }
135
136 {
137     my $fh;
138     
139     open($fh, '<', $0) || die "Could not open $0";
140     
141     my $email = Email::Moose->new(raw_body => $fh);
142     isa_ok($email, 'Email::Moose');
143     
144     isa_ok($email->raw_body, 'IO::File');
145     
146     close($fh);
147 }
148
149 {
150     my $fh = IO::File->new($0);
151     
152     my $email = Email::Moose->new(raw_body => $fh);
153     isa_ok($email, 'Email::Moose');
154     
155     isa_ok($email->raw_body, 'IO::File');
156     is($email->raw_body, $fh, '... and it is the one we expected');
157 }
158
159
160