Browse Source

Improvements AssertionError message

Fixes #217.
v0.7.4-release
koichik 14 years ago
parent
commit
5f97c9a005
  1. 16
      lib/assert.js
  2. 31
      test/simple/test-assert.js

16
lib/assert.js

@ -56,9 +56,21 @@ assert.AssertionError.prototype.toString = function() {
return [this.name + ':', this.message].join(' ');
} else {
return [this.name + ':',
JSON.stringify(this.expected),
JSON.stringify(this.expected, replacer),
this.operator,
JSON.stringify(this.actual)].join(' ');
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;
}
};

31
test/simple/test-assert.js

@ -229,3 +229,34 @@ try {
console.log('All OK');
assert.ok(gotError);
// #217
function testAssertionMessage(actual, expected) {
try {
assert.equal(actual, '');
} catch (e) {
assert.equal(e.toString(),
['AssertionError:', '""', '==', expected].join(' '));
}
}
testAssertionMessage(undefined, '"undefined"');
testAssertionMessage(null, 'null');
testAssertionMessage(true, 'true');
testAssertionMessage(false, 'false');
testAssertionMessage(0, '0');
testAssertionMessage(100, '100');
testAssertionMessage(NaN, '"NaN"');
testAssertionMessage(Infinity, '"Infinity"');
testAssertionMessage(-Infinity, '"-Infinity"');
testAssertionMessage('', '""');
testAssertionMessage('foo', '"foo"');
testAssertionMessage([], '[]');
testAssertionMessage([1,2,3], '[1,2,3]');
testAssertionMessage(/a/, '"/a/"');
testAssertionMessage(/abc/gim, '"/abc/gim"');
testAssertionMessage(function f() {}, '"function f() {}"');
testAssertionMessage({}, '{}');
testAssertionMessage({a:undefined, b:null}, '{"a":"undefined","b":null}');
testAssertionMessage({a:NaN, b:Infinity, c:-Infinity},
'{"a":"NaN","b":"Infinity","c":"-Infinity"}');

Loading…
Cancel
Save