cryptpad/www/assert/assertions.js

99 lines
3.3 KiB
JavaScript
Raw Normal View History

define([], function () {
return function () {
var failMessages = [];
var passed = 0;
var ASSERTS = [];
var MESSAGES = [];
var assert = function (test, msg) {
MESSAGES.push(msg || false);
2022-04-04 19:01:20 +08:00
ASSERTS.push(function (_cb, i) {
var called = false;
var to;
var cb = function (arg) {
if (to) {
clearTimeout(to);
to = undefined;
}
2022-04-04 19:01:20 +08:00
if (called) { return; }
called = true;
_cb(arg);
};
var _msg = document.createElement('span');
_msg.innerText = "An unexpected error occurred. See your browser's console for more details";
if (msg && !msg.innerText) {
msg.innerText = _msg.innerText;
}
to = setTimeout(function () {
cb({
test: i,
message: msg || _msg,
output: "TIMEOUT",
});
}, 25000);
try {
test(function (result) {
if (result === true) {
passed++;
cb();
} else {
cb({
test: i,
message: msg || _msg,
output: result,
});
}
}, msg);
} catch (err) {
console.error(err);
msg.innerText = `Synchronous error thrown (${(err.message || err)}). See console for more details.`;
//from ${err.fileName} line ${err.lineNumber}`;
cb({
test: i,
message: msg || _msg,
output: {
message: err.message,
file: err.fileName,
line: err.lineNumber,
stack: typeof(err.stack) === 'string'?
err.stack.split('\n'):
err.stack,
},
});
}
});
};
assert.run = function (cb, progress) {
progress = progress || function () {};
var count = ASSERTS.length;
var total = ASSERTS.length;
var done = function (err) {
count--;
if (err) { failMessages.push(err); }
if (count === 0) {
cb({
total: ASSERTS.length,
passed: passed,
errors: failMessages,
});
}
};
ASSERTS.forEach(function (f, index) {
f(function (err) {
//console.log("test " + index);
progress(index, total);
done(err, index);
}, index);
});
};
return assert;
};
});