Permalink
Browse files

Improved trim() and added tests for -Ofun

There actually was an error in my trim(),
and doing /^\s+(.*)\s+$/$1/ is not right either.
Tests prove it :)
  • Loading branch information...
1 parent 5076894 commit 9c39a5a7d22e1840423952d746efbd58dbf5eb8e @cosimo committed Jan 29, 2013
Showing with 43 additions and 4 deletions.
  1. +5 −4 lib/Net/Statsd/Server.pm
  2. +38 −0 t/trim.t
@@ -31,7 +31,7 @@ use constant {
DEFAULT_LOG_LEVEL => 'info',
};
-our $VERSION = '0.09';
+our $VERSION = '0.09_01';
our $logger;
# }}}
@@ -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;
View
@@ -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.