assert error change for error thrown, formatted getScript, added default epsilon for assert near

This commit is contained in:
ferricles 2022-06-24 21:24:22 -07:00
parent 61dfe2de65
commit 8805521ce2

View file

@ -418,23 +418,23 @@ function capitalizeFirst(str) {
* If we ever want to write something that needs to import other js files * If we ever want to write something that needs to import other js files
*/ */
const getScript = url => new Promise((resolve, reject) => { const getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script') const script = document.createElement('script');
script.src = url script.src = url;
script.async = true script.async = true;
script.onerror = reject script.onerror = reject;
script.onload = script.onreadystatechange = function () { script.onload = script.onreadystatechange = function () {
const loadState = this.readyState const loadState = this.readyState;
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null script.onload = script.onreadystatechange = null;
resolve() resolve();
} }
document.head.appendChild(script) document.head.appendChild(script);
}) })
/* /*
@ -479,10 +479,10 @@ function assert_equals(arg1, arg2, msg) {
* *
* @param {*} arg1 - first argument to compare. * @param {*} arg1 - first argument to compare.
* @param {*} arg2 - second argument to compare. * @param {*} arg2 - second argument to compare.
* @param {Number} epsilon - the margin of error (<= del difference is ok). * @param {Number} epsilon - the margin of error (<= del difference is ok). Defaults to -1E5.
* @param {String} msg - the error message to throw. * @param {String} msg - the error message to throw.
*/ */
function assert_near(arg1, arg2, epsilon, msg) { function assert_near(arg1, arg2, epsilon = 1E-5, msg) {
if (Math.abs(arg1 - arg2) > epsilon) { if (Math.abs(arg1 - arg2) > epsilon) {
throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + "."); throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + ".");
} }
@ -518,8 +518,8 @@ function assert_null(arg, msg) {
function assert_error(func_binding, msg) { function assert_error(func_binding, msg) {
try { try {
func_binding(); func_binding();
console.trace(msg ? msg : "Function didn't throw an error.");
} catch (err) { } catch (err) {
return; return;
} }
throw new Error(msg ? msg : "Function didn't throw an error.");
} }