summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: b26ed7e4d342ec21ab01f68af2a2b00cf78db964 (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. foreach my $format (0 .. $#formats) {
  78. %myconfig = (numberformat => $formats[$format][0]);
  79. my $thou = $formats[$format][1];
  80. my $dec = $formats[$format][2];
  81. my $rawValue = '6d00';
  82. $expected = $rawValue;
  83. $expected =~ s/d/$dec/gx;
  84. my $value = $form->parse_amount(\%myfooconfig, '6');
  85. is($form->format_amount(\%myconfig, $value, 2, '0'), $expected,
  86. "form: $value formatted as $formats[$format][0] - $expected");
  87. is($lsmb->format_amount('user' => \%myconfig,
  88. 'amount' => $value, 'precision' => 2,
  89. 'neg_format' => '0'), $expected,
  90. "lsmb: $value formatted as $formats[$format][0] - $expected");
  91. }
  92. $expected = $form->parse_amount({'numberformat' => '1000.00'}, '0.00');
  93. is($form->format_amount({'numberformat' => '1000.00'} , $expected, 2, 'x'), 'x',
  94. "form: 0.00 with dash x");
  95. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  96. 'amount' => $expected, 'precision' => 2,
  97. 'neg_format' => 'x'), 'x',
  98. "lsmb: 0.00 with dash x");
  99. is($form->format_amount({'numberformat' => '1000.00'} , $expected, 2, ''), '',
  100. "form: 0.00 with dash ''");
  101. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  102. 'amount' => $expected, 'precision' => 2,
  103. 'neg_format' => ''), '',
  104. "lsmb: 0.00 with dash ''");
  105. is($form->format_amount({'numberformat' => '1000.00'} , $expected, 2), '',
  106. "form: 0.00 with undef dash");
  107. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  108. 'amount' => $expected, 'precision' => 2), '',
  109. "lsmb: 0.00 with undef dash");
  110. is($form->format_amount({'apples' => '1000.00'} , 'foo', 2), 'NaN',
  111. "form: No numberformat set, invalid amount");
  112. is($lsmb->format_amount('user' => {'apples' => '1000.00'},
  113. 'amount' => 'foo', 'precision' => 2), 'NaN',
  114. "lsmb: No numberformat set, invalid amount");
  115. cmp_ok($form->format_amount({'apples' => '1000.00'} , '1.00', 2), '==', 1,
  116. "form: No numberformat set, valid amount");
  117. cmp_ok($lsmb->format_amount('user' => {'apples' => '1000.00'},
  118. 'amount' => '1.00', 'precision' => 2), '==', 1,
  119. "lsmb: No numberformat set, valid amount");
  120. is($form->format_amount({'numberformat' => '1000.00'} , '-1.00', 2, '-'), '(1.00)',
  121. "form: -1.00 with dash '-'");
  122. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  123. 'amount' => '-1.00', 'precision' => 2, 'neg_format' => '-'), '(1.00)',
  124. "lsmb: -1.00 with dash '-'");
  125. is($form->format_amount({'numberformat' => '1000.00'} , '1.00', 2, '-'), '1.00',
  126. "form: 1.00 with dash '-'");
  127. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  128. 'amount' => '1.00', 'precision' => 2, 'neg_format' => '-'), '1.00',
  129. "lsmb: 1.00 with dash '-'");
  130. is($form->format_amount({'numberformat' => '1000.00'} , '-1.00', 2, 'DRCR'),
  131. '1.00 DR', "form: -1.00 with dash DRCR");
  132. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  133. 'amount' => '-1.00', 'precision' => 2, 'neg_format' => 'DRCR'),
  134. '1.00 DR', "lsmb: -1.00 with dash DRCR");
  135. is($form->format_amount({'numberformat' => '1000.00'} , '1.00', 2, 'DRCR'),
  136. '1.00 CR', "form: 1.00 with dash DRCR");
  137. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  138. 'amount' => '1.00', 'precision' => 2, 'neg_format' => 'DRCR'),
  139. '1.00 CR', "lsmb: 1.00 with dash DRCR");
  140. is($form->format_amount({'numberformat' => '1000.00'} , '-1.00', 2, 'x'), '-1.00',
  141. "form: -1.00 with dash 'x'");
  142. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  143. 'amount' => '-1.00', 'precision' => 2, 'neg_format' => 'x'), '-1.00',
  144. "lsmb: -1.00 with dash 'x'");
  145. is($form->format_amount({'numberformat' => '1000.00'} , '1.00', 2, 'x'), '1.00',
  146. "form: 1.00 with dash 'x'");
  147. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  148. 'amount' => '1.00', 'precision' => 2, 'neg_format' => 'x'), '1.00',
  149. "lsmb: 1.00 with dash 'x'");
  150. # Triggers the $amount .= "\.$dec" if ($dec ne ""); check to false
  151. is($form->format_amount({'numberformat' => '1000.00'} , '1.00'), '1',
  152. "form: 1.00 with no precision or dash (1000.00)");
  153. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  154. 'amount' => '1.00'), '1',
  155. "lsmb: 1.00 with no precision or dash (1000.00)");
  156. is($form->format_amount({'numberformat' => '1,000.00'} , '1.00'), '1',
  157. "form: 1.00 with no precision or dash (1,000.00)");
  158. is($lsmb->format_amount('user' => {'numberformat' => '1,000.00'},
  159. 'amount' => '1.00'), '1',
  160. "lsmb: 1.00 with no precision or dash (1,000.00)");
  161. is($form->format_amount({'numberformat' => '1 000.00'} , '1.00'), '1',
  162. "form: 1.00 with no precision or dash (1 000.00)");
  163. is($lsmb->format_amount('user' => {'numberformat' => '1 000.00'},
  164. 'amount' => '1.00'), '1',
  165. "lsmb: 1.00 with no precision or dash (1 000.00)");
  166. is($form->format_amount({'numberformat' => '1\'000.00'} , '1.00'), '1',
  167. "form: 1.00 with no precision or dash (1'000.00)");
  168. is($lsmb->format_amount('user' => {'numberformat' => '1\'000.00'},
  169. 'amount' => '1.00'), '1',
  170. "lsmb: 1.00 with no precision or dash (1'000.00)");
  171. is($form->format_amount({'numberformat' => '1.000,00'} , '1,00'), '1',
  172. "form: 1,00 with no precision or dash (1.000,00)");
  173. is($lsmb->format_amount('user' => {'numberformat' => '1.000,00'},
  174. 'amount' => '1,00'), '1',
  175. "lsmb: 1,00 with no precision or dash (1.000,00)");
  176. is($form->format_amount({'numberformat' => '1000,00'} , '1,00'), '1',
  177. "form: 1,00 with no precision or dash (1000,00)");
  178. is($lsmb->format_amount('user' => {'numberformat' => '1000,00'},
  179. 'amount' => '1,00'), '1',
  180. "lsmb: 1,00 with no precision or dash (1000,00)");
  181. is($form->format_amount({'numberformat' => '1000.00'} , '1.50'), '1.5',
  182. "form: 1.50 with no precision or dash");
  183. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  184. 'amount' => '1.50'), '1.5',
  185. "lsmb: 1.50 with no precision or dash");
  186. is($form->format_amount({'numberformat' => '1000.00'} , '0.0', undef, '0'), '0',
  187. "form: 0.0 with no precision, dash '0'");
  188. is($lsmb->format_amount('user' => {'numberformat' => '1000.00'},
  189. 'amount' => '0.0', 'neg_format' => '0'), '0',
  190. "lsmb: 0.0 with no precision, dash '0'");
  191. foreach my $format (0 .. $#formats) {
  192. %myconfig = (numberformat => $formats[$format][0]);
  193. my $thou = $formats[$format][1];
  194. my $dec = $formats[$format][2];
  195. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  196. '7t777t777d77', '-12d34') {
  197. $expected = $rawValue;
  198. $expected =~ s/t/$thou/gx;
  199. $expected =~ s/d/$dec/gx;
  200. my $value = $rawValue;
  201. $value =~ s/t//gx;
  202. $value =~ s/d/\./gx;
  203. #my $ovalue = $value;
  204. $value = $form->parse_amount(\%myfooconfig,$value);
  205. #$value = $form->parse_amount(\%myconfig,$value);
  206. is($form->format_amount(\%myconfig,
  207. $form->format_amount(\%myconfig, $value, 2, 'x'),
  208. 2, 'x'), $expected,
  209. "form: Double formatting of $value as $formats[$format][0] - $expected");
  210. is($lsmb->format_amount('user' => \%myconfig,
  211. 'amount' =>
  212. $lsmb->format_amount('user' => \%myconfig,
  213. 'amount' => $value,
  214. 'precision' => 2,
  215. 'neg_format' => 'x'),
  216. 'precision' => 2, 'neg_format' => 'x'), $expected,
  217. "lsmb: Double formatting of $value as $formats[$format][0] - $expected");
  218. }
  219. }
  220. foreach my $format (0 .. $#formats) {
  221. %myconfig = ('numberformat' => $formats[$format][0]);
  222. my $thou = $formats[$format][1];
  223. my $dec = $formats[$format][2];
  224. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  225. '7t777t777d77', '-12d34', '(76t543d21)') {
  226. $expected = $rawValue;
  227. $expected =~ s/t/$thou/gx;
  228. $expected =~ s/d/$dec/gx;
  229. my $value = $rawValue;
  230. $value =~ s/t//gx;
  231. $value =~ s/d/\./gx;
  232. if ($value =~ m/^\(/gx) {
  233. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  234. } else {
  235. $value = Math::BigFloat->new($value);
  236. }
  237. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  238. "form: $expected parsed as $formats[$format][0] - $value");
  239. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  240. 'amount' => $expected), '==', $value,
  241. "lsmb: $expected parsed as $formats[$format][0] - $value");
  242. }
  243. $expected = '12 CR';
  244. my $value = Math::BigFloat->new('12');
  245. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  246. "form: $expected parsed as $formats[$format][0] - $value");
  247. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => $expected),
  248. '==', $value,
  249. "lsmb: $expected parsed as $formats[$format][0] - $value");
  250. $expected = '21 DR';
  251. $value = Math::BigFloat->new('-21');
  252. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  253. "form: $expected parsed as $formats[$format][0] - $value");
  254. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => $expected),
  255. '==', $value,
  256. "lsmb: $expected parsed as $formats[$format][0] - $value");
  257. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  258. "form: Empty string returns 0");
  259. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  260. Math::BigFloat->bnan(), "form: Invalid string returns NaN");
  261. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => ''), '==', 0,
  262. "lsmb: Empty string returns 0");
  263. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => 'foo'), 'eq',
  264. Math::BigFloat->bnan(), "lsmb: Invalid string returns NaN");
  265. }
  266. foreach my $format (0 .. $#formats) {
  267. %myconfig = ('numberformat' => $formats[$format][0]);
  268. my $thou = $formats[$format][1];
  269. my $dec = $formats[$format][2];
  270. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  271. '7t777t777d77', '-12d34', '(76t543d21)') {
  272. $expected = $rawValue;
  273. $expected =~ s/t/$thou/gx;
  274. $expected =~ s/d/$dec/gx;
  275. my $value = $rawValue;
  276. $value =~ s/t//gx;
  277. $value =~ s/d/\./gx;
  278. if ($value =~ m/^\(/gx) {
  279. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  280. } else {
  281. $value = Math::BigFloat->new($value);
  282. }
  283. cmp_ok($form->parse_amount(\%myconfig,
  284. $form->parse_amount(\%myconfig, $expected)),
  285. '==', $value,
  286. "form: $expected parsed as $formats[$format][0] - $value");
  287. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  288. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  289. 'amount' => $expected)),
  290. '==', $value,
  291. "lsmb: $expected parsed as $formats[$format][0] - $value");
  292. }
  293. $expected = '12 CR';
  294. my $value = Math::BigFloat->new('12');
  295. cmp_ok($form->parse_amount(\%myconfig,
  296. $form->parse_amount(\%myconfig, $expected)),
  297. '==', $value,
  298. "form: $expected parsed as $formats[$format][0] - $value");
  299. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  300. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  301. 'amount' => $expected)),
  302. '==', $value,
  303. "lsmb: $expected parsed as $formats[$format][0] - $value");
  304. $expected = '21 DR';
  305. $value = Math::BigFloat->new('-21');
  306. cmp_ok($form->parse_amount(\%myconfig,
  307. $form->parse_amount(\%myconfig, $expected)),
  308. '==', $value,
  309. "form: $expected parsed as $formats[$format][0] - $value");
  310. cmp_ok($lsmb->parse_amount('user' => \%myconfig,
  311. 'amount' => $lsmb->parse_amount('user' => \%myconfig,
  312. 'amount' => $expected)),
  313. '==', $value,
  314. "lsmb: $expected parsed as $formats[$format][0] - $value");
  315. cmp_ok($form->parse_amount(\%myconfig, ''), '==', 0,
  316. "form: Empty string returns 0");
  317. cmp_ok($form->parse_amount(\%myconfig), '==', 0,
  318. "form: undef string returns 0");
  319. cmp_ok($form->parse_amount(\%myconfig, 'foo'), 'eq',
  320. Math::BigFloat->bnan(), "form: Invalid string returns NaN");
  321. cmp_ok($lsmb->parse_amount('user' => \%myconfig), '==', 0,
  322. "lsmb: undef string returns 0");
  323. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => ''), '==', 0,
  324. "lsmb: Empty string returns 0");
  325. cmp_ok($lsmb->parse_amount('user' => \%myconfig, 'amount' => 'foo'), 'eq',
  326. Math::BigFloat->bnan(), "lsmb: Invalid string returns NaN");
  327. }