first shot at some regex to parse the attribute isa option
[gitmo/MooseX-Types-Structured.git] / t / constraints.t
index 47287fc..3a88cf7 100644 (file)
@@ -1,7 +1,7 @@
 BEGIN {
        use strict;
        use warnings;
-       use Test::More tests=>30;
+       use Test::More tests=>42;
        use Test::Exception;
 }
 
@@ -38,7 +38,7 @@ BEGIN {
     sub Dict {
         my ($args, $optional) = @_;
         my %args = @$args;
-        my %optional = ref $optional eq 'HASH' ? @$optional : ();
+        my %optional = ref $optional eq 'ARRAY' ? @$optional : ();
         
         return MooseX::Meta::TypeConstraint::Structured::Named->new(
             name => 'Dict',
@@ -55,7 +55,7 @@ BEGIN {
 
        sub _normalize_type_constraint {
                my $tc = shift @_;
-               if($tc && blessed $tc && $tc->isa('Moose::Meta::TypeConstraint')) {
+               if(defined $tc && blessed $tc && $tc->isa('Moose::Meta::TypeConstraint')) {
                        return $tc;
                } elsif($tc) {
                        return Moose::Util::TypeConstraints::find_or_parse_type_constraint($tc);
@@ -69,6 +69,32 @@ BEGIN {
        has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']);
        has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]);
     has 'optional_tuple' => (is=>'rw', isa=>Tuple(['Int', 'Int'],['Int']) );
+    has 'optional_dict' => (is=>'rw', isa=>Dict([key1=>'Int'],[key2=>'Int']) );
+    
+    has 'crazy' => (
+        is=>'rw',
+        isa=>Tuple(
+            ## First ArrayRef Arg is the required type constraints for the top
+            ## level Tuple.
+            [
+                'Int',
+                'MyString',
+                ## The third required element is a Dict type constraint, which
+                ## itself has two required keys and a third optional key.
+                Dict([name=>'Str',age=>'Int'],[visits=>'Int'])
+            ],
+            ## Second ArrayRef Arg defines the optional constraints for the top
+            ## level Tuple.
+            [
+                'Int',
+                ## This Tuple has one required type constraint and two optional.
+                Tuple(
+                      ['Int'],
+                      ['Int','HashRef'],
+                ),
+            ],        
+        )
+    );
 }
 
 ## Instantiate a new test object
@@ -78,7 +104,37 @@ ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
  
 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
  => 'Created correct object type.';
+## Test crazy
+
+lives_ok sub {
+    $record->crazy([1,'hello.abc.world', {name=>'John', age=>39}]);
+} => 'Set crazy attribute with no optionals used';
+
+is_deeply $record->crazy, [1, 'hello.abc.world', {name=>'John', age=>39}]
+ => 'correct values for crazy attributes no optionals';
+lives_ok sub {
+    $record->crazy([1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]);
+} => 'Set crazy attribute with all optionals used';
 
+is_deeply $record->crazy, [1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]
+ => 'correct values for crazy attributes all optionals';
+
+lives_ok sub {
+    $record->crazy([1,'hello.abc.world', {name=>'John', age=>39},10, [1,2]]);
+} => 'Set crazy attribute with some optionals used';
+
+throws_ok sub {
+    $record->crazy([1,'hello', 'test.xxx.test']);    
+}, qr/Validation failed for 'MyString'/
+ => 'Properly failed for bad value in crazy attribute 01';
+
+throws_ok sub {
+    $record->crazy([1,'hello.abc.world', {notname=>'John', notage=>39}]);    
+}, qr/Validation failed for 'Str'/
+ => 'Properly failed for bad value in crazy attribute 02';
 ## Test Tuple type constraint
 
 lives_ok sub {
@@ -205,3 +261,24 @@ throws_ok sub {
 }, qr/Validation failed for 'Int'/
  => 'Properly failed for bad value in optional bit';
 
+# Test optional_dict
+
+lives_ok sub {
+    $record->optional_dict({key1=>1,key2=>2});
+} => 'Set tuple attribute with optional bits';
+
+is_deeply $record->optional_dict, {key1=>1,key2=>2}
+ => 'correct values set';
+lives_ok sub {
+    $record->optional_dict({key1=>3});
+} => 'Set tuple attribute withOUT optional bits';
+
+is_deeply $record->optional_dict, {key1=>3}
+ => 'correct values set again';
+throws_ok sub {
+    $record->optional_dict({key1=>1,key2=>'bad'});   
+}, qr/Validation failed for 'Int'/
+ => 'Properly failed for bad value in optional bit';
\ No newline at end of file