1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
MikeMcl_decimal.js/test/setup.js

110 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2016-01-25 00:11:32 +00:00
// Adds global: T
T = (function () {
2016-02-06 17:51:29 +00:00
var passed, testNumber, write;
2016-01-25 00:11:32 +00:00
2016-02-06 17:51:29 +00:00
function T(name, tests) {
var time;
2016-03-09 22:54:34 +00:00
write(' Testing ' + name + '...');
2016-01-25 00:11:32 +00:00
passed = testNumber = 0;
2016-02-06 17:51:29 +00:00
time = new Date();
tests();
time = new Date() - time;
T.result = [passed, testNumber, time];
if (passed !== testNumber) write('\n');
2016-03-09 22:54:34 +00:00
write(' ' + passed + ' of ' + testNumber + ' tests passed in ' + time + ' ms\n');
2016-01-25 00:11:32 +00:00
}
if (typeof window != 'undefined') {
2016-02-06 17:51:29 +00:00
write = function (str) {
document.body.innerHTML += str.replace(/\n/g, '<br>').replace(/ /g, '&nbsp;');
2016-01-25 00:11:32 +00:00
};
} else {
Decimal = require('../decimal');
2016-02-06 17:51:29 +00:00
write = process.stdout.write.bind(process.stdout);
2016-01-25 00:11:32 +00:00
}
T.assert = function (actual) {
++testNumber;
if (actual === true) {
++passed;
//write('\n Expected and actual: ' + actual);
} else {
2016-02-06 17:51:29 +00:00
write(
'\n Test number ' + testNumber + ' failed: assert' +
'\n Expected: true' +
'\n Actual: ' + actual
);
2016-01-25 00:11:32 +00:00
//process.exit();
}
};
2016-02-04 23:52:10 +00:00
T.assertEqual = function (expected, actual) {
++testNumber;
2016-02-06 17:51:29 +00:00
// If expected and actual are both NaN, consider them equal.
2016-02-04 23:52:10 +00:00
if (expected === actual || expected !== expected && actual !== actual) {
++passed;
} else {
2016-02-06 17:51:29 +00:00
write(
'\n Test number ' + testNumber + ' failed: assertEqual' +
'\n Expected: ' + expected +
'\n Actual: ' + actual
);
2016-02-04 23:52:10 +00:00
}
};
T.assertEqualDecimal = function (x, y) {
++testNumber;
if (x.eq(y) || x.isNaN() && y.isNaN()) {
++passed;
} else {
2016-02-06 17:51:29 +00:00
write(
'\n Test number ' + testNumber + ' failed: assertEqualDecimal' +
'\n x: ' + x.valueOf() +
'\n y: ' + y.valueOf()
);
2016-02-04 23:52:10 +00:00
}
};
T.assertEqualProps = function (digits, exponent, sign, n) {
2016-01-25 00:11:32 +00:00
var i = 0,
len = digits.length;
++testNumber;
while (i < len && digits[i] === n.d[i]) ++i;
if (i === len && i === n.d.length && exponent === n.e && sign === n.s) {
++passed;
} else {
2016-02-06 17:51:29 +00:00
write(
'\n Test number ' + testNumber + ' failed: assertEqualProps' +
'\n Expected digits: ' + digits +
'\n Expected exponent: ' + exponent +
'\n Expected sign: ' + sign +
'\n Actual digits: ' + n.d +
'\n Actual exponent: ' + n.e +
'\n Actual sign: ' + n.s
);
2016-01-25 00:11:32 +00:00
}
};
T.assertException = function (func, msg) {
var actual;
++testNumber;
try {
func();
} catch (e) {
actual = e;
}
if (actual instanceof Error && /DecimalError/.test(actual.message)) {
++passed;
} else {
2016-02-06 17:51:29 +00:00
write(
'\n Test number ' + testNumber + ' failed: assertException' +
'\n Expected: ' + msg + ' to raise a DecimalError.' +
'\n Actual: ' + (actual || 'no exception')
);
2016-01-25 00:11:32 +00:00
}
};
return T;
})();