tester.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Add `Test` to global scope.
  2. Test = (function () {
  3. var passed, testNumber, write;
  4. function Test(name, tests) {
  5. var time;
  6. write(' Testing ' + name + '...');
  7. passed = testNumber = 0;
  8. time = new Date();
  9. tests();
  10. time = new Date() - time;
  11. Test.result = [passed, testNumber, time];
  12. if (passed !== testNumber) write('\n');
  13. write(' ' + passed + ' of ' + testNumber + ' tests passed in ' + time + ' ms\n');
  14. }
  15. if (typeof window != 'undefined') {
  16. write = function (str) {
  17. document.body.innerHTML += str.replace(/\n/g, '<br>').replace(/ /g, '&nbsp;');
  18. };
  19. } else {
  20. // Add `BigNumber` to global scope.
  21. BigNumber = require('../bignumber');
  22. write = process.stdout.write.bind(process.stdout);
  23. }
  24. Test.isTrue = function (actual) {
  25. ++testNumber;
  26. if (actual === true) {
  27. ++passed;
  28. //write('\n Expected and actual: ' + actual);
  29. } else {
  30. write(
  31. '\n Test number ' + testNumber + ' failed isTrue test' +
  32. '\n Expected: true' +
  33. '\n Actual: ' + actual
  34. );
  35. //process.exit();
  36. }
  37. };
  38. Test.areEqual = function (expected, actual) {
  39. ++testNumber;
  40. // If expected and actual are both NaN, consider them equal.
  41. if (expected === actual || expected !== expected && actual !== actual) {
  42. ++passed;
  43. } else {
  44. write(
  45. '\n Test number ' + testNumber + ' failed areEqual test' +
  46. '\n Expected: ' + expected +
  47. '\n Actual: ' + actual
  48. );
  49. //process.exit();
  50. }
  51. };
  52. Test.isException = function (func, msg) {
  53. var actual;
  54. ++testNumber;
  55. try {
  56. func();
  57. } catch (e) {
  58. actual = e;
  59. }
  60. if (actual instanceof Error && /BigNumber Error/.test(actual.message)) {
  61. ++passed;
  62. } else {
  63. write(
  64. '\n Test number ' + testNumber + ' failed isException test' +
  65. '\n Expected: ' + msg + ' to raise a BigNumber Error.' +
  66. '\n Actual: ' + (actual || 'no exception')
  67. );
  68. //process.exit();
  69. }
  70. };
  71. Test.write = write;
  72. return Test;
  73. })();
  74. BigNumber.DEBUG = true;