Skip to content

Commit

Permalink
Merge pull request Perl#7 from kappa/master
Browse files Browse the repository at this point in the history
Test more rare edge cases
  • Loading branch information
billward committed Feb 3, 2017
2 parents b890d3a + 999b733 commit 0ce55c5
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Format.pm
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ sub format_picture
$self->_check_seps();

# Handle negative numbers
my($neg_prefix) = $self->{neg_format} =~ /^([^x]+)/;
my($neg_prefix) = $self->{neg_format} =~ /^([^x]*)/;
my($pic_prefix) = $picture =~ /^([^\#]+)/;
my $neg_pic = $self->{neg_format};
(my $pos_pic = $self->{neg_format}) =~ s/[^x\s]/ /g;
Expand Down Expand Up @@ -857,7 +857,7 @@ sub format_price
$curr_symbol = "" unless defined($curr_symbol);

# Determine which value to use for frac digits
my $frac_digits = ($curr_symbol eq $self->{int_curr_symbol} ?
my $frac_digits = (defined $self->{int_curr_symbol} && $curr_symbol eq $self->{int_curr_symbol} ?
$self->{int_frac_digits} : $self->{frac_digits});

# Determine precision for decimal portion
Expand Down
2 changes: 2 additions & 0 deletions t/bigfloat.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ SKIP:
eval "use Math::BigFloat";
skip "No Math::BigFloat installed" if $@;

is(round(Math::BigFloat->new(123.456), 2), '123.46', "round as sub");

my $nf = Number::Format->new();
is($nf->round(Math::BigFloat->new(123.456), 2), '123.46', "round");
is($nf->format_number(Math::BigFloat->new(500.27), 2, 1), '500.27');
Expand Down
25 changes: 25 additions & 0 deletions t/broken_locale.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- CPerl -*-

use Test::More qw(no_plan);
use strict;

use POSIX;
setlocale(&LC_ALL, 'C');

BEGIN { use_ok('Number::Format') }

sub Number::Format::localeconv {
return {
p_sign_posn => -1,
n_sign_posn => 42,
decimal_point => ',',
# no thousands_sep
};
}

my $fmt = Number::Format->new;

is($fmt->{n_sign_posn}, 42, 'mocked locale in use');
isnt($fmt->{p_sign_posn}, -1, '-1 fixed');

is($fmt->{thousands_sep}, "", 'empty default thousand_sep on comma for decimal_point');
5 changes: 5 additions & 0 deletions t/format_bytes.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ is(format_bytes(1234567890.1, mode => "iec"), '1.15GiB', 'gibi');
is(format_bytes(1048576, mode => "iec",
unit => 'K'), '1,024KiB', 'iec unit');

my $fmt = Number::Format->new(-decimal_digits => 1);
is($fmt->format_bytes(12.345), '12.3', 'default precision to decimal_digits');
undef $fmt->{decimal_digits};
is($fmt->format_bytes(12.345), '12.35', 'hard default precision');

{
my @warnings;
local $SIG{__WARN__} = sub { @warnings = @_ };
Expand Down
1 change: 1 addition & 0 deletions t/format_negative.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ setlocale(&LC_ALL, 'C');
BEGIN { use_ok('Number::Format') }

my $x = Number::Format->new;
is($x->format_negative(123456.51, '[x]'), '[123456.51]', 'negative with explicit format');
$x->{neg_format}='-x';
is($x->format_negative(123456.51), '-123456.51', 'negative');
is($x->format_number(-.509), '-0.51', 'neg round');
Expand Down
5 changes: 5 additions & 0 deletions t/format_number.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ is(format_number('1.2300', 7, 1), '1.2300000', 'extra zeroes');
is(format_number(.23, 7, 1), '0.2300000', 'leading zero');
is(format_number(-100, 7, 1), '-100.0000000', 'negative with zeros');

is(format_number("0.000020000E+00", 7), '2e-05', 'scientific notation not processed');

my $fmt = Number::Format->new(-thousands_sep => ""); # e.g. Russian locales
is($fmt->format_number(12345678.509, 2), '12345678.51', 'rounding, but no thousand sep');

#
# https://rt.cpan.org/Ticket/Display.html?id=40126
# The test should fail because 20 digits is too big to correctly store
Expand Down
7 changes: 7 additions & 0 deletions t/format_picture.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ $pic = '#';
is($x->format_picture(1, $pic), ' 1 ', 'one digit 1');
is($x->format_picture(2, $pic), ' 2 ', 'one digit 2');

is($x->format_picture(1, ''), ' ', 'empty picture');

{
local $x->{neg_format} = 'x';
is($x->format_picture(1, ''), '', 'empty picture w/ trivial neg fmt');
}

{
my @warnings;
local $SIG{__WARN__} = sub { @warnings = @_ };
Expand Down
17 changes: 17 additions & 0 deletions t/format_price.t
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ foreach my $sep (0..2)
}
}

$usd = Number::Format->new(
-int_curr_symbol => 'USD',
-currency_symbol => '$',
-frac_digits => 3,
-decimal_digits => 1,
);
is($usd->format_price(123456.51, undef, 'int_curr_symbol'), 'USD 123,456.51', 'same as default');
is($usd->format_price(123456.511, undef, 'currency_symbol'), '$ 123,456.511', 'use currency_symbol str');

undef $usd->{int_curr_symbol};
is($usd->format_price(123456.5111), '$ 123,456.511', 'another default');
undef $usd->{currency_symbol};
is($usd->format_price(123456.5111), '123,456.511', 'hard default');
undef $usd->{frac_digits};
is($usd->format_price(123456.5111), '123,456.5', 'frac_digits undef default');
undef $usd->{decimal_digits};
is($usd->format_price(123456.5111), '123,456.51', 'hard decimal default');

my %prices = ( 1234 => "EUR 1.234,00",
56 => "EUR 56,00",
Expand Down
Empty file modified t/object.t
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions t/round.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ ok(compare_numbers(round(123456.78951, -2), 123500), 'precision=-2' );
# number math will result in 1 rather than 1.01 for this test.
is(round(1.005, 2), 1.01, 'string-eq' );

my $fmt = Number::Format->new;
undef $fmt->{decimal_digits};
ok(compare_numbers($fmt->round(123456.789), 123456.79), 'hard default precision');

# Compare numbers within an epsilon value to avoid false negative
# results due to floating point math
sub compare_numbers
Expand Down

0 comments on commit 0ce55c5

Please sign in to comment.