e50bd219fee5b9d9cbaebf6bd3f024821936ce45
[kivitendo-erp.git] / t / helper / number.t
1 use Test::More tests => 173;
2
3 use lib 't';
4
5 use SL::Helper::Number qw(:ALL);
6
7 use_ok 'Support::TestSetup';
8
9 Support::TestSetup::login();
10
11 # format
12
13 sub test_format {
14   my ($expected, $amount, $places, $numberformat, $dash, $comment) = @_;
15
16   my $other_numberformat = $numberformat eq '1.000,00' ? '1,000.00' : '1.000,00';
17
18   is (_format_number($amount, $places, numberformat => $numberformat, dash => $dash), $expected, "$comment - explicit");
19
20   {
21     local $::myconfig{numberformat} = $other_numberformat;
22     is (_format_number($amount, $places, numberformat => $numberformat, dash => $dash), $expected, "$comment - explicit with different numberformat");
23   }
24   {
25     local $::myconfig{numberformat} = $numberformat;
26     is (_format_number($amount, $places, dash => $dash), $expected, "$comment - implicit numberformat");
27   }
28
29   # test _format_total
30   if (($places // 0) == 2) {
31     is (_format_total($amount, numberformat => $numberformat, dash => $dash), $expected, "$comment - explicit");
32
33     {
34       local $::myconfig{numberformat} = $other_numberformat;
35       is (_format_total($amount, numberformat => $numberformat, dash => $dash), $expected, "$comment - explicit with different numberformat");
36     }
37     {
38       local $::myconfig{numberformat} = $numberformat;
39       is (_format_total($amount, dash => $dash), $expected, "$comment - implicit numberformat");
40     }
41   }
42 }
43
44
45 test_format('10,00', '1e1', 2, '1.000,00', undef, 'format 1e1 (numberformat: 1.000,00)');
46 test_format('1.000,00', 1000, 2, '1.000,00', undef, 'format 1000 (numberformat: 1.000,00)');
47 test_format('1.000,12', 1000.1234, 2, '1.000,00', undef,  'format 1000.1234 (numberformat: 1.000,00)');
48 test_format('1.000.000.000,12', 1000000000.1234, 2, '1.000,00', undef, 'format 1000000000.1234 (numberformat: 1.000,00)');
49 test_format('-1.000.000.000,12', -1000000000.1234, 2, '1.000,00', undef, 'format -1000000000.1234 (numberformat: 1.000,00)');
50
51 test_format('10.00', '1e1', 2, '1,000.00', undef, 'format 1e1 (numberformat: 1,000.00)');
52 test_format('1,000.00', 1000, 2, '1,000.00', undef, 'format 1000 (numberformat: 1,000.00)');
53 test_format('1,000.12', 1000.1234, 2, '1,000.00', undef, 'format 1000.1234 (numberformat: 1,000.00)');
54 test_format('1,000,000,000.12', 1000000000.1234, 2, '1,000.00', undef, 'format 1000000000.1234 (numberformat: 1,000.00)');
55 test_format('-1,000,000,000.12', -1000000000.1234, 2, '1,000.00', undef, 'format -1000000000.1234 (numberformat: 1,000.00)');
56
57 # negative places
58
59 test_format('1.00045', 1.00045, -2, '1,000.00', undef, 'negative places');
60 test_format('1.00045', 1.00045, -5, '1,000.00', undef, 'negative places 2');
61 test_format('1.00', 1, -2, '1,000.00', undef, 'negative places 3');
62
63 # bugs amd edge cases
64 test_format('0,00005', 0.00005, undef, '1.000,00', undef, 'messing with small numbers and no precision');
65 test_format('0', undef, undef, '1.000,00', undef, 'undef');
66 test_format('0', '', undef, '1.000,00', undef, 'empty string');
67 test_format('0,00', undef, 2, '1.000,00', undef, 'undef with precision');
68 test_format('0,00', '', 2, '1.000,00', undef, 'empty string with prcesion');
69
70 test_format('1', 0.545, 0, '1.000,00', undef, 'rounding up with precision 0');
71 test_format('-1', -0.545, 0, '1.000,00', undef, 'neg rounding up with precision 0');
72
73 test_format('1', 1.00, undef, '1.000,00', undef, 'autotrim to 0 places');
74
75 test_format('10', 10, undef, '1.000,00', undef, 'autotrim does not harm integers');
76 test_format('10,00', 10, 2, '1.000,00', undef, 'autotrim does not harm integers 2');
77 test_format('10,00', 10, -2, '1.000,00', undef, 'autotrim does not harm integers 3');
78 test_format('10', 10, 0, '1.000,00', undef, 'autotrim does not harm integers 4');
79
80 test_format('0', 0, 0, '1.000,00', undef, 'trivial zero');
81 test_format('0,00', -0.002, 2, '1.000,00', undef, 'negative zero');
82 test_format('-0,002', -0.002, 3, '1.000,00', undef, 'negative zero');
83
84 # dash
85
86 test_format('(350,00)', -350, 2, '1.000,00', '-', 'dash -');
87
88 # parse
89
90 sub test_parse {
91   my ($expected, $amount, $numberformat, $comment) = @_;
92
93   my $other_numberformat = $numberformat eq '1.000,00' ? '1,000.00' : '1.000,00';
94
95   is (_parse_number($amount, numberformat => $numberformat), $expected, "$comment - explicit");
96
97   {
98     local $::myconfig{numberformat} = $other_numberformat;
99     is (_parse_number($amount, numberformat => $numberformat), $expected, "$comment - explicit with different numberformat");
100   }
101   {
102     local $::myconfig{numberformat} = $numberformat;
103     is (_parse_number($amount), $expected, "$comment - implicit numberformat");
104   }
105 }
106
107
108 test_parse(12345,     '12345',        '1.000,00', '12345 (numberformat: 1.000,00)');
109 test_parse(1234.5,    '1.234,5',      '1.000,00', '1.234,5 (numberformat: 1.000,00)');
110 test_parse(9871234.5, '9.871.234,5',  '1.000,00', '9.871.234,5 (numberformat: 1.000,00)');
111 test_parse(1234.5,    '1234,5',       '1.000,00', '1234,5 (numberformat: 1.000,00)');
112 test_parse(12345,     '012345',       '1.000,00', '012345 (numberformat: 1.000,00)');
113 test_parse(1234.5,    '01.234,5',     '1.000,00', '01.234,5 (numberformat: 1.000,00)');
114 test_parse(1234.5,    '01234,5',      '1.000,00', '01234,5 (numberformat: 1.000,00)');
115 test_parse(9871234.5, '09.871.234,5', '1.000,00', '09.871.234,5 (numberformat: 1.000,00)');
116
117 # round
118
119 is(_round_number('3.231',2),'3.23');
120 is(_round_number('3.234',2),'3.23');
121 is(_round_number('3.235',2),'3.24');
122 is(_round_number('5.786',2),'5.79');
123 is(_round_number('2.342',2),'2.34');
124 is(_round_number('1.2345',2),'1.23');
125 is(_round_number('8.2345',2),'8.23');
126 is(_round_number('8.2350',2),'8.24');
127
128
129 is(_round_total('3.231'),'3.23');
130 is(_round_total('3.234'),'3.23');
131 is(_round_total('3.235'),'3.24');
132 is(_round_total('5.786'),'5.79');
133 is(_round_total('2.342'),'2.34');
134 is(_round_total('1.2345'),'1.23');
135 is(_round_total('8.2345'),'8.23');
136 is(_round_total('8.2350'),'8.24');