Skip to content

Commit

Permalink
assert: indicate if exception message is generated
Browse files Browse the repository at this point in the history
AssertionError.generatedMessage is now true when
AssertionError.message was generated from expected and actual

Fixes #5836, #6206
  • Loading branch information
glenjamin authored and tjfontaine committed Oct 11, 2013
1 parent 2b9e3fb commit 66b8c3c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ assert.AssertionError = function AssertionError(options) {
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
this.message = options.message || getMessage(this);
if (options.message) {
this.message = options.message;
this.generatedMessage = false;
} else {
this.message = getMessage(this);
this.generatedMessage = true;
}
var stackStartFunction = options.stackStartFunction || fail;
Error.captureStackTrace(this, stackStartFunction);
};
Expand Down
4 changes: 4 additions & 0 deletions test/simple/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function testAssertionMessage(actual, expected) {
} catch (e) {
assert.equal(e.toString(),
['AssertionError:', expected, '==', '""'].join(' '));
assert.ok(e.generatedMessage, "Message not marked as generated");
}
}
testAssertionMessage(undefined, '"undefined"');
Expand Down Expand Up @@ -299,10 +300,13 @@ try {
assert.equal(1, 2);
} catch (e) {
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2')
assert.ok(e.generatedMessage, 'Message not marked as generated');
}

try {
assert.equal(1, 2, 'oh no');
} catch (e) {
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no')
assert.equal(e.generatedMessage, false,
'Message incorrectly marked as generated');
}

0 comments on commit 66b8c3c

Please sign in to comment.