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