Fix expected error message
[gitmo/Moose.git] / t / 040_type_constraints / 007_util_more_type_coercion.t
index 6480222..e8a1044 100644 (file)
@@ -3,35 +3,32 @@
 use strict;
 use warnings;
 
-use Test::More tests => 26;
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
 
-BEGIN {
-    use_ok('Moose');           
-}
 
 {
     package HTTPHeader;
     use Moose;
     use Moose::Util::TypeConstraints;
-    
+
     coerce 'HTTPHeader'
-        => from ArrayRef 
+        => from ArrayRef
             => via { HTTPHeader->new(array => $_[0]) };
-            
+
     coerce 'HTTPHeader'
-        => from HashRef 
-            => via { HTTPHeader->new(hash => $_[0]) };    
-    
+        => from HashRef
+            => via { HTTPHeader->new(hash => $_[0]) };
+
     has 'array' => (is => 'ro');
-    has 'hash'  => (is => 'ro');    
+    has 'hash'  => (is => 'ro');
 
     package Engine;
     use strict;
     use warnings;
     use Moose;
-    
-    has 'header' => (is => 'rw', isa => 'HTTPHeader', coerce => 1);    
+
+    has 'header' => (is => 'rw', isa => 'HTTPHeader', coerce => 1);
 }
 
 {
@@ -40,9 +37,9 @@ BEGIN {
 
     # try with arrays
 
-    lives_ok {
+    ok ! exception {
         $engine->header([ 1, 2, 3 ]);
-    } '... type was coerced without incident';
+    }, '... type was coerced without incident';
     isa_ok($engine->header, 'HTTPHeader');
 
     is_deeply(
@@ -53,9 +50,9 @@ BEGIN {
 
     # try with hash
 
-    lives_ok {
+    ok ! exception {
         $engine->header({ one => 1, two => 2, three => 3 });
-    } '... type was coerced without incident';
+    }, '... type was coerced without incident';
     isa_ok($engine->header, 'HTTPHeader');
 
     is_deeply(
@@ -64,13 +61,13 @@ BEGIN {
         '... got the right hash value of the header');
     ok(!defined($engine->header->array), '... no array value set');
 
-    dies_ok {
-       $engine->header("Foo"); 
-    } '... dies with the wrong type, even after coercion';
+    ok exception {
+       $engine->header("Foo");
+    }, '... dies with the wrong type, even after coercion';
 
-    lives_ok {
-       $engine->header(HTTPHeader->new); 
-    } '... lives with the right type, even after coercion';
+    ok ! exception {
+       $engine->header(HTTPHeader->new);
+    }, '... lives with the right type, even after coercion';
 }
 
 {
@@ -109,11 +106,29 @@ BEGIN {
     ok(!defined($engine->header->array), '... no array value set');
 }
 
-dies_ok {
+ok exception {
     Engine->new(header => 'Foo');
-} '... dies correctly with bad params';
+}, '... dies correctly with bad params';
 
-dies_ok {
+ok exception {
     Engine->new(header => \(my $var));
-} '... dies correctly with bad params';
+}, '... dies correctly with bad params';
+
+{
+    my $tc = Moose::Util::TypeConstraints::find_type_constraint('HTTPHeader');
+    isa_ok($tc, 'Moose::Meta::TypeConstraint', 'HTTPHeader TC');
+
+    my $from_aref = $tc->assert_coerce([ 1, 2, 3 ]);
+    isa_ok($from_aref, 'HTTPHeader', 'assert_coerce from aref to HTTPHeader');
+    is_deeply($from_aref->array, [ 1, 2, 3 ], '...and has the right guts');
+
+    my $from_href = $tc->assert_coerce({ a => 1 });
+    isa_ok($from_href, 'HTTPHeader', 'assert_coerce from href to HTTPHeader');
+    is_deeply($from_href->hash, { a => 1 }, '...and has the right guts');
+
+    like exception { $tc->assert_coerce('total garbage') },
+      qr/Validation failed for .HTTPHeader./,
+      "assert_coerce throws if result is not acceptable";
+}
 
+done_testing;