more refactoring to the Structured Role, put together some tests to show the optional...
John Napiorkowski [Wed, 20 Aug 2008 22:36:24 +0000 (22:36 +0000)]
lib/MooseX/Meta/TypeConstraint/Role/Structured.pm
lib/MooseX/Meta/TypeConstraint/Structured/Named.pm
lib/MooseX/Meta/TypeConstraint/Structured/Positional.pm
t/constraints.t

index 7d7c097..00e7722 100644 (file)
@@ -1,6 +1,8 @@
 package MooseX::Meta::TypeConstraint::Role::Structured;
 
 use Moose::Role;
+use Moose::Util::TypeConstraints;
+requires qw(_normalize_args signature_equals);
 
 =head1 NAME
 
@@ -18,6 +20,18 @@ our $VERSION = '0.01';
 
 STUB - TBD
 
+=head1 TYPES
+
+The following types are defined in this class.
+
+=head2 Moose::Meta::TypeConstraint
+
+Used to make sure we can properly validate incoming signatures.
+
+=cut
+
+class_type 'Moose::Meta::TypeConstraint';
+
 =head1 ATTRIBUTES
 
 This class defines the following attributes.
@@ -56,28 +70,37 @@ This class defines the following methods.
 
 Get arguments into a known state or die trying.  Ideally we try to make this
 into a HashRef so we can match it up with the L</signature> HashRef.
-
-=cut
-
     
 =head2 constraint
 
 The constraint is basically validating the L</signature> against the incoming
 
-=cut
-
 =head2 equals
 
 modifier to make sure equals descends into the L</signature>
 
 =cut
 
+around 'equals' => sub {
+    my ($equals, $self, $compared_type_constraint) = @_;
+    
+    ## Make sure we are comparing typeconstraints of the same base class
+    return unless $compared_type_constraint->isa(__PACKAGE__);
+    
+    ## Make sure the base equals is also good
+    return unless $self->$equals($compared_type_constraint);
+    
+    ## Make sure the signatures match
+    return unless $self->signature_equals($compared_type_constraint);
+   
+    ## If we get this far, the two are equal
+    return 1;
+};
+
 =head2 signature_equals
 
 Check that the signature equals another signature.
 
-=cut
-
 =head1 AUTHOR
 
 John James Napiorkowski <jjnapiork@cpan.org>
index 9934236..8c0b1d2 100644 (file)
@@ -2,7 +2,6 @@ package MooseX::Meta::TypeConstraint::Structured::Named;
 
 use Moose;
 use Moose::Meta::TypeConstraint ();
-use Moose::Util::TypeConstraints;
 
 extends 'Moose::Meta::TypeConstraint';
 with 'MooseX::Meta::TypeConstraint::Role::Structured';
@@ -11,14 +10,6 @@ with 'MooseX::Meta::TypeConstraint::Role::Structured';
 
 MooseX::Meta::TypeConstraint::Structured::Named - Structured Type Constraints
 
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
-
 =head1 DESCRIPTION
 
 Structured type constraints let you assign an internal pattern of type
@@ -35,18 +26,6 @@ against this signature pattern.
 Named structured Constraints expect the internal constraints to be in keys or
 fields similar to what we expect in a HashRef.
 
-=head1 TYPES
-
-The following types are defined in this class.
-
-=head2 Moose::Meta::TypeConstraint
-
-Used to make sure we can properly validate incoming signatures.
-
-=cut
-
-class_type 'Moose::Meta::TypeConstraint';
-
 =head1 ATTRIBUTES
 
 This class defines the following attributes.
@@ -155,27 +134,7 @@ sub signature_equals {
     return 1;
 }
 
-=head2 equals
-
-modifier to make sure equals descends into the L</signature>
-
-=cut
 
-around 'equals' => sub {
-    my ($equals, $self, $compared_type_constraint) = @_;
-    
-    ## Make sure we are comparing typeconstraints of the same base class
-    return unless $compared_type_constraint->isa(__PACKAGE__);
-    
-    ## Make sure the base equals is also good
-    return unless $self->$equals($compared_type_constraint);
-    
-    ## Make sure the signatures match
-    return unless $self->signature_equals($compared_type_constraint);
-   
-    ## If we get this far, the two are equal
-    return 1;
-};
 
 =head1 AUTHOR
 
