Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 040_type_constraints / 009_union_types_and_coercions.t
index ca8fcab..91f7cc8 100644 (file)
@@ -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;