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