Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 4716dc6

Browse files
committed
assert: Simplify AssertError creation
1 parent 9f65b1e commit 4716dc6

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

lib/assert.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ var assert = module.exports = ok;
3838
// expected: expected })
3939

4040
assert.AssertionError = function AssertionError(options) {
41-
this.name = 'AssertionError';
4241
this.message = options.message;
4342
this.actual = options.actual;
4443
this.expected = options.expected;
4544
this.operator = options.operator;
4645
var stackStartFunction = options.stackStartFunction || fail;
4746

48-
if (Error.captureStackTrace) {
49-
Error.captureStackTrace(this, stackStartFunction);
50-
}
47+
this.name = getName(this, options.message);
48+
Error.captureStackTrace(this, stackStartFunction);
5149
};
5250

5351
// assert.AssertionError instanceof Error
@@ -74,18 +72,16 @@ function truncate(s, n) {
7472
}
7573
}
7674

77-
assert.AssertionError.prototype.toString = function() {
78-
if (this.message) {
79-
return [this.name + ':', this.message].join(' ');
75+
function getName(self, message) {
76+
if (message) {
77+
return 'AssertionError: ' + message;
8078
} else {
81-
return [
82-
this.name + ':',
83-
truncate(JSON.stringify(this.actual, replacer), 128),
84-
this.operator,
85-
truncate(JSON.stringify(this.expected, replacer), 128)
86-
].join(' ');
79+
return 'AssertionError: ' +
80+
truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
81+
self.operator + ' ' +
82+
truncate(JSON.stringify(self.expected, replacer), 128);
8783
}
88-
};
84+
}
8985

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

0 commit comments

Comments
 (0)