index 634f0fd..83d16af 100644 (file)
@@ -2,7 +2,6 @@ package MooseX::Meta::TypeConstraint::Structured::Positional;
 
 use Moose;
 use Moose::Meta::TypeConstraint ();
-use Moose::Util::TypeConstraints;
 
 extends 'Moose::Meta::TypeConstraint';
 with 'MooseX::Meta::TypeConstraint::Role::Structured';
@@ -11,14 +10,6 @@ with 'MooseX::Meta::TypeConstraint::Role::Structured';
 
 MooseX::Meta::TypeConstraint::Structured::Positional - Structured Type Constraints
 
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
-
 =head1 DESCRIPTION
 
 Structured type constraints let you assign an internal pattern of type
@@ -35,18 +26,6 @@ against this signature pattern.
 Positionally structured Constraints expect the internal constraints to be in
 'positioned' or ArrayRef style order.
 
-=head1 TYPES
-
-The following types are defined in this class.
-
-=head2 Moose::Meta::TypeConstraint
-
-Used to make sure we can properly validate incoming signatures.
-
-=cut
-
-class_type 'Moose::Meta::TypeConstraint';
-
 =head1 ATTRIBUTES
 
 This class defines the following attributes.
@@ -153,28 +132,6 @@ sub signature_equals {
     return 1;
 }
 
-=head2 equals
-
-modifier to make sure equals descends into the L</signature>
-
-=cut
-
-around 'equals' => sub {
-    my ($equals, $self, $compared_type_constraint) = @_;
-    
-    ## Make sure we are comparing typeconstraints of the same base class
-    return unless $compared_type_constraint->isa(__PACKAGE__);
-    
-    ## Make sure the base equals is also good
-    return unless $self->$equals($compared_type_constraint);
-    
-    ## Make sure the signatures match
-    return unless $self->signature_equals($compared_type_constraint);
-   
-    ## If we get this far, the two are equal
-    return 1;
-};
-
 =head1 AUTHOR
 
 John James Napiorkowski <jjnapiork@cpan.org>
index 95c9f18..47287fc 100644 (file)
@@ -1,7 +1,7 @@
 BEGIN {
        use strict;
        use warnings;
-       use Test::More tests=>25;
+       use Test::More tests=>30;
        use Test::Exception;
 }
 
@@ -29,6 +29,9 @@ BEGIN {
             signature => [map {
                                _normalize_type_constraint($_);
                        } @args],
+            optional_signature => [map {
+                               _normalize_type_constraint($_);
+                       } @optional],
         );
     }
 
@@ -44,6 +47,9 @@ BEGIN {
             signature => {map {
                                $_ => _normalize_type_constraint($args{$_});
                        } keys %args},
+            optional_signature => {map {
+                               $_ => _normalize_type_constraint($optional{$_});
+                       } keys %optional},
         );
     }
 
@@ -62,6 +68,7 @@ BEGIN {
        has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']);
        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']) );
 }
 
 ## Instantiate a new test object
@@ -71,7 +78,7 @@ ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
  
 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
  => 'Created correct object type.';
+
 ## Test Tuple type constraint
 
 lives_ok sub {
@@ -177,5 +184,24 @@ throws_ok sub {
 }, qr/Validation failed for 'Int'/
  => 'Threw error on bad constraint';
 
+## Test optional_tuple
 
+lives_ok sub {
+    $record->optional_tuple([1,2,3]);
+} => 'Set tuple attribute with optional bits';
+
+is_deeply $record->optional_tuple, [1,2,3]
+ => 'correct values set';
+lives_ok sub {
+    $record->optional_tuple([4,5]);
+} => 'Set tuple attribute withOUT optional bits';
+
+is_deeply $record->optional_tuple, [4,5]
+ => 'correct values set again';
+throws_ok sub {
+    $record->optional_tuple([1,2,'bad']);   
+}, qr/Validation failed for 'Int'/
+ => 'Properly failed for bad value in optional bit';