10 eval "use IO::String; use IO::File;";
11 plan skip_all => "IO::String and IO::File are required for this test" if $@;
22 use Moose::Util::TypeConstraints;
26 our $VERSION = '0.01';
28 # create subtype for IO::String
32 => where { $_->isa('IO::String') };
36 => via { IO::String->new($_) },
38 => via { IO::String->new($_) };
40 # create subtype for IO::File
44 => where { $_->isa('IO::File') };
48 => via { bless $_, 'IO::File' };
52 subtype 'IO::StringOrFile' => as 'IO::String | IO::File';
58 isa => 'IO::StringOrFile',
60 default => sub { IO::String->new() },
65 my $fh = $self->raw_body();
66 return do { local $/; <$fh> };
71 my $email = Email::Moose->new;
72 isa_ok($email, 'Email::Moose');
74 isa_ok($email->raw_body, 'IO::String');
76 is($email->as_string, undef, '... got correct empty string');
80 my $email = Email::Moose->new(raw_body => '... this is my body ...');
81 isa_ok($email, 'Email::Moose');
83 isa_ok($email->raw_body, 'IO::String');
85 is($email->as_string, '... this is my body ...', '... got correct string');
88 $email->raw_body('... this is the next body ...');
89 } '... this will coerce correctly';
91 isa_ok($email->raw_body, 'IO::String');
93 is($email->as_string, '... this is the next body ...', '... got correct string');
97 my $str = '... this is my body (ref) ...';
99 my $email = Email::Moose->new(raw_body => \$str);
100 isa_ok($email, 'Email::Moose');
102 isa_ok($email->raw_body, 'IO::String');
104 is($email->as_string, $str, '... got correct string');
106 my $str2 = '... this is the next body (ref) ...';
109 $email->raw_body(\$str2);
110 } '... this will coerce correctly';
112 isa_ok($email->raw_body, 'IO::String');
114 is($email->as_string, $str2, '... got correct string');
118 my $io_str = IO::String->new('... this is my body (IO::String) ...');
120 my $email = Email::Moose->new(raw_body => $io_str);
121 isa_ok($email, 'Email::Moose');
123 isa_ok($email->raw_body, 'IO::String');
124 is($email->raw_body, $io_str, '... and it is the one we expected');
126 is($email->as_string, '... this is my body (IO::String) ...', '... got correct string');
128 my $io_str2 = IO::String->new('... this is the next body (IO::String) ...');
131 $email->raw_body($io_str2);
132 } '... this will coerce correctly';
134 isa_ok($email->raw_body, 'IO::String');
135 is($email->raw_body, $io_str2, '... and it is the one we expected');
137 is($email->as_string, '... this is the next body (IO::String) ...', '... got correct string');
143 open($fh, '<', $0) || die "Could not open $0";
145 my $email = Email::Moose->new(raw_body => $fh);
146 isa_ok($email, 'Email::Moose');
148 isa_ok($email->raw_body, 'IO::File');
154 my $fh = IO::File->new($0);
156 my $email = Email::Moose->new(raw_body => $fh);
157 isa_ok($email, 'Email::Moose');
159 isa_ok($email->raw_body, 'IO::File');
160 is($email->raw_body, $fh, '... and it is the one we expected');