Skip to content

Commit

Permalink
assert: Simplify AssertError creation
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 3, 2013
1 parent 9f65b1e commit 4716dc6
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ var assert = module.exports = ok;
// expected: expected })

assert.AssertionError = function AssertionError(options) {
this.name = 'AssertionError';
this.message = options.message;
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
var stackStartFunction = options.stackStartFunction || fail;

if (Error.captureStackTrace) {
Error.captureStackTrace(this, stackStartFunction);
}
this.name = getName(this, options.message);
Error.captureStackTrace(this, stackStartFunction);
};

// assert.AssertionError instanceof Error
Expand All @@ -74,18 +72,16 @@ function truncate(s, n) {
}
}

assert.AssertionError.prototype.toString = function() {
if (this.message) {
return [this.name + ':', this.message].join(' ');
function getName(self, message) {
if (message) {
return 'AssertionError: ' + message;
} else {
return [
this.name + ':',
truncate(JSON.stringify(this.actual, replacer), 128),
this.operator,
truncate(JSON.stringify(this.expected, replacer), 128)
].join(' ');
return 'AssertionError: ' +
truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
self.operator + ' ' +
truncate(JSON.stringify(self.expected, replacer), 128);
}
};
}

// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
Expand Down

0 comments on commit 4716dc6

Please sign in to comment.