X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=t%2F040_type_constraints%2F009_union_types_and_coercions.t;h=91f7cc888efc1cc51a8b83721bb3244f06210661;hp=ca8fcab4035aed4ad99c53292f2da8c4447a42e2;hb=HEAD;hpb=b36cef5c9ef6b86439ed67cd94be97bec04037bf diff --git a/t/040_type_constraints/009_union_types_and_coercions.t b/t/040_type_constraints/009_union_types_and_coercions.t index ca8fcab..91f7cc8 100644 --- a/t/040_type_constraints/009_union_types_and_coercions.t +++ b/t/040_type_constraints/009_union_types_and_coercions.t @@ -1,4 +1,7 @@ #!/usr/bin/perl +# This is automatically generated by author/import-moose-test.pl. +# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!! +use t::lib::MooseCompat; use strict; use warnings; @@ -6,13 +9,10 @@ use warnings; use Test::More; use Test::Exception; -BEGIN { - eval "use IO::String; use IO::File;"; - plan skip_all => "IO::String and IO::File are required for this test" if $@; - plan tests => 28; -} - - +use Test::Requires { + 'IO::String' => '0.01', # skip all if not installed + 'IO::File' => '0.01', +}; { package Email::Mouse; @@ -47,8 +47,7 @@ BEGIN { # create the alias - my $st = subtype 'IO::StringOrFile' => as 'IO::String | IO::File'; - #::diag $st->dump; + subtype 'IO::StringOrFile' => as 'IO::String | IO::File'; # attributes @@ -62,7 +61,6 @@ BEGIN { sub as_string { my ($self) = @_; my $fh = $self->raw_body(); - return do { local $/; <$fh> }; } } @@ -160,5 +158,36 @@ BEGIN { is($email->raw_body, $fh, '... and it is the one we expected'); } +{ + package Foo; + + use Mouse; + use Mouse::Util::TypeConstraints; + + subtype 'Coerced' => as 'ArrayRef'; + coerce 'Coerced' + => from 'Value' + => via { [ $_ ] }; + + has carray => ( + is => 'ro', + isa => 'Coerced | Coerced', + coerce => 1, + ); +} + +{ + my $foo; + lives_ok { $foo = Foo->new( carray => 1 ) } + 'Can pass non-ref value for carray'; + is_deeply( + $foo->carray, [1], + 'carray was coerced to an array ref' + ); + throws_ok { 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'; +} +done_testing;