fix typo in docs: . instead of ;
[gitmo/Moose.git] / t / 040_type_constraints / 016_subtyping_parameterized_types.t
index bd01271..acdbd22 100644 (file)
@@ -3,16 +3,16 @@
 use strict;
 use warnings;
 
-use Test::More tests => 32;
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
 
 BEGIN {
     use_ok("Moose::Util::TypeConstraints");
 }
 
-lives_ok {
+is( exception {
     subtype 'MySpecialHash' => as 'HashRef[Int]';
-} '... created the subtype special okay';
+}, undef, '... created the subtype special okay' );
 
 {
     my $t = find_type_constraint('MySpecialHash');
@@ -37,14 +37,14 @@ lives_ok {
     ok( !$t->is_subtype_of("ThisTypeDoesNotExist"), "not a subtype of a non existant type" );
 }
 
-lives_ok {
-    subtype 'MySpecialHashExtended' 
+is( exception {
+    subtype 'MySpecialHashExtended'
         => as 'HashRef[Int]'
         => where {
             # all values are less then 10
             (scalar grep { $_ < 10 } values %{$_}) ? 1 : undef
         };
-} '... created the subtype special okay';
+}, undef, '... created the subtype special okay' );
 
 {
     my $t = find_type_constraint('MySpecialHashExtended');
@@ -63,11 +63,11 @@ lives_ok {
     ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly');
 }
 
-lives_ok {
+is( exception {
     subtype 'MyNonSpecialHash'
         => as "HashRef"
         => where { keys %$_ == 3 };
-};
+}, undef );
 
 {
     my $t = find_type_constraint('MyNonSpecialHash');
@@ -88,3 +88,44 @@ lives_ok {
     ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" );
     ok( !$t->check({ one => 1 }), "failed" );
 }
+
+{
+    ## Because to throw errors in M:M:Parameterizable needs Moose loaded in
+    ## order to throw errors.  In theory the use Moose belongs to that class
+    ## but when I put it there causes all sorts or trouble.  In theory this is
+    ## never a real problem since you are likely to use Moose somewhere when you
+    ## are creating type constraints.
+    use Moose ();
+
+    my $MyArrayRefInt =  subtype 'MyArrayRefInt',
+        as 'ArrayRef[Int]';
+
+    my $BiggerInt = subtype 'BiggerInt',
+        as 'Int',
+        where {$_>10};
+
+    my $SubOfMyArrayRef = subtype 'SubOfMyArrayRef',
+        as 'MyArrayRefInt[BiggerInt]';
+
+    ok $MyArrayRefInt->check([1,2,3]), '[1,2,3] is okay';
+    ok ! $MyArrayRefInt->check(["a","b"]), '["a","b"] is not';
+    ok $BiggerInt->check(100), '100 is  big enough';
+    ok ! $BiggerInt->check(5), '5 is  big enough';
+    ok $SubOfMyArrayRef->check([15,20,25]), '[15,20,25] is a bunch of big ints';
+    ok ! $SubOfMyArrayRef->check([15,5,25]), '[15,5,25] is NOT a bunch of big ints';
+
+    like( exception {
+        my $SubOfMyArrayRef = subtype 'SubSubOfMyArrayRef',
+            as 'SubOfMyArrayRef[Str]';
+    }, qr/Str is not a subtype of BiggerInt/, 'Failed to parameterize with a bad type parameter' );
+}
+
+{
+    my $RefToInt = subtype as 'ScalarRef[Int]';
+
+    ok $RefToInt->check(\1), '\1 is okay';
+    ok !$RefToInt->check(1), '1 is not';
+    ok !$RefToInt->check(\"foo"), '\"foo" is not';
+}
+
+done_testing;