You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/assert/assertions.js

99 lines
3.3 KiB
JavaScript

define([], function () {
return function () {
var failMessages = [];
var passed = 0;
var ASSERTS = [];
var MESSAGES = [];
var assert = function (test, msg) {
MESSAGES.push(msg || false);
ASSERTS.push(function (_cb, i) {
var called = false;
var to;
var cb = function (arg) {
if (to) {
clearTimeout(to);
to = undefined;
}
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;
};
});