test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var mt = require('./index.js');
  2. var colors = require('colors');
  3. var bignum = require('bignum');
  4. var a = new mt(-123.456);
  5. var res = a.inspect(); // internally calls toString and formatDollars
  6. console.log(" input: -123.456 \n<MoneyTight -$123.46>");
  7. console.log(res);
  8. console.log("\n")
  9. /* formatting:
  10. $0.02 $0.12 and negatives
  11. $1.02 $123.00 and negatives
  12. $12,345.67 $1,234,567.89 and negatives
  13. */
  14. test_format(.02 , '$0.02');
  15. test_format(.12, '$0.12');
  16. test_format(1.02, '$1.02');
  17. test_format(123.00, '$123.00');
  18. test_format(12345.67, '$12,345.67');
  19. test_format(1234567.89, '$1,234,567.89');
  20. test_format(-123.456, '-$123.45');
  21. process.exit();
  22. function test_format(dollars, expected) {
  23. var a = new mt(dollars);
  24. var actual = a.formatDollars();
  25. var color = actual == expected ? 'green' : 'red';
  26. console.log(('expected: ' + expected)[color]);
  27. console.log(('actual: ' + actual)[color]);
  28. console.log('');
  29. }
  30. console.log("-- div0sum --\n-- problem: 10 / 3 = 3.33 * 3 = 9.99 < 10\n")
  31. var a = new mt(10);
  32. var res = a.div0sum(3);
  33. console.log(" input: 10 / 3\n expected: [3.34, 3.33, 3.33]");
  34. console.log(res);
  35. console.log("\n")
  36. var a = new mt(10.01);
  37. var res = a.div0sum(3);
  38. console.log(" input: 10.01 / 3\n expected: [3.34, 3.34, 3.33]");
  39. console.log(res);
  40. console.log("\n")
  41. console.log("-- divRem --\n")
  42. var a = new mt(10.01);
  43. var res = a.divRem(3);
  44. console.log(" input: 10.01 / 3\n expected: [3.33, .02]");
  45. console.log(res);
  46. console.log("\n")