Redid conversion to Test::Fatal
[gitmo/Moose.git] / lib / Moose / Cookbook / Basics / Recipe5.pod
index d81b38d..7a6f3ed 100644 (file)
@@ -3,14 +3,11 @@
 
 =begin testing-SETUP
 
-BEGIN {
-    eval 'use HTTP::Headers; use Params::Coerce; use URI;';
-    if ($@) {
-        diag 'HTTP::Headers, Params::Coerce & URI required for this test';
-        ok(1);
-        exit 0;
-    }
-}
+use Test::Requires {
+    'HTTP::Headers'  => '0',
+    'Params::Coerce' => '0',
+    'URI'            => '0',
+};
 
 =end testing-SETUP
 
@@ -28,17 +25,17 @@ Moose::Cookbook::Basics::Recipe5 - More subtypes, coercion in a B<Request> class
   use Params::Coerce ();
   use URI            ();
 
-  subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
+  subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers');
 
-  coerce 'My.HTTP::Headers'
+  coerce 'My::Types::HTTP::Headers'
       => from 'ArrayRef'
           => via { HTTP::Headers->new( @{$_} ) }
       => from 'HashRef'
           => via { HTTP::Headers->new( %{$_} ) };
 
-  subtype 'My.URI' => as class_type('HTTP::Headers');
+  subtype 'My::Types::URI' => as class_type('URI');
 
-  coerce 'My.URI'
+  coerce 'My::Types::URI'
       => from 'Object'
           => via { $_->isa('URI')
                    ? $_
@@ -50,13 +47,13 @@ Moose::Cookbook::Basics::Recipe5 - More subtypes, coercion in a B<Request> class
       => as 'Str'
       => where { /^HTTP\/[0-9]\.[0-9]$/ };
 
-  has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
-  has 'uri'  => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+  has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
+  has 'uri'  => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
   has 'method'   => ( is => 'rw', isa => 'Str' );
   has 'protocol' => ( is => 'rw', isa => 'Protocol' );
   has 'headers'  => (
       is      => 'rw',
-      isa     => 'My.HTTP::Headers',
+      isa     => 'My::Types::HTTP::Headers',
       coerce  => 1,
       default => sub { HTTP::Headers->new }
   );
@@ -74,7 +71,7 @@ set the C<coerce> attribute option to a true value.
 
 First, we create the subtype to which we will coerce the other types:
 
-  subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
+  subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers');
 
 We are creating a subtype rather than using C<HTTP::Headers> as a type
 directly. The reason we do this is coercions are global, and a
@@ -111,7 +108,7 @@ to accept those data structure instead of an B<HTTP::Headers>
 instance, and just do the right thing. This is exactly what coercion
 is for:
 
-  coerce 'My.HTTP::Headers'
+  coerce 'My::Types::HTTP::Headers'
       => from 'ArrayRef'
           => via { HTTP::Headers->new( @{$_} ) }
       => from 'HashRef'
@@ -127,7 +124,7 @@ we want a particular attribute to be coerced:
 
   has 'headers' => (
       is      => 'rw',
-      isa     => 'My.HTTP::Headers',
+      isa     => 'My::Types::HTTP::Headers',
       coerce  => 1,
       default => sub { HTTP::Headers->new }
   );
@@ -150,11 +147,11 @@ help implement coercions. In this case we use L<Params::Coerce>.
 Once again, we need to declare a class type for our non-Moose L<URI>
 class:
 
-  subtype 'My.URI' => as class_type('HTTP::Headers');
+  subtype 'My::Types::URI' => as class_type('URI');
 
 Then we define the coercion:
 
-  coerce 'My.URI'
+  coerce 'My::Types::URI'
       => from 'Object'
           => via { $_->isa('URI')
                    ? $_
@@ -177,8 +174,8 @@ string is assumed to be an C<http> URI.
 
 Finally, we need to make sure our attributes enable coercion.
 
-  has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
-  has 'uri'  => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+  has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
+  has 'uri'  => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
 
 Re-using the coercion lets us enforce a consistent API across multiple
 attributes.
@@ -214,7 +211,7 @@ Dave Rolsky E<lt>autarch@urth.orgE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
@@ -260,25 +257,43 @@ isa_ok( $r, 'Request' );
     is( $header4->content_type, 'application/pdf',
         '... got the right content type in the header' );
 
-    dies_ok {
-        $r->headers('Foo');
-    }
-    '... dies when it gets bad params';
+    isnt(
+        exception {
+            $r->headers('Foo');
+        },
+        undef,
+        '... dies when it gets bad params'
+    );
 }
 
 {
     is( $r->protocol, undef, '... got nothing by default' );
 
-    lives_ok {
-        $r->protocol('HTTP/1.0');
-    }
-    '... set the protocol correctly';
+    is(
+        exception {
+            $r->protocol('HTTP/1.0');
+        },
+        undef,
+        '... set the protocol correctly'
+    );
+
     is( $r->protocol, 'HTTP/1.0', '... got nothing by default' );
 
-    dies_ok {
-        $r->protocol('http/1.0');
-    }
-    '... the protocol died with bar params correctly';
+    isnt(
+        exception {
+            $r->protocol('http/1.0');
+        },
+        undef,
+        '... the protocol died with bar params correctly'
+    );
+}
+
+{
+    $r->base('http://localhost/');
+    isa_ok( $r->base, 'URI' );
+
+    $r->uri('http://localhost/');
+    isa_ok( $r->uri, 'URI' );
 }
 
 =end testing