more tests - check for not matching Inf, NaN, etc.
parent
09fccabcf8
commit
d414782aa7
1
dist.ini
1
dist.ini
|
@ -46,6 +46,7 @@ Net::IDN::Encode = 2.003
|
|||
Astro::MoonPhase = 0
|
||||
Acme::rafl::Everywhere = 0.008
|
||||
Lingua::EN::Numbers::Ordinate = 1.02
|
||||
Data::Float = 0
|
||||
; Hashes
|
||||
Digest::SHA = 5.82
|
||||
; Factors
|
||||
|
|
|
@ -3,12 +3,14 @@ package DDG::Goodie::Conversions;
|
|||
|
||||
use DDG::Goodie;
|
||||
use Scalar::Util qw/looks_like_number/;
|
||||
use Data::Float qw/float_is_infinite float_is_nan/;
|
||||
|
||||
###@todo
|
||||
### -- 1 -- include more unit types
|
||||
### see: https://github.com/duckduckgo/zeroclickinfo-goodies/issues/318
|
||||
### -- 2 -- think about special ways feet-inches can be written (2'-4", etc.)
|
||||
### -- 3 -- would like to handle things like "6^2 g to oz"
|
||||
### -- 3 -- would like to handle things like "6^2 g to oz" (present undef;)
|
||||
### -- 4 -- would like to handle things like "5yds to km" (present undef;)
|
||||
|
||||
# metric ton is base unit for mass
|
||||
# known SI units and aliases / plurals
|
||||
|
@ -199,19 +201,26 @@ handle query => sub {
|
|||
my @args = split(/\s+/, $_);
|
||||
my $factor = 1;
|
||||
foreach my $arg (@args) {
|
||||
return if $arg =~ /\D/; # see @todo #3
|
||||
if (looks_like_number($arg)) {
|
||||
# looks_like_number thinks 'Inf' and 'NaN' are numbers:
|
||||
return if float_is_infinite($arg) || float_is_nan($arg);
|
||||
|
||||
$factor = $arg unless $factor != 1; # drop n > 1 #s
|
||||
|
||||
if ($match_types[0] !~ /temperature|pressure/) { # for when temp/pressure added in future
|
||||
return if $factor < 0; # negative weights, etc. seem impossible :)
|
||||
}
|
||||
}
|
||||
else {
|
||||
# if it doesn't look like a number, and it contains a number (e.g., '6^2'):
|
||||
return if $arg =~ /\d/;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# run the conversion:
|
||||
return "$factor $matches[0] is " . sprintf("%.3f", $factor * ($factors[1] / $factors[0])) . " $matches[1]";
|
||||
};
|
||||
|
||||
|
||||
|
||||
1;
|
||||
|
|
|
@ -25,18 +25,21 @@ ddg_goodie_test(
|
|||
'stone pound' => test_zci('1 stone is 14.000 pound',),
|
||||
'gram pound convert' => test_zci('1 gram is 0.002 pound',),
|
||||
"convert 1 ton to long ton" => test_zci('1 ton is 0.893 long ton',),
|
||||
'puff toke to kludge' => undef,
|
||||
'2 tons to kg' => test_zci('2 tons is 1814.372 kg',),
|
||||
'1 ton to kilos' => test_zci('1 ton is 907.186 kilos',),
|
||||
'3.9 oz g' => test_zci('3.9 oz is 110.563 g',),
|
||||
'convert -9 g to ozs' => undef,
|
||||
'convert 5 oz to yards' => undef,
|
||||
'2 miles to km' => test_zci('2 miles is 3.219 km',),
|
||||
'millimeter centimeters' => test_zci('1 millimeter is 0.100 centimeters',),
|
||||
'convert 5 feet to in' => test_zci('5 feet is 60.000 in',),
|
||||
'0.5 nautical mile to klick' => test_zci('0.5 nautical mile is 0.926 klick',),
|
||||
'meter meter' => test_zci('1 meter is 1.000 meter',),
|
||||
'6^2 oz to grams' => undef,
|
||||
'NaN oz to stones' => undef,
|
||||
'45x10 oz to stones' => undef,
|
||||
'convert -9 g to ozs' => undef,
|
||||
'convert 5 oz to yards' => undef,
|
||||
'puff toke to kludge' => undef,
|
||||
'Inf oz to stones' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue