• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Adds global: T
2
3T = (function () {
4  var passed, testNumber, write;
5
6  function T(name, tests) {
7    var time;
8    write(' Testing ' + name + '...');
9    passed = testNumber = 0;
10    time = new Date();
11    tests();
12    time = new Date() - time;
13    T.result = [passed, testNumber, time];
14    if (passed !== testNumber) write('\n');
15    write(' ' + passed + ' of ' + testNumber + ' tests passed in ' + time + ' ms\n');
16  }
17
18  if (typeof window != 'undefined') {
19    write = function (str) {
20      document.body.innerHTML += str.replace(/\n/g, '<br>').replace(/ /g, '&nbsp;');
21    };
22  } else {
23    Decimal = require('../decimal');
24    write = process.stdout.write.bind(process.stdout);
25  }
26
27  T.assert = function (actual) {
28    ++testNumber;
29    if (actual === true) {
30      ++passed;
31      //write('\n Expected and actual: ' + actual);
32    } else {
33      write(
34        '\n  Test number ' + testNumber + ' failed: assert' +
35        '\n  Expected: true' +
36        '\n  Actual:   ' + actual
37      );
38      //process.exit();
39    }
40  };
41
42  T.assertEqual = function (expected, actual) {
43    ++testNumber;
44    // If expected and actual are both NaN, consider them equal.
45    if (expected === actual || expected !== expected && actual !== actual) {
46      ++passed;
47    } else {
48      write(
49        '\n  Test number ' + testNumber + ' failed: assertEqual' +
50        '\n  Expected: ' + expected +
51        '\n  Actual:   ' + actual
52      );
53    }
54  };
55
56  T.assertEqualDecimal = function (x, y) {
57    ++testNumber;
58    if (x.eq(y) || x.isNaN() && y.isNaN()) {
59      ++passed;
60    } else {
61      write(
62        '\n  Test number ' + testNumber + ' failed: assertEqualDecimal' +
63        '\n  x: ' + x.valueOf() +
64        '\n  y: ' + y.valueOf()
65      );
66    }
67  };
68
69  T.assertEqualProps = function (digits, exponent, sign, n) {
70    var i = 0,
71      len = digits.length;
72    ++testNumber;
73    while (i < len && digits[i] === n.d[i]) ++i;
74    if (i === len && i === n.d.length && exponent === n.e && sign === n.s) {
75      ++passed;
76    } else {
77      write(
78        '\n  Test number ' + testNumber + ' failed: assertEqualProps' +
79        '\n  Expected digits:   ' + digits +
80        '\n  Expected exponent: ' + exponent +
81        '\n  Expected sign:     ' + sign +
82        '\n  Actual digits:     ' + n.d +
83        '\n  Actual exponent:   ' + n.e +
84        '\n  Actual sign:       ' + n.s
85      );
86    }
87  };
88
89  T.assertException = function (func, msg) {
90    var actual;
91    ++testNumber;
92    try {
93      func();
94    } catch (e) {
95      actual = e;
96    }
97    if (actual instanceof Error && /DecimalError/.test(actual.message)) {
98      ++passed;
99    } else {
100      write(
101        '\n  Test number ' + testNumber + ' failed: assertException' +
102        '\n  Expected: ' + msg + ' to raise a DecimalError.' +
103        '\n  Actual:   ' + (actual || 'no exception')
104      );
105    }
106  };
107
108  return T;
109})();
110