From: Jesse Luehrs Date: Sun, 19 Jul 2009 08:27:28 +0000 (-0500) Subject: add length to String X-Git-Tag: 0.89_02~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a1e47bcbf882485fe587fa2c2836bda33263a35;p=gitmo%2FMoose.git add length to String --- diff --git a/lib/Moose/Meta/Attribute/Trait/Native/MethodProvider/String.pm b/lib/Moose/Meta/Attribute/Trait/Native/MethodProvider/String.pm index 9be3ee1..b90b060 100644 --- a/lib/Moose/Meta/Attribute/Trait/Native/MethodProvider/String.pm +++ b/lib/Moose/Meta/Attribute/Trait/Native/MethodProvider/String.pm @@ -73,6 +73,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 { @@ -139,6 +147,8 @@ L. =item B +=item B + =item B =back diff --git a/t/070_attribute_traits/207_trait_string.t b/t/070_attribute_traits/207_trait_string.t index fca6f0e..d51abfd 100644 --- a/t/070_attribute_traits/207_trait_string.t +++ b/t/070_attribute_traits/207_trait_string.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 22; use Test::Moose 'does_ok'; my $uc; @@ -25,6 +25,7 @@ my $uc; chop_string => 'chop', chomp_string => 'chomp', clear_string => 'clear', + length_string => 'length', exclaim => [ append => ['!'] ], capitalize_last => [ replace => [ qr/(.)$/, $uc = sub { uc $1 } ] ], @@ -37,8 +38,10 @@ 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; is( $page->string, 'b', '... got the incremented value' ); @@ -65,6 +68,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!' ); @@ -103,6 +107,7 @@ is_deeply( chop_string => 'chop', chomp_string => 'chomp', clear_string => 'clear', + length_string => 'length', exclaim => [ append => ['!'] ], capitalize_last => [ replace => [ qr/(.)$/, $uc ] ], invalid_number => [ match => [qr/\D/] ],