properly supporting a where clause in the suger example and proof you can customize...
[gitmo/MooseX-Dependent.git] / t / 02-depending.t
index 245e246..72d799b 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests=>22; {
+use Test::More tests=>23; {
     
     use strict;
     use warnings;
@@ -13,9 +13,13 @@ use Test::More tests=>22; {
     )];
        
        ## sugar for alternative syntax: depending {} TC,TC
-       sub depending(&$$) {
-               my ($coderef, $dependent_tc, $constraining_tc) = @_;
-               return Depending[$dependent_tc,$coderef,$constraining_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
@@ -44,7 +48,7 @@ use Test::More tests=>22; {
         Int,
         sub {
             my ($dependent_int, $constraining_arrayref) = @_;
-            (grep { $_ == $dependent_int} @$constraining_arrayref) ? 0:1
+            (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
         },
         ArrayRef[Int],
       ];
@@ -57,18 +61,23 @@ use Test::More tests=>22; {
     ok UniqueInt->check([2,[3..6]]), 'PASS unique in set';
     ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
        
-       ## Same as above, with suger
+       ## Same as above, with sugar
     subtype UniqueInt2,
          as depending {
             my ($dependent_int, $constraining_arrayref) = @_;
-            (grep { $_ == $dependent_int} @$constraining_arrayref) ? 0:1               
-         } Int, ArrayRef[Int];
+            (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]]), 'PASS unique in set';
-    ok UniqueInt2->check([3,[100..110]]), 'PASS 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';        
 }