Add inlining for union types
Dave Rolsky [Sun, 10 Apr 2011 20:42:25 +0000 (15:42 -0500)]
lib/Moose/Meta/TypeConstraint/Union.pm
t/type_constraints/util_std_type_constraints.t

index 5a2a6f1..8b695e0 100644 (file)
@@ -7,6 +7,7 @@ use metaclass;
 
 use Moose::Meta::TypeCoercion::Union;
 
+use List::MoreUtils qw(all);
 use List::Util qw(first);
 
 use base 'Moose::Meta::TypeConstraint';
@@ -70,6 +71,21 @@ sub _actually_compile_type_constraint {
     };
 }
 
+sub has_inlined_type_constraint {
+    my $self = shift;
+
+    return all { $_->has_inlined_type_constraint }
+        @{ $self->type_constraints };
+}
+
+sub _inline_check {
+    my $self = shift;
+    my $val  = shift;
+
+    return
+        join ' || ', map { '(' . $_->_inline_check($val) . ')' }
+        @{ $self->type_constraints };
+};
 
 sub equals {
     my ( $self, $type_or_name ) = @_;
index 4b4dc4b..162945b 100644 (file)
@@ -720,7 +720,6 @@ for my $name ( sort keys %tests ) {
                 ( bless {}, 'DuckLike' ),
             ],
             reject => [
-                'Thing',
                 $ZERO,
                 $ONE,
                 $INT,
@@ -757,7 +756,6 @@ for my $name ( sort keys %tests ) {
         'Enumerated', {
             accept => \@allowed,
             reject => [
-                'Thing',
                 $ZERO,
                 $ONE,
                 $INT,
@@ -786,6 +784,46 @@ for my $name ( sort keys %tests ) {
 }
 
 {
+    my $union = Moose::Meta::TypeConstraint::Union->new(
+        type_constraints => [
+            find_type_constraint('Int'),
+            find_type_constraint('Object'),
+        ],
+    );
+
+    test_constraint(
+        $union, {
+            accept => [
+                $ZERO,
+                $ONE,
+                $INT,
+                $NEG_INT,
+                $FH_OBJECT,
+                $REGEX,
+                $REGEX_OBJ,
+                $OBJECT,
+            ],
+            reject => [
+                $NUM,
+                $NEG_NUM,
+                $EMPTY_STRING,
+                $STRING,
+                $NUM_IN_STRING,
+                $SCALAR_REF,
+                $SCALAR_REF_REF,
+                $ARRAY_REF,
+                $HASH_REF,
+                $CODE_REF,
+                $GLOB,
+                $GLOB_REF,
+                $FH,
+                $UNDEF,
+            ],
+        }
+    );
+}
+
+{
     package DoesRole;
 
     use Moose;