more more moose way to type constraints
[gitmo/Mouse.git] / t / 800_shikabased / 002-coerce_multi_class.t
index 915a144..5b9775b 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 8;
+use Test::More tests => 14;
 
 {
     package Response::Headers;
@@ -18,12 +18,12 @@ use Test::More tests => 8;
     use Mouse;
     use Mouse::TypeRegistry;
 
-    subtype 'Headers' => sub { defined $_ && eval { $_->isa('Response::Headers') } };
-    coerce 'Headers' => +{
-        HashRef => sub {
+    subtype 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } };
+    coerce 'Headers' =>
+        from 'HashRef' => via {
             Response::Headers->new(%{ $_ });
         },
-    };
+    ;
 
     has headers => (
         is     => 'rw',
@@ -32,17 +32,71 @@ use Test::More tests => 8;
     );
 }
 
-{
+eval {
+    package Request;
+    use Mouse::TypeRegistry;
+
+    subtype 'Headers' => where { defined $_ && eval { $_->isa('Request::Headers') } };
+};
+like $@, qr/The type constraint 'Headers' has already been created, cannot be created again in Request/;
+
+eval {
+    package Request;
+    use Mouse::TypeRegistry;
+
+    coerce 'TooBad' =>
+        from 'HashRef' => via {
+            Request::Headers->new(%{ $_ });
+        },
+    ;
+};
+like $@, qr/Cannot find type 'TooBad', perhaps you forgot to load it./;
+
+eval {
+    package Request;
+    use Mouse::TypeRegistry;
+
+    coerce 'Headers' =>
+        from 'HashRef' => via {
+            Request::Headers->new(%{ $_ });
+        },
+    ;
+};
+like $@, qr/A coercion action already exists for 'HashRef'/;
+
+eval {
     package Request;
-    use Mouse;
     use Mouse::TypeRegistry;
 
-    subtype 'Headers' => sub { defined $_ && eval { $_->isa('Request::Headers') } };
-    coerce 'Headers' => +{
-        HashRef => sub {
+    coerce 'Headers' =>
+        from 'HashRefa' => via {
             Request::Headers->new(%{ $_ });
         },
-    };
+    ;
+};
+like $@, qr/Could not find the type constraint \(HashRefa\) to coerce from/;
+
+eval {
+    package Request;
+    use Mouse::TypeRegistry;
+
+    coerce 'Headers' =>
+        from 'ArrayRef' => via {
+            Request::Headers->new(%{ $_ });
+        },
+    ;
+};
+ok !$@;
+
+eval {
+    package Response;
+    subtype 'Headers' => where { defined $_ && eval { $_->isa('Response::Headers') } };
+};
+like $@, qr/The type constraint 'Headers' has already been created, cannot be created again in Response/;
+
+{
+    package Request;
+    use Mouse;
 
     has headers => (
         is     => 'rw',
@@ -51,21 +105,12 @@ use Test::More tests => 8;
     );
 }
 
-{
-    package Response;
-    subtype 'Headers' => sub { defined $_ && eval { $_->isa('Response::Headers') } };
-    coerce 'Headers' => +{
-        HashRef => sub {
-            Response::Headers->new(%{ $_ });
-        },
-    };
-}
 
 my $req = Request->new(headers => { foo => 'bar' });
-isa_ok($req->headers, 'Request::Headers');
+isa_ok($req->headers, 'Response::Headers');
 is($req->headers->foo, 'bar');
 $req->headers({foo => 'yay'});
-isa_ok($req->headers, 'Request::Headers');
+isa_ok($req->headers, 'Response::Headers');
 is($req->headers->foo, 'yay');
 
 my $res = Response->new(headers => { foo => 'bar' });