File: format_price.t

package info (click to toggle)
libnumber-format-perl 1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: perl: 590; makefile: 2
file content (180 lines) | stat: -rw-r--r-- 6,999 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- CPerl -*-

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

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

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

my $usd = Number::Format->new(
                              -int_curr_symbol   => 'USD',
                              -currency_symbol   => '$',
                              -decimal_point     => '.',
                              -frac_digits       => 2,
                              -int_frac_digits   => 2,
                              -n_cs_precedes     => 1,
                              -n_sep_by_space    => 1,
                              -n_sign_posn       => 1,
                              -negative_sign     => '-',
                              -p_cs_precedes     => 1,
                              -p_sep_by_space    => 1,
                              -p_sign_posn       => 1,
                              -positive_sign     => '',
                              -thousands_sep     => ',',
                              -mon_thousands_sep => ',',
                              -decimal_fill      => 1,
                              -decimal_digits    => 2,
                              -mon_decimal_point => '.',
                             );

is($usd->format_price(123456.51),   'USD 123,456.51',     'thou');
is($usd->format_price(1234567.509), 'USD 1,234,567.51',   'mill');
is($usd->format_price(1234.51, 3),  'USD 1,234.510',      'three dec');
is($usd->format_price(123456789.1), 'USD 123,456,789.10', 'zero in dec');
is($usd->format_price(100, '0'),    'USD 100',            'no dec');

$usd->{p_cs_precedes}  = 1;
$usd->{p_sep_by_space} = 0;
$usd->{n_cs_precedes}  = 1;
$usd->{n_sep_by_space} = 0;
$usd->{p_sign_posn}    = 1;
$usd->{n_sign_posn}    = 1;

is($usd->format_price(19.95, undef, 'currency_symbol'),
   '$19.95', 'domestic');

$usd->{int_frac_digits} = 4;
$usd->{frac_digits} = 3;

is($usd->format_price(19.95, undef, 'currency_symbol'),
   '$19.950', 'frac_digits');
is($usd->format_price(19.95), 'USD19.9500', 'int frac digits');

$usd->{n_sign_posn} = 0;
is($usd->format_price(-9.95), '(USD9.9500)', 'n_sign_posn=0');

$usd->{n_sign_posn} = 1;
is($usd->format_price(-9.95), '-USD9.9500', 'n_sign_posn=1');

$usd->{n_sign_posn} = 2;
is($usd->format_price(-9.95), 'USD9.9500-', 'n_sign_posn=2');

$usd->{n_sign_posn} = 3;
is($usd->format_price(-9.95), '-USD9.9500', 'n_sign_posn=3');

$usd->{n_sign_posn} = 4;
is($usd->format_price(-9.95), 'USD-9.9500', 'n_sign_posn=4');

$usd->{n_cs_precedes} = 1;
$usd->{n_sign_posn} = 3;
$usd->{n_sep_by_space} = 0;
is($usd->format_price(-9.95), '-USD9.9500', 'cs_precedes=1,sep_by_space=0');

$usd->{n_sep_by_space} = 1;
is($usd->format_price(-9.95), '-USD 9.9500', 'cs_precedes=1,sep_by_space=1');

$usd->{n_sep_by_space} = 2;
is($usd->format_price(-9.95), '- USD9.9500', 'cs_precedes=1,sep_by_space=2');

$usd->{n_cs_precedes} = 0;
$usd->{n_sign_posn} = 3;
$usd->{n_sep_by_space} = 0;
is($usd->format_price(-9.95), '9.9500-USD', 'cs_precedes=0,sep_by_space=0');

$usd->{n_sep_by_space} = 1;
is($usd->format_price(-9.95), '9.9500 -USD', 'cs_precedes=0,sep_by_space=1');

