10 'IO::String' => '0.01', # skip all if not installed
17 use Moose::Util::TypeConstraints;
21 our $VERSION = '0.01';
23 # create subtype for IO::String
27 => where { $_->isa('IO::String') };
31 => via { IO::String->new($_) },
33 => via { IO::String->new($_) };
35 # create subtype for IO::File
39 => where { $_->isa('IO::File') };
43 => via { bless $_, 'IO::File' };
47 subtype 'IO::StringOrFile' => as 'IO::String | IO::File';
53 isa => 'IO::StringOrFile',
55 default => sub { IO::String->new() },
60 my $fh = $self->raw_body();
61 return do { local $/; <$fh> };
66 my $email = Email::Moose->new;
67 isa_ok($email, 'Email::Moose');
69 isa_ok($email->raw_body, 'IO::String');
71 is($email->as_string, undef, '... got correct empty string');
75 my $email = Email::Moose->new(raw_body => '... this is my body ...');
76 isa_ok($email, 'Email::Moose');
78 isa_ok($email->raw_body, 'IO::String');
80 is($email->as_string, '... this is my body ...', '... got correct string');
83 $email->raw_body('... this is the next body ...');
84 }, undef, '... this will coerce correctly' );
86 isa_ok($email->raw_body, 'IO::String');
88 is($email->as_string, '... this is the next body ...', '... got correct string');
92 my $str = '... this is my body (ref) ...';
94 my $email = Email::Moose->new(raw_body => \$str);
95 isa_ok($email, 'Email::Moose');
97 isa_ok($email->raw_body, 'IO::String');
99 is($email->as_string, $str, '... got correct string');
101 my $str2 = '... this is the next body (ref) ...';
104 $email->raw_body(\$str2);
105 }, undef, '... this will coerce correctly' );
107 isa_ok($email->raw_body, 'IO::String');
109 is($email->as_string, $str2, '... got correct string');
113 my $io_str = IO::String->new('... this is my body (IO::String) ...');
115 my $email = Email::Moose->new(raw_body => $io_str);
116 isa_ok($email, 'Email::Moose');
118 isa_ok($email->raw_body, 'IO::String');
119 is($email->raw_body, $io_str, '... and it is the one we expected');
121 is($email->as_string, '... this is my body (IO::String) ...', '... got correct string');
123 my $io_str2 = IO::String->new('... this is the next body (IO::String) ...');
126 $email->raw_body($io_str2);
127 }, undef, '... this will coerce correctly' );
129 isa_ok($email->raw_body, 'IO::String');
130 is($email->raw_body, $io_str2, '... and it is the one we expected');
132 is($email->as_string, '... this is the next body (IO::String) ...', '... got correct string');
138 open($fh, '<', $0) || die "Could not open $0";
140 my $email = Email::Moose->new(raw_body => $fh);
141 isa_ok($email, 'Email::Moose');
143 isa_ok($email->raw_body, 'IO::File');
149 my $fh = IO::File->new($0);
151 my $email = Email::Moose->new(raw_body => $fh);
152 isa_ok($email, 'Email::Moose');
154 isa_ok($email->raw_body, 'IO::File');
155 is($email->raw_body, $fh, '... and it is the one we expected');
162 use Moose::Util::TypeConstraints;
164 subtype 'Coerced' => as 'ArrayRef';
171 isa => 'Coerced | Coerced',
178 is( exception { $foo = Foo->new( carray => 1 ) }, undef, 'Can pass non-ref value for carray' );
181 'carray was coerced to an array ref'
184 like( exception { Foo->new( carray => {} ) }, qr/\QValidation failed for 'Coerced|Coerced' with value \E(?!undef)/, 'Cannot pass a hash ref for carray attribute, and hash ref is not coerced to an undef' );