From 6250c9f16369e156d12d5fa943e7d4ca8ab3809c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Fri, 5 Jan 2024 16:25:57 +0100 Subject: [PATCH] fix: remove warnings for empty/undef quantities (#9573) --- lib/ProductOpener/Units.pm | 44 ++++++++++++++++++++++++-------------- tests/unit/units.t | 21 ++++++++++-------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/ProductOpener/Units.pm b/lib/ProductOpener/Units.pm index ee3d042e2fe79..b2ee561a16add 100644 --- a/lib/ProductOpener/Units.pm +++ b/lib/ProductOpener/Units.pm @@ -248,17 +248,19 @@ sub parse_quantity_unit ($quantity, $standard_unit_bool = undef) { # 6 bricks de 1 l # 10 unités, 170 g # 4 bouteilles en verre de 20cl - if ($quantity - =~ /(?\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?$number_regexp)(\s)?(?$units_regexp)\b/i - ) - { - $m = $+{number}; - $q = lc($+{quantity}); - $u = $+{unit}; - } - elsif ($quantity =~ /(?$number_regexp)(\s)?(?$units_regexp)\s*\b/i) { - $q = lc($+{quantity}); - $u = $+{unit}; + if (defined $quantity) { + if ($quantity + =~ /(?\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?$number_regexp)(\s)?(?$units_regexp)\b/i + ) + { + $m = $+{number}; + $q = lc($+{quantity}); + $u = $+{unit}; + } + elsif ($quantity =~ /(?$number_regexp)(\s)?(?$units_regexp)\s*\b/i) { + $q = lc($+{quantity}); + $u = $+{unit}; + } } return ($q, $m, $u); @@ -303,13 +305,23 @@ Returns undef if no unit was detected. =cut sub extract_standard_unit ($quantity_field) { + + my $standard_unit = undef; + my (undef, undef, $unit) = parse_quantity_unit($quantity_field); - # search in the map of all synonyms in all languages ($units_names) - $unit = lc($unit); - my $unit_id = $units_names{$unit}; # $unit_id can be undefined + if (defined $unit) { + + # search in the map of all synonyms in all languages ($units_names) + $unit = lc($unit); + my $unit_id = $units_names{$unit}; # $unit_id can be undefined + if (defined $unit_id) { + + $standard_unit = $units{$unit_id}{standard_unit}; # standard_unit can be undefined + } + } - return $units{$unit_id}{standard_unit}; # standard_unit can be undefined + return $standard_unit; } =head2 normalize_serving_size($serving) @@ -324,7 +336,7 @@ sub normalize_serving_size ($serving) { # Regex captures any ( )? group, but leaves allowances for a preceding # token to allow for patterns like "One bag (32g)", "1 small bottle (180ml)" etc - if ($serving =~ /^(.*[ \(])?(?$number_regexp)( )?(?$units_regexp)\b/i) { + if ((defined $serving) and ($serving =~ /^(.*[ \(])?(?$number_regexp)( )?(?$units_regexp)\b/i)) { my $q = $+{quantity}; my $u = $+{unit}; $q = convert_string_to_number($q); diff --git a/tests/unit/units.t b/tests/unit/units.t index bc835523f6df0..94437c28ee71c 100644 --- a/tests/unit/units.t +++ b/tests/unit/units.t @@ -151,19 +151,22 @@ is(normalize_serving_size("5 bottles .33L"), 330); is(normalize_serving_size("5 bottles2.33L"), undef); # Broken string, missing word separator before number my @serving_sizes = ( - ["100g", "100"], - ["250 g", "250"], - ["1.5kg", "1500"], - ["2,5g", "2.5"], - ["1 plate (25g)", "25"], - ["1 grilled link (82g)", "82"], - ["2 buns = 20g", "20"], - ["43 someinvalidunit (430g)", "430"], - ["1500ml", "1500"], + [undef, undef, undef], + ["", undef, undef], + ["100g", "100", "g"], + ["250 g", "250", "g"], + ["1.5kg", "1500", "g"], + ["2,5g", "2.5", "g"], + ["1 plate (25g)", "25", "g"], + ["1 grilled link (82g)", "82", "g"], + ["2 buns = 20g", "20", "g"], + ["43 someinvalidunit (430g)", "430", "g"], + ["1500ml", "1500", "ml"], ); foreach my $test_ref (@serving_sizes) { is(normalize_serving_size($test_ref->[0]), $test_ref->[1]) or diag explain $test_ref; + is(extract_standard_unit($test_ref->[0]), $test_ref->[2]) or diag explain $test_ref; } done_testing();