From: Dave Rolsky Date: Sun, 10 Apr 2011 20:42:25 +0000 (-0500) Subject: Add inlining for union types X-Git-Tag: 2.0100~79 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0953281678ed36026ecb37f8e03e1cbd2144b9cf;p=gitmo%2FMoose.git Add inlining for union types --- diff --git a/lib/Moose/Meta/TypeConstraint/Union.pm b/lib/Moose/Meta/TypeConstraint/Union.pm index 5a2a6f1..8b695e0 100644 --- a/lib/Moose/Meta/TypeConstraint/Union.pm +++ b/lib/Moose/Meta/TypeConstraint/Union.pm @@ -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 ) = @_; diff --git a/t/type_constraints/util_std_type_constraints.t b/t/type_constraints/util_std_type_constraints.t index 4b4dc4b..162945b 100644 --- a/t/type_constraints/util_std_type_constraints.t +++ b/t/type_constraints/util_std_type_constraints.t @@ -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;