mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
assert: Simplify AssertError creation
This commit is contained in:
parent
9f65b1edf7
commit
4716dc662d
1 changed files with 10 additions and 14 deletions
|
@ -38,16 +38,14 @@ var assert = module.exports = ok;
|
||||||
// expected: expected })
|
// expected: expected })
|
||||||
|
|
||||||
assert.AssertionError = function AssertionError(options) {
|
assert.AssertionError = function AssertionError(options) {
|
||||||
this.name = 'AssertionError';
|
|
||||||
this.message = options.message;
|
this.message = options.message;
|
||||||
this.actual = options.actual;
|
this.actual = options.actual;
|
||||||
this.expected = options.expected;
|
this.expected = options.expected;
|
||||||
this.operator = options.operator;
|
this.operator = options.operator;
|
||||||
var stackStartFunction = options.stackStartFunction || fail;
|
var stackStartFunction = options.stackStartFunction || fail;
|
||||||
|
|
||||||
if (Error.captureStackTrace) {
|
this.name = getName(this, options.message);
|
||||||
Error.captureStackTrace(this, stackStartFunction);
|
Error.captureStackTrace(this, stackStartFunction);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// assert.AssertionError instanceof Error
|
// assert.AssertionError instanceof Error
|
||||||
|
@ -74,18 +72,16 @@ function truncate(s, n) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.AssertionError.prototype.toString = function() {
|
function getName(self, message) {
|
||||||
if (this.message) {
|
if (message) {
|
||||||
return [this.name + ':', this.message].join(' ');
|
return 'AssertionError: ' + message;
|
||||||
} else {
|
} else {
|
||||||
return [
|
return 'AssertionError: ' +
|
||||||
this.name + ':',
|
truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
|
||||||
truncate(JSON.stringify(this.actual, replacer), 128),
|
self.operator + ' ' +
|
||||||
this.operator,
|
truncate(JSON.stringify(self.expected, replacer), 128);
|
||||||
truncate(JSON.stringify(this.expected, replacer), 128)
|
|
||||||
].join(' ');
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// At present only the three keys mentioned above are used and
|
// At present only the three keys mentioned above are used and
|
||||||
// understood by the spec. Implementations or sub modules can pass
|
// understood by the spec. Implementations or sub modules can pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue