summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: 5b694b1db84fe6ef6c742b7704b2cec1d1e138ad (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More 'no_plan';
  5. use Math::BigFloat;
  6. use LedgerSMB::Form;
  7. my $form = new Form;
  8. my %myconfig;
  9. ok(defined $form);
  10. isa_ok($form, 'Form');
  11. my $expected;
  12. foreach my $value ('0.01', '0.05', '0.015', '0.025', '1.1', '1.5', '1.9',
  13. '10.01', '4', '5', '5.1', '5.4', '5.5', '5.6', '6', '0',
  14. '0.000', '10.155', '55', '0.001', '14.5', '15.5', '4.5') {
  15. foreach my $places ('3', '2', '1', '0') {
  16. Math::BigFloat->round_mode('+inf');
  17. $expected = Math::BigFloat->new($value)->ffround(-$places);
  18. $expected->precision(undef);
  19. is($form->round_amount($value, $places), $expected,
  20. "$value to $places decimal places - $expected");
  21. Math::BigFloat->round_mode('-inf');
  22. $expected = Math::BigFloat->new(-$value)->ffround(-$places);
  23. $expected->precision(undef);
  24. is($form->round_amount(-$value, $places), $expected,
  25. "-$value to $places decimal places - $expected");
  26. }
  27. foreach my $places ('-1', '-2') {
  28. Math::BigFloat->round_mode('+inf');
  29. $expected = Math::BigFloat->new($value)->ffround(-($places-1));
  30. is($form->round_amount($value, $places), $expected,
  31. "$value to $places decimal places - $expected");
  32. Math::BigFloat->round_mode('-inf');
  33. $expected = Math::BigFloat->new(-$value)->ffround(-($places-1));
  34. is($form->round_amount(-$value, $places), $expected,
  35. "-$value to $places decimal places - $expected");
  36. }
  37. }
  38. # TODO Number formatting still needs work for l10n
  39. my @formats = (['1,000.00', ',', '.'], ["1'000.00", "'", '.'],
  40. ['1.000,00', '.', ','], ['1000,00', '', ','],
  41. ['1000.00', '', '.'], ['1 000.00', ' ', '.']);
  42. my %myfooconfig = (numberformat => '1000.00');
  43. foreach my $format (0 .. $#formats) {
  44. %myconfig = (numberformat => $formats[$format][0]);
  45. my $thou = $formats[$format][1];
  46. my $dec = $formats[$format][2];
  47. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  48. '7t777t777d77', '-12d34') {
  49. $expected = $rawValue;
  50. $expected =~ s/t/$thou/gx;
  51. $expected =~ s/d/$dec/gx;
  52. my $value = $rawValue;
  53. $value =~ s/t//gx;
  54. $value =~ s/d/\./gx;
  55. ##$value = Math::BigFloat->new($value);
  56. $value = $form->parse_amount(\%myfooconfig,$value);
  57. is($form->format_amount(\%myconfig, $value, 2, 'x'), $expected,
  58. "$value formatted as $formats[$format][0] - $expected");
  59. }
  60. }
  61. foreach my $format (0 .. $#formats) {
  62. %myconfig = (numberformat => $formats[$format][0]);
  63. my $thou = $formats[$format][1];
  64. my $dec = $formats[$format][2];
  65. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  66. '7t777t777d77', '-12d34') {
  67. $expected = $rawValue;
  68. $expected =~ s/t/$thou/gx;
  69. $expected =~ s/d/$dec/gx;
  70. my $value = $rawValue;
  71. $value =~ s/t//gx;
  72. $value =~ s/d/\./gx;
  73. #my $ovalue = $value;
  74. $value = $form->parse_amount(\%myfooconfig,$value);
  75. #$value = $form->parse_amount(\%myconfig,$value);
  76. is($form->format_amount(\%myconfig,
  77. $form->format_amount(\%myconfig, $value, 2, 'x'),
  78. 2, 'x'),
  79. $expected, "Double formatting of $value as $formats[$format][0] - $expected");
  80. }
  81. }
  82. foreach my $format (0 .. $#formats) {
  83. %myconfig = ('numberformat' => $formats[$format][0]);
  84. my $thou = $formats[$format][1];
  85. my $dec = $formats[$format][2];
  86. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  87. '7t777t777d77', '-12d34', '(76t543d21)') {
  88. $expected = $rawValue;
  89. $expected =~ s/t/$thou/gx;
  90. $expected =~ s/d/$dec/gx;
  91. my $value = $rawValue;
  92. $value =~ s/t//gx;
  93. $value =~ s/d/\./gx;
  94. if ($value =~ m/^\(/gx) {
  95. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  96. } else {
  97. $value = Math::BigFloat->new($value);
  98. }
  99. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  100. "$expected parsed as $formats[$format][0] - $value");
  101. }
  102. $expected = '12 CR';
  103. my $value = Math::BigFloat->new('12');
  104. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  105. "$expected parsed as $formats[$format][0] - $value");
  106. $expected = '21 DR';
  107. $value = Math::BigFloat->new('-21');
  108. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  109. "$expected parsed as $formats[$format][0] - $value");
  110. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  111. "Empty string returns 0");
  112. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  113. Math::BigFloat->bnan(), "Invalid string returns NaN");
  114. }
  115. foreach my $format (0 .. $#formats) {
  116. %myconfig = ('numberformat' => $formats[$format][0]);
  117. my $thou = $formats[$format][1];
  118. my $dec = $formats[$format][2];
  119. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  120. '7t777t777d77', '-12d34', '(76t543d21)') {
  121. $expected = $rawValue;
  122. $expected =~ s/t/$thou/gx;
  123. $expected =~ s/d/$dec/gx;
  124. my $value = $rawValue;
  125. $value =~ s/t//gx;
  126. $value =~ s/d/\./gx;
  127. if ($value =~ m/^\(/gx) {
  128. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  129. } else {
  130. $value = Math::BigFloat->new($value);
  131. }
  132. cmp_ok($form->parse_amount(\%myconfig,
  133. $form->parse_amount(\%myconfig, $expected)),
  134. '==', $value,
  135. "$expected parsed as $formats[$format][0] - $value");
  136. }
  137. $expected = '12 CR';
  138. my $value = Math::BigFloat->new('12');
  139. cmp_ok($form->parse_amount(\%myconfig,
  140. $form->parse_amount(\%myconfig, $expected)),
  141. '==', $value,
  142. "$expected parsed as $formats[$format][0] - $value");
  143. $expected = '21 DR';
  144. $value = Math::BigFloat->new('-21');
  145. cmp_ok($form->parse_amount(\%myconfig,
  146. $form->parse_amount(\%myconfig, $expected)),
  147. '==', $value,
  148. "$expected parsed as $formats[$format][0] - $value");
  149. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  150. "Empty string returns 0");
  151. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  152. Math::BigFloat->bnan(), "Invalid string returns NaN");
  153. }