unexpected
Advanced tools
Comparing version 10.4.0 to 10.5.0
@@ -10,2 +10,6 @@ var createStandardErrorMessage = require('./createStandardErrorMessage'); | ||
function isAssertionArg(arg) { | ||
return arg.type.is('assertion'); | ||
} | ||
module.exports = function createWrappedExpectProto(unexpected) { | ||
@@ -87,3 +91,3 @@ var wrappedExpectProto = { | ||
for (var i = 0 ; i < this.assertionRule.args.length ; i += 1) { | ||
if (this.assertionRule.args[i].isAssertion) { | ||
if (this.assertionRule.args[i].type.is('assertion')) { | ||
assertionIndex = i; | ||
@@ -167,3 +171,3 @@ break; | ||
OUTER: while (true) { | ||
if (currentAssertionRule.args.length > 1 && currentAssertionRule.args[currentAssertionRule.args.length - 2].isAssertion) { | ||
if (currentAssertionRule.args.length > 1 && isAssertionArg(currentAssertionRule.args[currentAssertionRule.args.length - 2])) { | ||
assertionIndices.push(offset + currentAssertionRule.args.length - 2); | ||
@@ -173,3 +177,3 @@ var assertions = unexpected.assertions[args[offset + currentAssertionRule.args.length - 2]]; | ||
for (var i = 0 ; i < assertions.length ; i += 1) { | ||
if (assertions[i].args.some(function (a) { return a.isAssertion; })) { | ||
if (assertions[i].args.some(isAssertionArg)) { | ||
offset += currentAssertionRule.args.length - 1; | ||
@@ -176,0 +180,0 @@ currentAssertionRule = assertions[i]; |
@@ -8,2 +8,3 @@ var utils = require('./utils'); | ||
var defaultDepth = require('./defaultDepth'); | ||
var AssertionString = require('./AssertionString'); | ||
@@ -975,2 +976,9 @@ module.exports = function (expect) { | ||
}); | ||
expect.addType({ | ||
name: 'assertion', | ||
identify: function (value) { | ||
return value instanceof AssertionString; | ||
} | ||
}); | ||
}; |
@@ -15,5 +15,8 @@ var createStandardErrorMessage = require('./createStandardErrorMessage'); | ||
var AssertionString = require('./AssertionString'); | ||
var AssertionStringType = require('./AssertionStringType'); | ||
var throwIfNonUnexpectedError = require('./throwIfNonUnexpectedError'); | ||
function isAssertionArg(arg) { | ||
return arg.type.is('assertion'); | ||
} | ||
var anyType = { | ||
@@ -302,5 +305,5 @@ _unexpectedType: true, | ||
}); | ||
} else if (arg.type === AssertionStringType) { | ||
} else if (arg.type.is('assertion')) { | ||
result.push([ | ||
{ type: AssertionStringType, minimum: 1, maximum: 1, isAssertion: true }, | ||
{ type: arg.type, minimum: 1, maximum: 1 }, | ||
{ type: that.typeByName['any'], minimum: 0, maximum: Infinity } | ||
@@ -345,5 +348,2 @@ ]); | ||
function lookupType(typeName) { | ||
if (typeName === 'assertion') { | ||
return AssertionStringType; | ||
} | ||
var result = that.typeByName[typeName]; | ||
@@ -377,3 +377,2 @@ if (!result) { | ||
assertionString.replace(/\s*<((?:[a-z_](?:|[a-z0-9_.-]*[_a-z0-9])[?*+]?)(?:\|(?:[a-z_](?:|[a-z0-9_.-]*[_a-z0-9])[?*+]?))*)>|\s*([^<]+)/ig, function ($0, $1, $2, index) { | ||
if (index !== nextIndex) { | ||
@@ -419,3 +418,3 @@ throw new SyntaxError('Cannot parse token at index ' + nextIndex + ' in ' + assertionString); | ||
return argRequirements.some(function (argRequirement) { | ||
return argRequirement.type === AssertionStringType; | ||
return argRequirement.type.is('assertion'); | ||
}); | ||
@@ -428,3 +427,3 @@ })) { | ||
var assertionRequirements = lastArgRequirements.filter(function (argRequirement) { | ||
return argRequirement.type === AssertionStringType; | ||
return argRequirement.type.is('assertion'); | ||
}); | ||
@@ -632,3 +631,3 @@ | ||
if (this.getType(type.name) || type.name === 'assertion') { | ||
if (this.getType(type.name)) { | ||
throw new Error('The type with the name ' + type.name + ' already exists'); | ||
@@ -839,2 +838,27 @@ } | ||
Unexpected.prototype.throwAssertionNotFoundError = function (subject, testDescriptionString, args) { | ||
var candidateHandlers = this.assertions[testDescriptionString]; | ||
if (candidateHandlers) { | ||
this.fail({ | ||
errorMode: 'bubbleThrough', | ||
message: function (output) { | ||
var subjectOutput = function (output) { | ||
output.appendInspected(subject); | ||
}; | ||
var argsOutput = function (output) { | ||
output.appendItems(args, ', '); | ||
}; | ||
output.append(createStandardErrorMessage(output.clone(), subjectOutput, testDescriptionString, argsOutput)).nl() | ||
.indentLines(); | ||
output.i().error('No matching assertion, did you mean:').nl(); | ||
var assertionDeclarations = Object.keys(candidateHandlers.reduce(function (result, handler) { | ||
result[handler.declaration] = true; | ||
return result; | ||
}, {})).sort(); | ||
assertionDeclarations.forEach(function (declaration, i) { | ||
output.nl(i > 0 ? 1 : 0).i().text(declaration); | ||
}); | ||
} | ||
}); | ||
} | ||
var assertionsWithScore = []; | ||
@@ -844,13 +868,5 @@ var assertionStrings = Object.keys(this.assertions); | ||
function lookup(assertionString) { | ||
try { | ||
return that.lookupAssertionRule(subject, assertionString, args); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
function compareAssertions(a, b) { | ||
var aAssertion = lookup(a); | ||
var bAssertion = lookup(b); | ||
var aAssertion = that.lookupAssertionRule(subject, a, args); | ||
var bAssertion = that.lookupAssertionRule(subject, b, args); | ||
if (!aAssertion && !bAssertion) { | ||
@@ -910,3 +926,3 @@ return 0; | ||
Unexpected.prototype.lookupAssertionRule = function (subject, testDescriptionString, args) { | ||
Unexpected.prototype.lookupAssertionRule = function (subject, testDescriptionString, args, requireAssertionSuffix) { | ||
var that = this; | ||
@@ -916,6 +932,5 @@ if (typeof testDescriptionString !== 'string') { | ||
} | ||
var handlers = this.assertions[testDescriptionString]; | ||
if (!handlers) { | ||
this.throwAssertionNotFoundError(subject, testDescriptionString, args); | ||
return null; | ||
} | ||
@@ -934,3 +949,3 @@ var cachedTypes = {}; | ||
function matches(value, assertionType, key, relaxed) { | ||
if (assertionType === AssertionStringType && typeof value === 'string') { | ||
if (assertionType.is('assertion') && typeof value === 'string') { | ||
return true; | ||
@@ -955,2 +970,5 @@ } | ||
} | ||
if (requireAssertionSuffix && !handler.args.some(isAssertionArg)) { | ||
return false; | ||
} | ||
@@ -989,23 +1007,3 @@ var requireArgumentsLength = calculateLimits(handler.args); | ||
that.fail({ | ||
errorMode: 'bubbleThrough', | ||
message: function (output) { | ||
var subjectOutput = function (output) { | ||
output.appendInspected(subject); | ||
}; | ||
var argsOutput = function (output) { | ||
output.appendItems(args, ', '); | ||
}; | ||
output.append(createStandardErrorMessage(output.clone(), subjectOutput, testDescriptionString, argsOutput)).nl() | ||
.indentLines(); | ||
output.i().error('No matching assertion, did you mean:').nl(); | ||
var assertionDeclarations = Object.keys(handlers.reduce(function (result, handler) { | ||
result[handler.declaration] = true; | ||
return result; | ||
}, {})).sort(); | ||
assertionDeclarations.forEach(function (declaration, i) { | ||
output.nl(i > 0 ? 1 : 0).i().text(declaration); | ||
}); | ||
} | ||
}); | ||
return null; | ||
}; | ||
@@ -1036,2 +1034,21 @@ | ||
var assertionRule = that.lookupAssertionRule(subject, testDescriptionString, args); | ||
if (!assertionRule) { | ||
var tokens = testDescriptionString.split(' '); | ||
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); | ||
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; | ||
} | ||
} | ||
if (!assertionRule) { | ||
that.throwAssertionNotFoundError(subject, testDescriptionString, args); | ||
} | ||
} | ||
var flags = extend({}, assertionRule.flags); | ||
@@ -1069,3 +1086,3 @@ var wrappedExpect = function () { | ||
var argRule = wrappedExpect.assertionRule.args[i]; | ||
if (typeof arg === 'string' && (argRule && (argRule.type === AssertionStringType) || wrappedExpect._getAssertionIndices().indexOf(i) >= 0)) { | ||
if (typeof arg === 'string' && (argRule && argRule.type.is('assertion') || wrappedExpect._getAssertionIndices().indexOf(i) >= 0)) { | ||
return new AssertionString(arg); | ||
@@ -1072,0 +1089,0 @@ } |
{ | ||
"name": "unexpected", | ||
"version": "10.4.0", | ||
"version": "10.5.0", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -48,4 +48,5 @@ "keywords": [ | ||
"serve": "*", | ||
"unexpected-documentation-site-generator": "^3.2.0" | ||
"unexpected-documentation-site-generator": "^4.0.0", | ||
"unexpected-markdown": "1.3.0" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
724440
18268
18
31