10 eval "use IO::String; use IO::File;";
11 plan skip_all => "IO::String and IO::File are required for this test" if $@;
18 use Moose::Util::TypeConstraints;
22 our $VERSION = '0.01';
24 # create subtype for IO::String
28 => where { $_->isa('IO::String') };
32 => via { IO::String->new($_) },
34 => via { IO::String->new($_) };
36 # create subtype for IO::File
40 => where { $_->isa('IO::File') };
44 => via { bless $_, 'IO::File' };
48 subtype 'IO::StringOrFile' => as 'IO::String | IO::File';
54 isa => 'IO::StringOrFile',
56 default => sub { IO::String->new() },
61 my $fh = $self->raw_body();
62 return do { local $/; <$fh> };
67 my $email = Email::Moose->new;
68 isa_ok($email, 'Email::Moose');
70 isa_ok($email->raw_body, 'IO::String');
72 is($email->as_string, undef, '... got correct empty string');
76 my $email = Email::Moose->new(raw_body => '... this is my body ...');
77 isa_ok($email, 'Email::Moose');
79 isa_ok($email->raw_body, 'IO::String');
81 is($email->as_string, '... this is my body ...', '... got correct string');
84 $email->raw_body('... this is the next body ...');
85 } '... this will coerce correctly';
87 isa_ok($email->raw_body, 'IO::String');
89 is($email->as_string, '... this is the next body ...', '... got correct string');
93 my $str = '... this is my body (ref) ...';
95 my $email = Email::Moose->new(raw_body => \$str);
96 isa_ok($email, 'Email::Moose');
98 isa_ok($email->raw_body, 'IO::String');
100 is($email->as_string, $str, '... got correct string');
102 my $str2 = '... this is the next body (ref) ...';
105 $email->raw_body(\$str2);
106 } '... this will coerce correctly';
108 isa_ok($email->raw_body, 'IO::String');
110 is($email->as_string, $str2, '... got correct string');
114 my $io_str = IO::String->new('... this is my body (IO::String) ...');
116 my $email = Email::Moose->new(raw_body => $io_str);
117 isa_ok($email, 'Email::Moose');
119 isa_ok($email->raw_body, 'IO::String');
120 is($email->raw_body, $io_str, '... and it is the one we expected');
122 is($email->as_string, '... this is my body (IO::String) ...', '... got correct string');
124 my $io_str2 = IO::String->new('... this is the next body (IO::String) ...');
127 $email->raw_body($io_str2);
128 } '... this will coerce correctly';
130 isa_ok($email->raw_body, 'IO::String');
131 is($email->raw_body, $io_str2, '... and it is the one we expected');
133 is($email->as_string, '... this is the next body (IO::String) ...', '... got correct string');
139 open($fh, '<', $0) || die "Could not open $0";
141 my $email = Email::Moose->new(raw_body => $fh);
142 isa_ok($email, 'Email::Moose');
144 isa_ok($email->raw_body, 'IO::File');
150 my $fh = IO::File->new($0);
152 my $email = Email::Moose->new(raw_body => $fh);
153 isa_ok($email, 'Email::Moose');
155 isa_ok($email->raw_body, 'IO::File');
156 is($email->raw_body, $fh, '... and it is the one we expected');