the basic, basics in place
[gitmo/MooseX-Dependent.git] / t / 02-depending.t
index 0305d38..9831139 100644 (file)
@@ -1,23 +1,33 @@
-use Test::More tests=>15; {
+use Test::More tests=>29; {
     
     use strict;
     use warnings;
-    
-    use Test::Exception;
+
     use MooseX::Types::Dependent qw(Depending);
        use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
        use MooseX::Types -declare => [qw(
         IntGreaterThanInt
         UniqueInt
+               UniqueInt2
     )];
+       
+       ## sugar for alternative syntax: depending {} TC,TC
+       sub depending(&@) {
+               my ($coderef, $dependent_tc, $constraining_tc, @args) = @_;             
+               if(@args) {
+                       return (Depending[$dependent_tc,$coderef,$constraining_tc],@args);
+               } else {
+                       return Depending[$dependent_tc,$coderef,$constraining_tc];
+               }
+       }
     
     ## The dependent value must exceed the constraining value
     subtype IntGreaterThanInt,
       as Depending[
         Int,
         sub {
-                       my ($dependent_val, $constraining_val) = @_;
-                       return ($dependent_val > $constraining_val) ? 1:undef;
+            my ($dependent_val, $constraining_val) = @_;
+            return ($dependent_val > $constraining_val) ? 1:undef;
         },
         Int,
       ];
@@ -31,24 +41,65 @@ use Test::More tests=>15; {
        ok IntGreaterThanInt->check([12,1]), "Success, 12 is greater than1.";
        ok IntGreaterThanInt->check([0,-10]), "Success, 0 is greater than -10.";
     
-    ## The dependent value cannot exist in the constraining arrayref
+    ## The dependent value cannot exist in the constraining arrayref.  Also, it
+       ## (the dependent type) must exceed 2.
     subtype UniqueInt,
       as Depending[
         Int,
         sub {
             my ($dependent_int, $constraining_arrayref) = @_;
-            ## Yes, this is braindead way to check for uniques in an array
-            ## but this doesn't require additional dependencies.
-            (grep { $_ == $dependent_int} @$constraining_arrayref) ? 0:1
+            (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
         },
         ArrayRef[Int],
-      ];
-      
+      ],
+         where {
+               my ($dependent_val, $constraining_value) = @$_;
+               return $dependent_val > 2 ? 1:undef;
+         };
+         #message {"Custom Error: $_"};
+
     isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent';
     ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int';
     ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref';    
     ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set';
     ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set';
-    ok UniqueInt->check([2,[3..6]]), 'PASS unique in set';
-    ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';    
+    ok !UniqueInt->check([2,[3..6]]), 'FAIL dependent is too small';
+    ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
+    ok UniqueInt->check([4,[100..110]]), 'PASS unique in set'; 
+       
+       ## Basically as above, with sugar.
+    subtype UniqueInt2,
+         as depending {
+            my ($dependent_int, $constraining_arrayref) = @_;
+            (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1           
+         } Int, ArrayRef[Int],
+         where {
+               my ($dependent_val, $constraining_value) = @$_;
+               return $dependent_val > 2 ? 1:undef;
+         };
+
+    isa_ok UniqueInt2, 'MooseX::Meta::TypeConstraint::Dependent';
+    ok !UniqueInt2->check(['a',[1,2,3]]), '"a" not an Int';
+    ok !UniqueInt2->check([1,['b','c']]), '"b","c" not an arrayref';    
+    ok !UniqueInt2->check([1,[1,2,3]]), 'not unique in set';
+    ok !UniqueInt2->check([10,[1,10,15]]), 'not unique in set';
+    ok !UniqueInt2->check([2,[3..6]]), 'FAIL dependent is too small';
+    ok UniqueInt2->check([3,[100..110]]), 'PASS unique in set';
+    ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set';
+
+       ## Basic error messages.  TODO should be it's own test
+       like UniqueInt->validate(['a',[1,2,3]]), qr/failed for 'Int' failed with value a/,
+         "a is not an Int";
+       
+       like UniqueInt->validate([1,['b','c']]), qr/failed for 'ArrayRef\[Int\]'/,
+         "ArrayRef doesn't contain Ints";
+       
+       like UniqueInt->validate([1,[1,2,3]]), qr/failed with value \[ 1, \[ 1, 2, 3 \] \]/,
+         "Is not unique in the constraint";
+       
+    like UniqueInt->validate([10,[1,10,15]]), qr/failed with value \[ 10, \[ 1, 10, 15 \] \]/,
+         "Expected Error message for [10,[1,10,15]]";
+       
+    like UniqueInt->validate([2,[3..6]]), qr/failed with value \[ 2, \[ 3, 4, 5, 6 \] \]/,
+         "Expected Error message for [2,[3..6]]";
 }