Skip to content

Commit

Permalink
Improved trim() and added tests for -Ofun
Browse files Browse the repository at this point in the history
There actually was an error in my trim(),
and doing /^\s+(.*)\s+$/$1/ is not right either.
Tests prove it :)
  • Loading branch information
cosimo committed Jan 29, 2013
1 parent 5076894 commit 9c39a5a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/Net/Statsd/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use constant {
DEFAULT_LOG_LEVEL => 'info',
};

our $VERSION = '0.09';
our $VERSION = '0.09_01';
our $logger;

# }}}
Expand Down Expand Up @@ -688,9 +688,10 @@ sub stats {
}

sub trim {
$_[0] =~ s{^\s*}{};
$_[0] =~ s{\s*$}{};
return $_[0];
my $s = shift;
$s =~ s{^\s+}{};
$s =~ s{\s+$}{};
return $s;
}

1;
Expand Down
38 changes: 38 additions & 0 deletions t/trim.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=head1 NAME
t/trim.t - Net::Statsd::Server test suite
=head1 DESCRIPTION
Tests the trim function
=cut

use strict;
use warnings;
use Test::More;

use Net::Statsd::Server;

my @tests = (
[ ' abc ' => 'abc' ],
[ ' def' => 'def' ],
[ 'ghi ' => 'ghi' ],
[ ' abc def ghi ' => 'abc def ghi' ],
[ ' ' => '' ],
[ ' ' => '' ],
[ '' => '' ],
[ undef, undef ],
);

plan tests => @tests * 2;

for (@tests) {
my ($str, $expected) = @{ $_ };
my $copy_of_str = $str;
my $res = Net::Statsd::Server::trim($str);
is($str, $copy_of_str, "trim() doesn't touch the original string");
is($res, $expected, "trim('$str') actually returns '$expected'");
}

# END

0 comments on commit 9c39a5a

Please sign in to comment.