Bug 17302: [Follow-up] Make Normalize.pm undef-resistent
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 16 Sep 2016 07:39:15 +0000 (09:39 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Sun, 25 Sep 2016 14:04:54 +0000 (14:04 +0000)
Trivial changes for passing undef to the norm routines.
Added a few dumb tests too.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Util/Normalize.pm
t/Koha/Util/Normalize.t

index 487d5da..a654c3f 100644 (file)
@@ -41,8 +41,10 @@ Default normalization function
 =cut
 
 sub legacy_default {
+    my ( $string ) = @_;
+    return if !defined( $string );
 
-    my $string = uc shift;
+    $string = uc $string;
 
     $string =~ s/[.;:,\]\[\)\(\/'"]//g;
     $string =~ s/^\s+//;
@@ -59,8 +61,8 @@ Normalization function removing spaces
 =cut
 
 sub remove_spaces {
-
-    my $string = shift;
+    my ( $string ) = @_;
+    return if !defined( $string );
 
     $string =~ s/\s+//g;
 
@@ -74,8 +76,10 @@ Normalization function converting characters into upper-case
 =cut
 
 sub upper_case {
+    my ( $string ) = @_;
+    return if !defined( $string );
 
-    my $string = uc shift;
+    $string = uc $string;
 
     return $string;
 }
@@ -87,8 +91,10 @@ Normalization function converting characters into lower-case
 =cut
 
 sub lower_case {
+    my ( $string ) = @_;
+    return if !defined( $string );
 
-    my $string = lc shift;
+    $string = lc $string;
 
     return $string;
 }
index 9ebdf06..7480c23 100644 (file)
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 6;
+use Test::Warn;
 
 BEGIN {
     use_ok('Koha::Util::Normalize');
 }
 
+subtest 'pass undef' => sub {
+    plan tests => 8;
+
+    is( legacy_default(), undef, 'legacy_default returns undef' );
+    warning_is { legacy_default() } undef, 'no warn from legacy_default';
+
+    is( remove_spaces(), undef, 'remove_spaces returns undef' );
+    warning_is { remove_spaces() } undef, 'no warn from remove_spaces';
+
+    is( upper_case(), undef, 'upper_case returns undef' );
+    warning_is { upper_case() } undef, 'no warn from upper_case';
+
+    is( lower_case(), undef, 'lower_case returns undef' );
+    warning_is { lower_case() } undef, 'no warn from lower_case';
+};
+
+
 subtest 'legacy_default() normalizer' => sub {
 
     plan tests => 1;