From: John Napiorkowski Date: Mon, 25 Aug 2008 22:46:00 +0000 (+0000) Subject: overloading for union constraints, more tests, some code cleanup X-Git-Tag: 0.06~4^2~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cf1a8bfa50cb6cab796582ddae0a5b05dfcd8759;hp=a706b0f2457b28ca59151f4e91aa18267d0948d9;p=gitmo%2FMooseX-Types.git overloading for union constraints, more tests, some code cleanup --- diff --git a/lib/MooseX/Types/TypeDecorator.pm b/lib/MooseX/Types/TypeDecorator.pm index 5472646..b73ccf3 100644 --- a/lib/MooseX/Types/TypeDecorator.pm +++ b/lib/MooseX/Types/TypeDecorator.pm @@ -3,10 +3,17 @@ package MooseX::Types::TypeDecorator; use strict; use warnings; +use Moose::Util::TypeConstraints; use overload( '""' => sub { shift->type_constraint->name; }, + '|' => sub { + my @names = grep {$_} map {"$_"} @_; + ## Don't know why I can't use the array version of this... + my $names = join('|', @names); + Moose::Util::TypeConstraints::create_type_constraint_union($names); + }, ); =head1 NAME diff --git a/t/13_typedecorator.t b/t/13_typedecorator.t index 684ffd1..585dce2 100644 --- a/t/13_typedecorator.t +++ b/t/13_typedecorator.t @@ -2,7 +2,7 @@ use warnings; use strict; -use Test::More tests => 26; +use Test::More tests => 29; use Test::Exception; use FindBin; use lib "$FindBin::Bin/lib"; @@ -15,13 +15,14 @@ use lib "$FindBin::Bin/lib"; Int ); use DecoratorLibrary qw( - MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 + MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef ); has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1); has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1); has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1); has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]); + has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef); } ## Make sure we have a 'create object sanity check' @@ -112,4 +113,16 @@ is_deeply $type->arrayrefint03, [qw(11 12 13)], throws_ok sub { $type->arrayrefint03([qw(a b c)]) -}, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings'; \ No newline at end of file +}, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings'; + +# TEST StrOrArrayRef + +ok $type->StrOrArrayRef('string') + => 'String part of union is good'; + +ok $type->StrOrArrayRef([1,2,3]) + => 'arrayref part of union is good'; + +throws_ok sub { + $type->StrOrArrayRef({a=>111}); +}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref'; \ No newline at end of file diff --git a/t/lib/DecoratorLibrary.pm b/t/lib/DecoratorLibrary.pm index cac870a..c99c189 100644 --- a/t/lib/DecoratorLibrary.pm +++ b/t/lib/DecoratorLibrary.pm @@ -11,6 +11,7 @@ use MooseX::Types MyArrayRefInt02 MyHashRefOfInts MyHashRefOfStr + StrOrArrayRef )]; subtype MyArrayRefBase, @@ -48,5 +49,7 @@ coerce MyArrayRefInt02, ### Can't do HashRef[ArrayRef] here, need to force precidence I guess??? from HashRef([ArrayRef]), via {[ sort map { @$_ } values(%$_)] }; - + +subtype StrOrArrayRef, + from Str|ArrayRef; 1;