Consolidation and extended test runs
[kivitendo-erp.git] / t / form / format_amount.t
1 use strict;
2 use Test::More;
3
4 use lib 't';
5 use Support::TestSetup;
6
7 Support::TestSetup::login();
8
9 my $config = {};
10
11 $config->{numberformat} = '1.000,00';
12
13 is($::form->format_amount($config, '1e1', 2), '10,00', 'format 1e1 (numberformat: 1.000,00)');
14 is($::form->format_amount($config, 1000, 2), '1.000,00', 'format 1000 (numberformat: 1.000,00)');
15 is($::form->format_amount($config, 1000.1234, 2), '1.000,12', 'format 1000.1234 (numberformat: 1.000,00)');
16 is($::form->format_amount($config, 1000000000.1234, 2), '1.000.000.000,12', 'format 1000000000.1234 (numberformat: 1.000,00)');
17 is($::form->format_amount($config, -1000000000.1234, 2), '-1.000.000.000,12', 'format -1000000000.1234 (numberformat: 1.000,00)');
18
19 $config->{numberformat} = '1,000.00';
20
21 is($::form->format_amount($config, '1e1', 2), '10.00', 'format 1e1 (numberformat: 1,000.00)');
22 is($::form->format_amount($config, 1000, 2), '1,000.00', 'format 1000 (numberformat: 1,000.00)');
23 is($::form->format_amount($config, 1000.1234, 2), '1,000.12', 'format 1000.1234 (numberformat: 1,000.00)');
24 is($::form->format_amount($config, 1000000000.1234, 2), '1,000,000,000.12', 'format 1000000000.1234 (numberformat: 1,000.00)');
25 is($::form->format_amount($config, -1000000000.1234, 2), '-1,000,000,000.12', 'format -1000000000.1234 (numberformat: 1,000.00)');
26
27 $config->{numberformat} = "1'000.00";
28
29 is($::form->format_amount($config, '1e1', 2), "10.00", "format 1e1 (numberformat: 1'000.00)");
30 is($::form->format_amount($config, 1000, 2), "1'000.00", "format 1000 (numberformat: 1'000.00)");
31 is($::form->format_amount($config, 1000.1234, 2), "1'000.12", "format 1000.1234 (numberformat: 1'000.00)");
32 is($::form->format_amount($config, 1000000000.1234, 2), "1'000'000'000.12", "format 1000000000.1234 (numberformat: 1'000.00)");
33 is($::form->format_amount($config, -1000000000.1234, 2), "-1'000'000'000.12", "format -1000000000.1234 (numberformat: 1'000.00)");
34
35 # negative places
36
37 is($::form->format_amount($config, 1.00045, -2), '1.00045', 'negative places');
38 is($::form->format_amount($config, 1.00045, -5), '1.00045', 'negative places 2');
39 is($::form->format_amount($config, 1, -2), '1.00', 'negative places 3');
40
41 # bugs amd edge cases
42 $config->{numberformat} = '1.000,00';
43
44 is($::form->format_amount({ numberformat => '1.000,00' }, 0.00005), '0,00005', 'messing with small numbers and no precision');
45 is($::form->format_amount({ numberformat => '1.000,00' }, undef), '0', 'undef');
46 is($::form->format_amount({ numberformat => '1.000,00' }, ''), '0', 'empty string');
47 is($::form->format_amount({ numberformat => '1.000,00' }, undef, 2), '0,00', 'undef with precision');
48 is($::form->format_amount({ numberformat => '1.000,00' }, '', 2), '0,00', 'empty string with prcesion');
49
50 is($::form->format_amount($config, 0.545, 0), '1', 'rounding up with precision 0');
51 is($::form->format_amount($config, -0.545, 0), '-1', 'neg rounding up with precision 0');
52
53 is($::form->format_amount($config, 1.00), '1', 'autotrim to 0 places');
54
55 is($::form->format_amount($config, 10), '10', 'autotrim does not harm integers');
56 is($::form->format_amount($config, 10, 2), '10,00' , 'autotrim does not harm integers 2');
57 is($::form->format_amount($config, 10, -2), '10,00' , 'autotrim does not harm integers 3');
58 is($::form->format_amount($config, 10, 0), '10', 'autotrim does not harm integers 4');
59
60 is($::form->format_amount($config, 0, 0), '0' , 'trivial zero');
61
62 # dash stuff
63
64 $config->{numberformat} = '1.000,00';
65
66 is($::form->format_amount($config, -350, 2, '-'), '(350,00)', 'dash -');
67
68
69 done_testing;
70
71 1;