From: Kent Fredric Date: Sun, 19 Jun 2011 01:07:48 +0000 (+1200) Subject: Improve the docs for this feature, extend the tests X-Git-Tag: 2.0103~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=74dccf76caa27e0b25d04a7f9c8bebdabcce9df8;p=gitmo%2FMoose.git Improve the docs for this feature, extend the tests --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index b3e5b60..a906081 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -764,6 +764,8 @@ __END__ enum 'RGBColors', [qw(red green blue)]; + union 'StringOrArray', [qw( String Array )]; + no Moose::Util::TypeConstraints; =head1 DESCRIPTION @@ -1020,6 +1022,33 @@ can then be used in an attribute definition like so: isa => enum([qw[ ascending descending ]]), ); +=item B + +This will create a basic subtype where any of the provided constraints +may match in order to satisfy this constraint. + +=item B + +If passed an ARRAY reference as the only parameter instead of the +C<$name>, C<\@constraints> pair, this will create an unnamed union. +This can then be used in an attribute definition like so: + + has 'items' => ( + is => 'ro', + isa => union([qw[ Str ArrayRef ]]), + ); + +This is similar to the existing string union: + + isa => 'Str|ArrayRef' + +except that it supports anonymous elements as child constraints: + + has 'color' => ( + isa => 'ro', + isa => union([ 'Int', enum([qw[ red green blue ]]) ]), + ); + =item B This is just sugar for the type constraint construction syntax. diff --git a/t/type_constraints/util_std_type_constraints.t b/t/type_constraints/util_std_type_constraints.t index 98bb2be..31e22b4 100644 --- a/t/type_constraints/util_std_type_constraints.t +++ b/t/type_constraints/util_std_type_constraints.t @@ -1041,6 +1041,49 @@ foreach my $type_name (qw(Str Num Int ClassName RoleName)) ); } +{ + note 'Combined Union Test'; + my $union = union( [ 'Int', enum( [qw[ red green blue ]] ) ] ); + + test_constraint( + $union, { + accept => [ + $ZERO, + $ONE, + $INT, + $NEG_INT, + 'red', + 'green', + 'blue', + ], + reject => [ + 'yellow', + 'pink', + $FH_OBJECT, + $REGEX, + $REGEX_OBJ, + $FAKE_REGEX, + $OBJECT, + $NUM, + $NEG_NUM, + $EMPTY_STRING, + $STRING, + $NUM_IN_STRING, + $INT_WITH_NL1, + $INT_WITH_NL2, + $SCALAR_REF, + $SCALAR_REF_REF, + $ARRAY_REF, + $HASH_REF, + $CODE_REF, + $GLOB, + $GLOB_REF, + $FH, + $UNDEF, + ], + } + ); +} {