org.eclipse.n4js.mangelhaft.assert
Version:
An assert library used with the Mangelhaft testing framework.
88 lines (85 loc) • 2.48 kB
JavaScript
// Generated by N4JS transpiler; for copyright see original N4JS source file.
import 'n4js-runtime'
function replacer(key, value) {
if (value === undefined) {
return '' + value;
}
if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) {
return (value).toString();
}
if (typeof value === 'function' || value instanceof RegExp) {
return (value).toString();
}
return value;
}
function truncate(s, n) {
if (typeof s == 'string') {
return s.length < n ? s : s.slice(0, n);
} else {
return s;
}
}
export class AssertionError extends Error {
constructor(spec) {
spec = spec || {};
let stackHolder = {
stack: ""
}, msg = spec.message ? spec.message : "";
try {
if (!(spec.actual == null && spec.expected == null && spec.operator == null)) {
msg += ` (${String(spec.actual)} not ${String(spec.operator)} ${String(spec.expected)} )`;
}
} catch(e) {}
super(msg);
this.name = this.constructor.n4type.name;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.name);
}
this.actual = undefined;
this.expected = undefined;
this.operator = undefined;
this.stackStartFunction = undefined;
this.name = "AssertionError";
const Er = Error;
if (!spec.stack) {
if (Er.captureStackTrace && spec.stackStartFunction) {
Er.captureStackTrace(stackHolder, spec.stackStartFunction);
} else {
try {
let err = new Error();
stackHolder.stack = err.stack.toString();
} catch(e) {}
}
}
if (typeof stackHolder.stack === "string") {
spec.stack = stackHolder.stack.split("\n");
}
this.stack = spec.stack.join("\n");
this.actual = spec.actual;
this.expected = spec.expected;
this.operator = spec.operator;
this.stackStartFunction = spec.stackStartFunction;
}
toString() {
let str = "";
try {
str += this.name + ": ";
if (this.message) {
str += this.message;
} else {
str += [
truncate(JSON.stringify(this.actual, replacer), 128),
this.operator,
truncate(JSON.stringify(this.expected, replacer), 128)
].join(' ');
}
} catch(ex) {
console.log("could not provide helpful assertion error message because of error", ex);
}
return str;
}
static get n4type() {
return $getReflectionForClass(this, '["AssertionError","org/eclipse/n4js/mangelhaft/assert/AssertionError","org.eclipse.n4js.mangelhaft.assert",["f.actual","f.expected","f.operator","f.stackStartFunction"]]');
}
}
//# sourceMappingURL=AssertionError.map