diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2011-08-09 14:18:16 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2011-08-09 14:20:06 -0700 |
commit | 0696e78d6436c3a30803fe0e2bea42781691dc01 (patch) | |
tree | f8399666c707ffa694234261bac15db6ae2b5008 | |
parent | fd1fc5080f6526e28c090b446400924ba08a1ae0 (diff) | |
download | nodejs-0696e78d6436c3a30803fe0e2bea42781691dc01.tar.gz nodejs-0696e78d6436c3a30803fe0e2bea42781691dc01.tar.bz2 nodejs-0696e78d6436c3a30803fe0e2bea42781691dc01.zip |
Improve assert error messages
1. actual and expected should be displayed in the same order they were given
2. long values should be truncated.
-rw-r--r-- | lib/assert.js | 45 | ||||
-rw-r--r-- | test/simple/test-assert.js | 2 |
2 files changed, 29 insertions, 18 deletions
diff --git a/lib/assert.js b/lib/assert.js index 9ca871da3..3f35b4510 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -51,26 +51,37 @@ assert.AssertionError = function AssertionError(options) { }; util.inherits(assert.AssertionError, Error); +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; + } +} + assert.AssertionError.prototype.toString = function() { if (this.message) { - return [this.name + ':', this.message].join(' '); + return [ this.name + ':', this.message ].join(' '); } else { - return [this.name + ':', - JSON.stringify(this.expected, replacer), - this.operator, - JSON.stringify(this.actual, replacer)].join(' '); - } - 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; + return [ + this.name + ':', + truncate(JSON.stringify(this.actual, replacer), 128), + this.operator, + truncate(JSON.stringify(this.expected, replacer), 128) + ].join(' '); } }; diff --git a/test/simple/test-assert.js b/test/simple/test-assert.js index b47f04545..f0e78b85f 100644 --- a/test/simple/test-assert.js +++ b/test/simple/test-assert.js @@ -236,7 +236,7 @@ function testAssertionMessage(actual, expected) { assert.equal(actual, ''); } catch (e) { assert.equal(e.toString(), - ['AssertionError:', '""', '==', expected].join(' ')); + ['AssertionError:', expected, '==', '""'].join(' ')); } } testAssertionMessage(undefined, '"undefined"'); |