-
-
Save isaacs/5302979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From df93050b6b3843097e624066ca626a7c3f2a0d9d Mon Sep 17 00:00:00 2001 | |
From: isaacs <i@izs.me> | |
Date: Wed, 3 Apr 2013 09:43:17 -0700 | |
Subject: [PATCH] assert: Simplify AssertError creation | |
--- | |
lib/assert.js | 22 +++++++++------------- | |
1 files changed, 9 insertions(+), 13 deletions(-) | |
diff --git a/lib/assert.js b/lib/assert.js | |
index a2afdcf..0b5e626 100644 | |
--- a/lib/assert.js | |
+++ b/lib/assert.js | |
@@ -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 | |
@@ -74,16 +72,14 @@ 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); | |
} | |
}; | |
-- | |
1.7.5.4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment