Skip to content

Commit

Permalink
Merge ae3c0c6 into ac3e9d3
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed May 7, 2017
2 parents ac3e9d3 + ae3c0c6 commit 155eb14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,15 +1126,22 @@ Unexpected.prototype._expect = function expect(subject, testDescriptionString) {

if (!assertionRule) {
var tokens = testDescriptionString.split(' ');
for (var n = tokens.length - 1; n > 0 ; n -= 1) {
OUTER: for (var n = tokens.length - 1; n > 0 ; n -= 1) {
var prefix = tokens.slice(0, n).join(' ');
var argsWithAssertionPrepended = [ tokens.slice(n).join(' ') ].concat(args);
var remainingTokens = tokens.slice(n);
var argsWithAssertionPrepended = [ remainingTokens.join(' ') ].concat(args);
assertionRule = that.lookupAssertionRule(subject, prefix, argsWithAssertionPrepended, true);
if (assertionRule) {
// Great, found the longest prefix of the string that yielded a suitable assertion for the given subject and args
testDescriptionString = prefix;
args = argsWithAssertionPrepended;
break;
// Found the longest prefix of the string that yielded a suitable assertion for the given subject and args
// To avoid bogus error messages when shifting later (#394) we require some prefix of the remaining tokens
// to be a valid assertion name:
for (var i = 1 ; i < remainingTokens.length ; i += 1) {
if (that.assertions.hasOwnProperty(remainingTokens.slice(0, i + 1).join(' '))) {
testDescriptionString = prefix;
args = argsWithAssertionPrepended;
break OUTER;
}
}
}
}
if (!assertionRule) {
Expand Down
14 changes: 14 additions & 0 deletions test/unexpected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,20 @@ describe('unexpected', function () {
);
});
});

// https://github.com/unexpectedjs/unexpected/issues/394
it('should produce a meaningful error message when the last half of a compound assertion is not a valid assertion name', function () {
expect(function () {
expect(function () {}, 'when called with', 'M1', 'to throw a', SyntaxError);
}, 'to throw',
"expected function () {}\n" +
"when called with 'M1', 'to throw a', function SyntaxError() { /* native code */ }\n" +
" The assertion does not have a matching signature for:\n" +
" <function> when called with <string> <string> <function>\n" +
" did you mean:\n" +
" <function> [when] called with <array-like> <assertion?>"
);
});
});

describe('#output', function () {
Expand Down

0 comments on commit 155eb14

Please sign in to comment.