summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: 95502666af07dc0a821df006ddfb38e41cf9636f (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', '0d00') {
  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, '0'), $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' => '0'), $expected,
  74. "lsmb: $value formatted as $formats[$format][0] - $expected");
  75. }
  76. }
  77. $expected = $form->parse_amount({'numberformat' => '1000.00'}, '0.00');
  78. is($form->format_amount({'numberformat' => '1000.00'} , $expected, 2, 'x'), 'x',
  79. "form: 0.00 with dash x");
  80. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  81. 'amount' => $expected, 'precision' => 2,
  82. 'neg_format' => 'x'), 'x',
  83. "lsmb: 0.00 with dash x");
  84. foreach my $format (0 .. $#formats) {
  85. %myconfig = (numberformat => $formats[$format][0]);
  86. my $thou = $formats[$format][1];
  87. my $dec = $formats[$format][2];
  88. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  89. '7t777t777d77', '-12d34') {
  90. $expected = $rawValue;
  91. $expected =~ s/t/$thou/gx;
  92. $expected =~ s/d/$dec/gx;
  93. my $value = $rawValue;
  94. $value =~ s/t//gx;
  95. $value =~ s/d/\./gx;
  96. #my $ovalue = $value;
  97. $value = $form->parse_amount(\%myfooconfig,$value);
  98. #$value = $form->parse_amount(\%myconfig,$value);
  99. is($form->format_amount(\%myconfig,
  100. $form->format_amount(\%myconfig, $value, 2, 'x'),
  101. 2, 'x'), $expected,
  102. "form: Double formatting of $value as $formats[$format][0] - $expected");
  103. is($lsmb->format_amount('user' => \%myconfig,
  104. 'amount' =>
  105. $lsmb->format_amount('user' => \%myconfig,
  106. 'amount' => $value,
  107. 'precision' => 2,
  108. 'neg_format' => 'x'),
  109. 'precision' => 2, 'neg_format' => 'x'), $expected,
  110. "lsmb: Double formatting of $value as $formats[$format][0] - $expected");
  111. }
  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, $expected), '==', $value,
  131. "form: $expected parsed as $formats[$format][0] - $value");
  132. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  133. 'amount' => $expected), '==', $value,
  134. "lsmb: $expected parsed as $formats[$format][0] - $value");
  135. }
  136. $expected = '12 CR';
  137. my $value = Math::BigFloat->new('12');
  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. $expected = '21 DR';
  144. $value = Math::BigFloat->new('-21');
  145. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  146. "form: $expected parsed as $formats[$format][0] - $value");
  147. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => $expected),
  148. '==', $value,
  149. "lsmb: $expected parsed as $formats[$format][0] - $value");
  150. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  151. "form: Empty string returns 0");
  152. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  153. Math::BigFloat->bnan(), "form: Invalid string returns NaN");
  154. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => ''), '==', 0,
  155. "lsmb: Empty string returns 0");
  156. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => 'foo'), 'eq',
  157. Math::BigFloat->bnan(), "lsmb: Invalid string returns NaN");
  158. }
  159. foreach my $format (0 .. $#formats) {
  160. %myconfig = ('numberformat' => $formats[$format][0]);
  161. my $thou = $formats[$format][1];
  162. my $dec = $formats[$format][2];
  163. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  164. '7t777t777d77', '-12d34', '(76t543d21)') {
  165. $expected = $rawValue;
  166. $expected =~ s/t/$thou/gx;
  167. $expected =~ s/d/$dec/gx;
  168. my $value = $rawValue;
  169. $value =~ s/t//gx;
  170. $value =~ s/d/\./gx;
  171. if ($value =~ m/^\(/gx) {
  172. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  173. } else {
  174. $value = Math::BigFloat->new($value);
  175. }
  176. cmp_ok($form->parse_amount(\%myconfig,
  177. $form->parse_amount(\%myconfig, $expected)),
  178. '==', $value,
  179. "form: $expected parsed as $formats[$format][0] - $value");
  180. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  181. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  182. 'amount' => $expected)),
  183. '==', $value,
  184. "lsmb: $expected parsed as $formats[$format][0] - $value");
  185. }
  186. $expected = '12 CR';
  187. my $value = Math::BigFloat->new('12');
  188. cmp_ok($form->parse_amount(\%myconfig,
  189. $form->parse_amount(\%myconfig, $expected)),
  190. '==', $value,
  191. "form: $expected parsed as $formats[$format][0] - $value");
  192. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  193. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  194. 'amount' => $expected)),
  195. '==', $value,
  196. "lsmb: $expected parsed as $formats[$format][0] - $value");
  197. $expected = '21 DR';
  198. $value = Math::BigFloat->new('-21');
  199. cmp_ok($form->parse_amount(\%myconfig,
  200. $form->parse_amount(\%myconfig, $expected)),
  201. '==', $value,
  202. "form: $expected parsed as $formats[$format][0] - $value");
  203. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  204. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  205. 'amount' => $expected)),
  206. '==', $value,
  207. "lsmb: $expected parsed as $formats[$format][0] - $value");
  208. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  209. "form: Empty string returns 0");
  210. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  211. Math::BigFloat->bnan(), "form: Invalid string returns NaN");
  212. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => ''), '==', 0,
  213. "lsmb: Empty string returns 0");
  214. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => 'foo'), 'eq',
  215. Math::BigFloat->bnan(), "lsmb: Invalid string returns NaN");
  216. }