$usd->{n_sep_by_space} = 2;
is($usd->format_price(-9.95), '9.9500- USD', 'cs_precedes=0,sep_by_space=2');
my %results = ('sep=0 posn=0 prec=0'    => '(9.9500USD)',
               'sep=0 posn=0 prec=1'    => '(USD9.9500)',
               'sep=0 posn=1 prec=0'    => '-9.9500USD',
               'sep=0 posn=1 prec=1'    => '-USD9.9500',
               'sep=0 posn=2 prec=0'    => '9.9500USD-',
               'sep=0 posn=2 prec=1'    => 'USD9.9500-',
               'sep=0 posn=3 prec=0'    => '9.9500-USD',
               'sep=0 posn=3 prec=1'    => '-USD9.9500',
               'sep=0 posn=4 prec=0'    => '9.9500USD-',
               'sep=0 posn=4 prec=1'    => 'USD-9.9500',
               'sep=1 posn=0 prec=0'    => '(9.9500 USD)',
               'sep=1 posn=0 prec=1'    => '(USD 9.9500)',
               'sep=1 posn=1 prec=0'    => '-9.9500 USD',
               'sep=1 posn=1 prec=1'    => '-USD 9.9500',
               'sep=1 posn=2 prec=0'    => '9.9500 USD-',
               'sep=1 posn=2 prec=1'    => 'USD 9.9500-',
               'sep=1 posn=3 prec=0'    => '9.9500 -USD',
               'sep=1 posn=3 prec=1'    => '-USD 9.9500',
               'sep=1 posn=4 prec=0'    => '9.9500 USD-',
               'sep=1 posn=4 prec=1'    => 'USD- 9.9500',
               'sep=2 posn=0 prec=0'    => '(9.9500USD)',
               'sep=2 posn=0 prec=1'    => '(USD9.9500)',
               'sep=2 posn=1 prec=0'    => '- 9.9500USD',
               'sep=2 posn=1 prec=1'    => '- USD9.9500',
               'sep=2 posn=2 prec=0'    => '9.9500USD -',
               'sep=2 posn=2 prec=1'    => 'USD9.9500 -',
               'sep=2 posn=3 prec=0'    => '9.9500- USD',
               'sep=2 posn=3 prec=1'    => '- USD9.9500',
               'sep=2 posn=4 prec=0'    => '9.9500USD -',
               'sep=2 posn=4 prec=1'    => 'USD -9.9500'
              );

foreach my $sep (0..2)
{
    foreach my $posn (0..4)
    {
        foreach my $prec (0..1)
        {
            my $key = "sep=$sep posn=$posn prec=$prec";
            my $want = $results{$key};
            $usd->{n_cs_precedes} = $prec;
            $usd->{n_sign_posn} = $posn;
            $usd->{n_sep_by_space} = $sep;
            is($usd->format_price(-9.95), $want, "$key -> $want");
        }
    }
}


my %prices = ( 1234    => "EUR 1.234,00",
               56      => "EUR 56,00",
               75.2345 => "EUR 75,23",
               12578.5 => "EUR 12.578,50" );

my $nf = Number::Format->new(
                             -int_curr_symbol   => 'EUR',
                             -currency_symbol   => '$',
                             -decimal_point     => ',',
                             -frac_digits       => 2,
                             -int_frac_digits   => 2,
                             -n_cs_precedes     => 1,
                             -n_sep_by_space    => 1,
                             -n_sign_posn       => 1,
                             -negative_sign     => '-',
                             -p_cs_precedes     => 1,
                             -p_sep_by_space    => 1,
                             -p_sign_posn       => 1,
                             -positive_sign     => '',
                             -thousands_sep     => '.',
                             -mon_thousands_sep => '.',
                             -decimal_fill      => 1,
                             -decimal_digits    => 2,
                             -mon_decimal_point => ',',
                            );

for my $price ( sort keys %prices )
{
    my $want = $prices{$price};
    is($nf->format_price($price, 2), $want, "$price -> $want");
}

{
    my @warnings;
    local $SIG{__WARN__} = sub { @warnings = @_ };
    is($nf->format_price(undef), "EUR 0,00");
    my $file = __FILE__;
    like("@warnings",
         qr{Use of uninitialized value in call to Number::Format::format_price at \Q$file\E line \d+[.]?\n});
}