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