From: Florian Ragwitz Date: Sun, 19 Jul 2009 00:50:22 +0000 (+0200) Subject: Add length to String. X-Git-Tag: 0.21~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-AttributeHelpers.git;a=commitdiff_plain;h=6c321470afd71bf53767618279e3b7bd7dd8cb10 Add length to String. --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm index 716e9d4..21748a7 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm @@ -6,7 +6,7 @@ our $VERSION = '0.21'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -sub append : method { +sub append : method { my ($attr, $reader, $writer) = @_; return sub { $writer->( $_[0], $reader->($_[0]) . $_[1] ) }; @@ -72,6 +72,14 @@ sub clear : method { return sub { $writer->( $_[0], '' ) } } +sub length : method { + my ($attr, $reader, $writer) = @_; + return sub { + my $v = $reader->($_[0]); + return CORE::length($v); + }; +} + sub substr : method { my ($attr, $reader, $writer) = @_; return sub { @@ -104,10 +112,10 @@ __END__ =head1 NAME MooseX::AttributeHelpers::MethodProvider::String - + =head1 DESCRIPTION -This is a role which provides the method generators for +This is a role which provides the method generators for L. =head1 METHODS @@ -138,13 +146,15 @@ L. =item B +=item B + =item B =back =head1 BUGS -All complex software has bugs lurking in it, and this module is no +All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. diff --git a/t/007_basic_string.t b/t/007_basic_string.t index 8ff483c..3763169 100644 --- a/t/007_basic_string.t +++ b/t/007_basic_string.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More tests => 30; BEGIN { use_ok('MooseX::AttributeHelpers'); @@ -28,6 +28,7 @@ BEGIN { chomp => 'chomp_string', clear => 'clear_string', substr => 'sub_string', + length => 'length_string', }, curries => { append => {exclaim => [ '!' ]}, @@ -42,13 +43,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$/"); @@ -70,6 +73,7 @@ is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" ); $page->replace_string(qr/([ao])/, sub { uc($1) }); is($page->string, 'bArcfo', "substitution"); +is($page->length_string, 6, 'right length'); $page->exclaim; is($page->string, 'bArcfo!', 'exclaim!'); @@ -104,7 +108,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', @@ -114,5 +118,6 @@ is_deeply($string->provides, { chomp => 'chomp_string', clear => 'clear_string', substr => 'sub_string', + length => 'length_string', }, '... got the right provides methods'); diff --git a/t/207_trait_string.t b/t/207_trait_string.t index 7eb470e..d6d6783 100644 --- a/t/207_trait_string.t +++ b/t/207_trait_string.t @@ -3,11 +3,11 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More tests => 30; use Test::Moose 'does_ok'; BEGIN { - use_ok('MooseX::AttributeHelpers'); + use_ok('MooseX::AttributeHelpers'); } { @@ -29,6 +29,7 @@ BEGIN { chomp => 'chomp_string', clear => 'clear_string', substr => 'sub_string', + length => 'length_string', }, curries => { append => {exclaim => [ '!' ]}, @@ -43,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$/"); @@ -66,6 +69,7 @@ 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" ); @@ -105,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', @@ -115,5 +119,6 @@ is_deeply($string->provides, { chomp => 'chomp_string', clear => 'clear_string', substr => 'sub_string', + length => 'length_string', }, '... got the right provides methods');