Calculator: fix mis-construed style for numbers with initial 0.

There should never be a thousands separator in that place, so don't
allow one.  This was a bit masked by taking all of the numbers in
combination which gave them more context.

Testing a number individually exposed the error.
master
Matt Miller 2014-06-24 15:51:08 +08:00
parent bc2249abde
commit 41c070ba2b
2 changed files with 4 additions and 2 deletions

View File

@ -246,8 +246,9 @@ sub _well_formed_for_style_func {
return (
$number =~ /^[\d$thousands$decimal]+$/
# Only contains things we understand.
&& ($number !~ /$thousands/ || ($number !~ /$thousands\d{1,2}\b/ && $number !~ /$thousands\d{4,}/))
# You can leave out thousands breaks, but the ones you put in must be in the right place.
&& ($number !~ /$thousands/ || ($number !~ /$thousands\d{1,2}\b/ && $number !~ /$thousands\d{4,}/ && $number !~ /^0\Q$thousands\E/))
# You can leave out thousands breaks, but the ones you put in must be in the right place
# which does not include following an initial 0.
# Note that this does not confirm that they put all the 'required' ones in.
&& ($number !~ /$decimal/ || $number !~ /$decimal(?:.*)?(?:$decimal|$thousands)/)
# You can omit the decimal but you cannot have another decimal or thousands after:

View File

@ -13,6 +13,7 @@ subtest 'display format selection' => sub {
my $ds_name = 'DDG::Goodie::Calculator::display_style';
my $ds = \&$ds_name;
is($ds->('0,013')->{id}, 'euro', '0,013 is euro');
is($ds->('4,431', '4.321')->{id}, 'perl', '4,431 and 4.321 is perl');
is($ds->('4,431', '4.32')->{id}, 'perl', '4,431 and 4.32 is perl');
is($ds->('4,431', '4,32')->{id}, 'euro', '4,431 and 4,32 is euro');