[Moose-commits] r7479 - in Moose/trunk: . lib/Moose/Util t/040_type_constraints

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Mon Feb 2 22:52:21 GMT 2009


Author: autarch
Date: 2009-02-02 14:52:21 -0800 (Mon, 02 Feb 2009)
New Revision: 7479

Added:
   Moose/trunk/t/040_type_constraints/033_type_names.t
Modified:
   Moose/trunk/Changes
   Moose/trunk/lib/Moose/Util/TypeConstraints.pm
Log:
Do some hackish validation of type names in MUTC, though unfortunately
I can't find a clean way to put this into the metaclasses.

Also make sure to allow "." in type names.

Modified: Moose/trunk/Changes
===================================================================
--- Moose/trunk/Changes	2009-02-02 20:08:35 UTC (rev 7478)
+++ Moose/trunk/Changes	2009-02-02 22:52:21 UTC (rev 7479)
@@ -17,6 +17,13 @@
         cause very unhelpful errors when it tried to throw an error
         before Moose was loaded. (Dave Rolsky)
 
+    * Moose::Util::TypeConstraints
+      - You could declare a name with subtype such as "Foo!Bar" that
+        would be allowed, but if you used it in a parameterized type
+        such as "ArrayRef[Foo!Bar]" it wouldn't work. We now do some
+        vetting on names created via the sugar functions, so that they
+        can only contain alphanumerics, ":", and ".".
+
 0.65 Thu, January 22, 2008
     * Moose and Moose::Meta::Method::Overridden
       - If an overridden method called super(), and then the

Modified: Moose/trunk/lib/Moose/Util/TypeConstraints.pm
===================================================================
--- Moose/trunk/lib/Moose/Util/TypeConstraints.pm	2009-02-02 20:08:35 UTC (rev 7478)
+++ Moose/trunk/lib/Moose/Util/TypeConstraints.pm	2009-02-02 22:52:21 UTC (rev 7479)
@@ -381,6 +381,10 @@
                 . " and cannot be created again in "
                 . $pkg_defined_in )
             if defined $type;
+
+        $name =~ /^[\w:\.]+$/
+            or die qq{$name contains invalid characters for a type name.}
+            . qq{Names can contain alphanumeric character, ":", and "."\n};
     }
 
     my %opts = (
@@ -439,7 +443,7 @@
 
     use re "eval";
 
-    my $valid_chars = qr{[\w:]};
+    my $valid_chars = qr{[\w:\.]};
     my $type_atom   = qr{ $valid_chars+ };
 
     my $any;
@@ -788,6 +792,9 @@
 
 =head2 Type Constraint Naming
 
+Type name declared via this module can only contain alphanumeric
+characters, colons (:), and periods (.).
+
 Since the types created by this module are global, it is suggested
 that you namespace your types just as you would namespace your
 modules. So instead of creating a I<Color> type for your B<My::Graphics>

Added: Moose/trunk/t/040_type_constraints/033_type_names.t
===================================================================
--- Moose/trunk/t/040_type_constraints/033_type_names.t	                        (rev 0)
+++ Moose/trunk/t/040_type_constraints/033_type_names.t	2009-02-02 22:52:21 UTC (rev 7479)
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+
+use Test::More tests => 6;
+use Test::Exception;
+
+use Moose::Meta::TypeConstraint;
+use Moose::Util::TypeConstraints;
+
+
+TODO:
+{
+    local $TODO = 'type names are not validated in the TC metaclass';
+
+    throws_ok { Moose::Meta::TypeConstraint->new( name => 'Foo-Bar' ) }
+    qr/contains invalid characters/,
+        'Type names cannot contain a dash';
+}
+
+lives_ok { Moose::Meta::TypeConstraint->new( name => 'Foo.Bar::Baz' ) }
+'Type names can contain periods and colons';
+
+throws_ok { subtype 'Foo-Baz' => as 'Item' }
+qr/contains invalid characters/,
+    'Type names cannot contain a dash (via subtype sugar)';
+
+lives_ok { subtype 'Foo.Bar::Baz' => as 'Item' }
+'Type names can contain periods and colons (via subtype sugar)';
+
+is( Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[In-valid]'),
+    undef,
+    'find_or_parse_type_constraint returns undef on an invalid name' );
+
+is( Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Va.lid]'),
+    'ArrayRef[Va.lid]',
+    'find_or_parse_type_constraint returns name for valid name' );


Property changes on: Moose/trunk/t/040_type_constraints/033_type_names.t
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Rev
Name: svn:eol-style
   + native




More information about the Moose-commits mailing list