Add length to String.
[gitmo/MooseX-AttributeHelpers.git] / t / 207_trait_string.t
index 0383870..d6d6783 100644 (file)
@@ -3,11 +3,11 @@
 use strict;
 use warnings;
 
-use Test::More tests => 17;
+use Test::More tests => 30;
 use Test::Moose 'does_ok';
 
 BEGIN {
-    use_ok('MooseX::AttributeHelpers');   
+    use_ok('MooseX::AttributeHelpers');
 }
 
 {
@@ -15,7 +15,7 @@ BEGIN {
     use Moose;
 
     has 'string' => (
-        traits    => [qw/String/],
+        traits    => [qw/MooseX::AttributeHelpers::Trait::String/],
         is        => 'rw',
         isa       => 'Str',
         default   => sub { '' },
@@ -28,6 +28,14 @@ BEGIN {
             chop    => 'chop_string',
             chomp   => 'chomp_string',
             clear   => 'clear_string',
+            substr  => 'sub_string',
+            length  => 'length_string',
+        },
+        curries  => {
+            append  => {exclaim         => [ '!' ]},
+            replace => {capitalize_last => [ qr/(.)$/, sub { uc $1 } ]},
+            match   => {invalid_number  => [ qr/\D/ ]},
+            substr  => {shift_chars     => sub { $_[1]->($_[0], 0, $_[2], '') } },
         }
     );
 }
@@ -36,13 +44,15 @@ my $page = MyHomePage->new();
 isa_ok($page, 'MyHomePage');
 
 is($page->string, '', '... got the default value');
+is($page->length_string, 0, '... length is zero');
 
 $page->string('a');
+is($page->length_string, 1, '... new string has length of one');
 
-$page->inc_string; 
+$page->inc_string;
 is($page->string, 'b', '... got the incremented value');
 
-$page->inc_string; 
+$page->inc_string;
 is($page->string, 'c', '... got the incremented value (again)');
 
 $page->append_string("foo$/");
@@ -59,12 +69,34 @@ is($page->string, "cfo", 'chopped string');
 
 $page->prepend_string("bar");
 is($page->string, 'barcfo', 'prepended to string');
+is($page->length_string, 6, 'right length');
 
 is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
 
 $page->replace_string(qr/([ao])/, sub { uc($1) });
 is($page->string, 'bArcfo', "substitution");
 
+$page->exclaim;
+is($page->string, 'bArcfo!', 'exclaim!');
+
+is($page->sub_string(2), 'rcfo!', 'substr(offset)');
+is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
+is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
+is($page->string, 'bAfo!', 'replacement got inserted');
+
+is($page->shift_chars(2), 'bA', 'curried substr');
+is($page->string, 'fo!', 'replacement got inserted');
+
+$page->string('Moosex');
+$page->capitalize_last;
+is($page->string, 'MooseX', 'capitalize last');
+
+$page->string('1234');
+ok(!$page->invalid_number, 'string "isn\'t an invalid number');
+
+$page->string('one two three four');
+ok($page->invalid_number, 'string an invalid number');
+
 $page->clear_string;
 is($page->string, '', "clear");
 
@@ -77,7 +109,7 @@ is($string->helper_type, 'Str', '... got the expected helper type');
 
 is($string->type_constraint->name, 'Str', '... got the expected type constraint');
 
-is_deeply($string->provides, { 
+is_deeply($string->provides, {
     inc     => 'inc_string',
     append  => 'append_string',
     prepend => 'prepend_string',
@@ -86,5 +118,7 @@ is_deeply($string->provides, {
     chop    => 'chop_string',
     chomp   => 'chomp_string',
     clear   => 'clear_string',
+    substr  => 'sub_string',
+    length  => 'length_string',
 }, '... got the right provides methods');