10 eval "use IO::String; use IO::File;";
\r
11 plan skip_all => "IO::String and IO::File are required for this test" if $@;
\r
18 package Email::Mouse;
\r
20 use Mouse::Util::TypeConstraints;
\r
24 our $VERSION = '0.01';
\r
26 # create subtype for IO::String
\r
28 subtype 'IO::String'
\r
30 => where { $_->isa('IO::String') };
\r
34 => via { IO::String->new($_) },
\r
35 => from 'ScalarRef',
\r
36 => via { IO::String->new($_) };
\r
38 # create subtype for IO::File
\r
42 => where { $_->isa('IO::File') };
\r
45 => from 'FileHandle'
\r
46 => via { bless $_, 'IO::File' };
\r
50 my $st = subtype 'IO::StringOrFile' => as 'IO::String | IO::File';
\r
56 isa => 'IO::StringOrFile',
\r
58 default => sub { IO::String->new() },
\r
63 my $fh = $self->raw_body();
\r
65 return do { local $/; <$fh> };
\r
70 my $email = Email::Mouse->new;
\r
71 isa_ok($email, 'Email::Mouse');
\r
73 isa_ok($email->raw_body, 'IO::String');
\r
75 is($email->as_string, undef, '... got correct empty string');
\r
79 my $email = Email::Mouse->new(raw_body => '... this is my body ...');
\r
80 isa_ok($email, 'Email::Mouse');
\r
82 isa_ok($email->raw_body, 'IO::String');
\r
84 is($email->as_string, '... this is my body ...', '... got correct string');
\r
87 $email->raw_body('... this is the next body ...');
\r
88 } '... this will coerce correctly';
\r
90 isa_ok($email->raw_body, 'IO::String');
\r
92 is($email->as_string, '... this is the next body ...', '... got correct string');
\r
96 my $str = '... this is my body (ref) ...';
\r
98 my $email = Email::Mouse->new(raw_body => \$str);
\r
99 isa_ok($email, 'Email::Mouse');
\r
101 isa_ok($email->raw_body, 'IO::String');
\r
103 is($email->as_string, $str, '... got correct string');
\r
105 my $str2 = '... this is the next body (ref) ...';
\r
108 $email->raw_body(\$str2);
\r
109 } '... this will coerce correctly';
\r
111 isa_ok($email->raw_body, 'IO::String');
\r
113 is($email->as_string, $str2, '... got correct string');
\r
117 my $io_str = IO::String->new('... this is my body (IO::String) ...');
\r
119 my $email = Email::Mouse->new(raw_body => $io_str);
\r
120 isa_ok($email, 'Email::Mouse');
\r
122 isa_ok($email->raw_body, 'IO::String');
\r
123 is($email->raw_body, $io_str, '... and it is the one we expected');
\r
125 is($email->as_string, '... this is my body (IO::String) ...', '... got correct string');
\r
127 my $io_str2 = IO::String->new('... this is the next body (IO::String) ...');
\r
130 $email->raw_body($io_str2);
\r
131 } '... this will coerce correctly';
\r
133 isa_ok($email->raw_body, 'IO::String');
\r
134 is($email->raw_body, $io_str2, '... and it is the one we expected');
\r
136 is($email->as_string, '... this is the next body (IO::String) ...', '... got correct string');
\r
142 open($fh, '<', $0) || die "Could not open $0";
\r
144 my $email = Email::Mouse->new(raw_body => $fh);
\r
145 isa_ok($email, 'Email::Mouse');
\r
147 isa_ok($email->raw_body, 'IO::File');
\r
153 my $fh = IO::File->new($0);
\r
155 my $email = Email::Mouse->new(raw_body => $fh);
\r
156 isa_ok($email, 'Email::Mouse');
\r
158 isa_ok($email->raw_body, 'IO::File');
\r
159 is($email->raw_body, $fh, '... and it is the one we expected');
\r