fix union with string types 0.14
Rafael Kitover [Sat, 27 Jun 2009 01:00:30 +0000 (18:00 -0700)]
Changes
lib/MooseX/Types.pm
lib/MooseX/Types/TypeDecorator.pm
t/20_union_with_string_type.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 368fdef..35905df 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for MooseX-Types
 
+0.14    Fri Jun 26 17:52:20 PDT 2009
+        - Fix union with string type
+
 0.13    Tue Jun 23 05:46:33 PDT 2009
         - Add Test::Moose to build_requires for Fedora
 
index 7c9d129..a19d709 100644 (file)
@@ -20,7 +20,7 @@ use Scalar::Util                      'reftype';
 use namespace::clean -except => [qw( meta )];
 
 use 5.008;
-our $VERSION = '0.13';
+our $VERSION = '0.14';
 my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
 
 =head1 SYNOPSIS
index 99dc926..12c7f48 100644 (file)
@@ -23,8 +23,12 @@ use overload(
         ## It's kind of ugly that we need to know about Union Types, but this
         ## is needed for syntax compatibility.  Maybe someday we'll all just do
         ## Or[Str,Str,Int]
-        
-        my @tc = grep {blessed $_} @_;
+
+       my @tc = map {
+           blessed $_ ? $_ :
+             Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)
+       } @_;
+
         my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
         return Moose::Util::TypeConstraints::register_type_constraint($union);
     },
diff --git a/t/20_union_with_string_type.t b/t/20_union_with_string_type.t
new file mode 100644 (file)
index 0000000..2d35deb
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+my $exception;
+{
+    package TypeLib;
+
+    use MooseX::Types -declare => [qw( MyUnionType MyStr )];
+    use MooseX::Types::Moose qw(Str Item);
+
+    subtype MyUnionType, as Str|'Int';
+    subtype MyStr, as Str;
+
+    eval { coerce MyStr, from Item, via {"$_"} };
+    $exception = $@;
+}
+
+ok !$@, 'types are not mutated by union with a string type';