Upper and Lower types should accept nonalphabetic characters too topic/string_case_types
Karen Etheridge [Thu, 14 Jun 2012 17:58:47 +0000 (10:58 -0700)]
Changes
lib/MooseX/Types/Common/String.pm
t/01-string.t

diff --git a/Changes b/Changes
index c0b1825..cf74a99 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,5 @@
+  - Upper* and Lower* string types now accept non-alphabetic characters (Karen
+    Etheridge)
 0.001007 2012-02-23 11:50EST
   - remove Test::Exception from a test
 0.001006 2012-02-21 13:50EST
index ef2b0c2..f557e39 100644 (file)
@@ -94,12 +94,12 @@ subtype NonEmptyStr,
 
 subtype LowerCaseStr,
   as NonEmptyStr,
-  where { /^[a-z]+$/xms },
-  message { "Must only contain lower case letters" },
+  where { !/[A-Z]/ms },
+  message { "Must not contain upper case letters" },
     ( $Moose::VERSION >= 2.0200
         ? inline_as {
             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
-                . qq{ ( $_[1] =~ m/^[a-z]+\$/xms ) };
+                . qq{ ( $_[1] !~ /[A-Z]/ms ) };
         }
         : ()
     );
@@ -110,12 +110,12 @@ coerce LowerCaseStr,
 
 subtype UpperCaseStr,
   as NonEmptyStr,
-  where { /^[A-Z]+$/xms },
-  message { "Must only contain upper case letters" },
+  where { !/[a-z]/ms },
+  message { "Must not contain lower case letters" },
     ( $Moose::VERSION >= 2.0200
         ? inline_as {
             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
-                . qq{ ( $_[1] =~ m/^[A-Z]+\$/xms ) };
+                . qq{ ( $_[1] !~ m/[a-z]/ms ) };
         }
         : ()
     );
@@ -126,12 +126,12 @@ coerce UpperCaseStr,
 
 subtype LowerCaseSimpleStr,
   as NonEmptySimpleStr,
-  where { /^[a-z]+$/x },
-  message { "Must only contain lower case letters" },
+  where { !/[A-Z]/ },
+  message { "Must not contain upper case letters" },
     ( $Moose::VERSION >= 2.0200
         ? inline_as {
             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
-                . qq{ ( $_[1] =~ m/^[a-z]+\$/x ) };
+                . qq{ ( $_[1] !~ m/[A-Z]/ ) };
         }
         : ()
     );
@@ -142,12 +142,12 @@ coerce LowerCaseSimpleStr,
 
 subtype UpperCaseSimpleStr,
   as NonEmptySimpleStr,
-  where { /^[A-Z]+$/x },
-  message { "Must only contain upper case letters" },
+  where { !/[a-z]/ },
+  message { "Must not contain lower case letters" },
     ( $Moose::VERSION >= 2.0200
         ? inline_as {
             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
-                . qq{ ( $_[1] =~ m/^[A-Z]+\$/x ) };
+                . qq{ ( $_[1] !~ m/[a-z]/ ) };
         }
         : ()
     );
@@ -188,12 +188,12 @@ A Str with no new-line characters and length > 0
 
 =item * LowerCaseSimpleStr
 
-A Str with no new-line characters, length > 0 and all lowercase characters
+A Str with no new-line characters, length > 0 and no uppercase characters
 A coercion exists via C<lc> from NonEmptySimpleStr
 
 =item * UpperCaseSimpleStr
 
-A Str with no new-line characters, length > 0 and all uppercase characters
+A Str with no new-line characters, length > 0 and no lowercase characters
 A coercion exists via C<uc> from NonEmptySimpleStr
 
 =item * Password
@@ -206,12 +206,12 @@ A Str with length > 0
 
 =item * LowerCaseStr
 
-A Str with length > 0 and all lowercase characters.
+A Str with length > 0 and no uppercase characters.
 A coercion exists via C<lc> from NonEmptyStr
 
 =item * UpperCaseStr
 
-A Str with length > 0 and all uppercase characters.
+A Str with length > 0 and no lowercase characters.
 A coercion exists via C<uc> from NonEmptyStr
 
 =item * NumericCode
index 9311296..39d50b9 100644 (file)
@@ -2,7 +2,7 @@
 
 use strict;
 use warnings;
-use Test::More tests => 25;
+use Test::More tests => 33;
 use Test::Fatal;
 
 {
@@ -36,6 +36,8 @@ use Test::Fatal;
 
 my $ins = FooTest->new;
 
+# TODO: need to check both the inlined and non-inlined versions!
+
 is(exception { $ins->simplestr('') }, undef, 'SimpleStr');
 is(exception { $ins->simplestr('good string') }, undef, 'SimpleStr 2');
 isnt(exception { $ins->simplestr("bad\nstring") }, 'SimpleStr 3');
@@ -57,16 +59,23 @@ is(exception { $ins->strongpassword('83773r_ch01c3') }, undef, 'StrongPassword 2
 
 isnt(exception { $ins->lcsimplestr('NOTOK') }, undef, 'LowerCaseSimpleStr');
 is(exception { $ins->lcsimplestr('ok') }, undef, 'LowerCaseSimpleStr 2');
+isnt(exception { $ins->lcsimplestr('NOTOK_123`"') }, undef, 'LowerCaseSimpleStr 3');
+is(exception { $ins->lcsimplestr('ok_123`"') }, undef, 'LowerCaseSimpleStr 4');
 
 isnt(exception { $ins->ucsimplestr('notok') }, undef, 'UpperCaseSimpleStr');
 is(exception { $ins->ucsimplestr('OK') }, undef, 'UpperCaseSimpleStr 2');
+isnt(exception { $ins->ucsimplestr('notok_123`"') }, undef, 'UpperCaseSimpleStr 3');
+is(exception { $ins->ucsimplestr('OK_123`"') }, undef, 'UpperCaseSimpleStr 4');
 
 isnt(exception { $ins->lowercasestr('NOTOK') }, undef, 'LowerCaseStr');
 is(exception { $ins->lowercasestr("ok\nok") }, undef, 'LowerCaseStr 2');
+isnt(exception { $ins->lowercasestr('NOTOK_123`"') }, undef, 'LowerCaseStr 3');
+is(exception { $ins->lowercasestr("ok\n_123`'") }, undef, 'LowerCaseStr 4');
 
 isnt(exception { $ins->uppercasestr('notok') }, undef, 'UpperCaseStr');
 is(exception { $ins->uppercasestr("OK\nOK") }, undef, 'UpperCaseStr 2');
-
+isnt(exception { $ins->uppercasestr('notok_123`"') }, undef, 'UpperCaseStr 3');
+is(exception { $ins->uppercasestr("OK\n_123`'") }, undef, 'UpperCaseStr 4');
 
 is(   exception { $ins->numericcode('032') }, undef,  'NumericCode lives');
 isnt( exception { $ins->numericcode('abc') }, undef,  'NumericCode dies' );