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