Redid conversion to Test::Fatal
[gitmo/Moose.git] / t / 040_type_constraints / 021_maybe_type_constraint.t
index e97986c..7ea9340 100644 (file)
@@ -3,8 +3,8 @@
 use strict;
 use warnings;
 
-use Test::More tests => 31;
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
 
 use Moose::Util::TypeConstraints;
 
@@ -26,27 +26,53 @@ ok(!$type->check('Hello World'), '... checked type correctly (fail)');
 ok(!$type->check([]), '... checked type correctly (fail)');
 
 {
+    package Bar;
+    use Moose;
+
     package Foo;
     use Moose;
-    
-    has 'bar' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);    
+    use Moose::Util::TypeConstraints;
+
+    has 'arr' => (is => 'rw', isa => 'Maybe[ArrayRef]', required => 1);
+    has 'bar' => (is => 'rw', isa => class_type('Bar'));
+    has 'maybe_bar' => (is => 'rw', isa => maybe_type(class_type('Bar')));
 }
 
-lives_ok {
-    Foo->new(bar => []);
-} '... it worked!';
+is( exception {
+    Foo->new(arr => [], bar => Bar->new);
+}, undef, '... Bar->new isa Bar' );
+
+isnt( exception {
+    Foo->new(arr => [], bar => undef);
+}, undef, '... undef isnta Bar' );
+
+is( exception {
+    Foo->new(arr => [], maybe_bar => Bar->new);
+}, undef, '... Bar->new isa maybe(Bar)' );
+
+is( exception {
+    Foo->new(arr => [], maybe_bar => undef);
+}, undef, '... undef isa maybe(Bar)' );
+
+isnt( exception {
+    Foo->new(arr => [], maybe_bar => 1);
+}, undef, '... 1 isnta maybe(Bar)' );
+
+is( exception {
+    Foo->new(arr => []);
+}, undef, '... it worked!' );
 
-lives_ok {
-    Foo->new(bar => undef);
-} '... it worked!';
+is( exception {
+    Foo->new(arr => undef);
+}, undef, '... it worked!' );
 
-dies_ok {
-    Foo->new(bar => 100);
-} '... failed the type check';
+isnt( exception {
+    Foo->new(arr => 100);
+}, undef, '... failed the type check' );
 
-dies_ok {
-    Foo->new(bar => 'hello world');
-} '... failed the type check';
+isnt( exception {
+    Foo->new(arr => 'hello world');
+}, undef, '... failed the type check' );
 
 
 {
@@ -54,10 +80,10 @@ dies_ok {
     use Moose;
 
     has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
-    has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]');        
-    has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]');  
-    has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]');        
-    has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]');  
+    has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]');
+    has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]');
+    has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]');
+    has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]');
 }
 
 ok my $obj = Test::MooseX::Types::Maybe->new
@@ -67,13 +93,13 @@ ok my $obj = Test::MooseX::Types::Maybe->new
 
 ok my $Maybe_Int  = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
  => 'made TC Maybe[Int]';
+
 ok $Maybe_Int->check(1)
  => 'passed (1)';
+
 ok $obj->Maybe_Int(1)
  => 'assigned (1)';
+
 ok $Maybe_Int->check()
  => 'passed ()';
 
@@ -85,23 +111,21 @@ ok $Maybe_Int->check(0)
 
 ok defined $obj->Maybe_Int(0)
  => 'assigned (0)';
+
 ok $Maybe_Int->check(undef)
  => 'passed (undef)';
+
 ok sub {$obj->Maybe_Int(undef); 1}->()
  => 'assigned (undef)';
+
 ok !$Maybe_Int->check("")
  => 'failed ("")';
-throws_ok sub { $obj->Maybe_Int("") }, 
- qr/Attribute \(Maybe_Int\) does not pass the type constraint/
- => 'failed assigned ("")';
+
+like( exception { $obj->Maybe_Int("") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("")' );
 
 ok !$Maybe_Int->check("a")
  => 'failed ("a")';
 
-throws_ok sub { $obj->Maybe_Int("a") }, 
- qr/Attribute \(Maybe_Int\) does not pass the type constraint/
- => 'failed assigned ("a")';
\ No newline at end of file
+like( exception { $obj->Maybe_Int("a") }, qr/Attribute \(Maybe_Int\) does not pass the type constraint/, 'failed assigned ("a")' );
+
+done_testing;