incremented version and updated changelog, fixed bug that created extra coercions...
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
index 684ffd1..89cc3a2 100644 (file)
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 26;
+use Test::More tests => 39;
 use Test::Exception;
 use FindBin;
 use lib "$FindBin::Bin/lib";
@@ -12,16 +12,24 @@ use lib "$FindBin::Bin/lib";
     
     use Moose;
     use MooseX::Types::Moose qw(
-        Int
+        Int Str ArrayRef HashRef
     );
     use DecoratorLibrary qw(
-        MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02
+        MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
+        AtLeastOneInt 
     );
     
     has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
     has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1);
     has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1);
     has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]);
+    has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef);
+    has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt);
+    has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);
+    #has 'deep' => (is=>'rw', isa=>ArrayRef([ArrayRef([HashRef([Int])])]));
+    
+    has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] );
+    
 }
 
 ## Make sure we have a 'create object sanity check'
@@ -34,10 +42,10 @@ isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
 
 ## test arrayrefbase normal and coercion
 
-ok $type->arrayrefbase([qw(a b c)])
- => 'Assigned arrayrefbase qw(a b c)';
+ok $type->arrayrefbase([qw(a b c d e)])
+ => 'Assigned arrayrefbase qw(a b c d e)';
  
-is_deeply $type->arrayrefbase, [qw(a b c)],
+is_deeply $type->arrayrefbase, [qw(a b c d e)],
  => 'Assignment is correct';
 
 ok $type->arrayrefbase('d,e,f')
@@ -112,4 +120,56 @@ is_deeply $type->arrayrefint03, [qw(11 12 13)],
  
 throws_ok sub {
     $type->arrayrefint03([qw(a b c)])
-}, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
\ No newline at end of file
+}, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
+
+# TEST StrOrArrayRef
+
+ok $type->StrOrArrayRef('string')
+ => 'String part of union is good';
+
+ok $type->StrOrArrayRef([1,2,3])
+ => 'arrayref part of union is good';
+throws_ok sub {
+    $type->StrOrArrayRef({a=>111});
+}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
+
+# Test AtLeastOneInt
+
+ok $type->AtLeastOneInt([1,2]),
+ => 'Good assignment';
+
+is_deeply $type->AtLeastOneInt, [1,2]
+ => "Got expected values.";
+throws_ok sub {
+    $type->AtLeastOneInt([]);
+}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
+
+throws_ok sub {
+    $type->AtLeastOneInt(['a','b']);
+}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
+
+## Test pipeoverloading
+
+ok $type->pipeoverloading(1)
+ => 'Integer for union test accepted';
+ok $type->pipeoverloading('a')
+ => 'String for union test accepted';
+
+throws_ok sub {
+    $type->pipeoverloading({a=>1,b=>2});
+}, qr/Validation failed for 'Int | Str'/ => 'Union test corrected fails a HashRef';
+
+## test deep
+
+ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
+ => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
+
+is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
+ => 'Assignment is correct';
+throws_ok sub {
+    $type->deep({a=>1,b=>2});
+}, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
\ No newline at end of file