unexpected
Advanced tools
@@ -93,19 +93,22 @@ var utils = require('./utils'); | ||
} catch (e) { | ||
if (e._isUnexpected && this.flags.not) { | ||
e.createDiff = function (output) { | ||
var lastIndex = 0; | ||
function flushUntilIndex(i) { | ||
if (i > lastIndex) { | ||
output.text(subject.substring(lastIndex, i)); | ||
lastIndex = i; | ||
if (e._isUnexpected) { | ||
e.label = 'should match'; | ||
if (this.flags.not) { | ||
e.createDiff = function (output) { | ||
var lastIndex = 0; | ||
function flushUntilIndex(i) { | ||
if (i > lastIndex) { | ||
output.text(subject.substring(lastIndex, i)); | ||
lastIndex = i; | ||
} | ||
} | ||
} | ||
subject.replace(new RegExp(regexp.source, 'g'), function ($0, index) { | ||
flushUntilIndex(index); | ||
lastIndex += $0.length; | ||
output.diffRemovedHighlight($0); | ||
}); | ||
flushUntilIndex(subject.length); | ||
return {diff: output}; | ||
}; | ||
subject.replace(new RegExp(regexp.source, 'g'), function ($0, index) { | ||
flushUntilIndex(index); | ||
lastIndex += $0.length; | ||
output.diffRemovedHighlight($0); | ||
}); | ||
flushUntilIndex(subject.length); | ||
return {diff: output}; | ||
}; | ||
} | ||
} | ||
@@ -305,6 +308,9 @@ expect.fail(e); | ||
} catch (e) { | ||
if (!this.flags.not && e._isUnexpected) { | ||
e.createDiff = function (output, diff) { | ||
return diff(subject, value); | ||
}; | ||
if (e._isUnexpected) { | ||
e.label = 'should equal'; | ||
if (!this.flags.not) { | ||
e.createDiff = function (output, diff) { | ||
return diff(subject, value); | ||
}; | ||
} | ||
} | ||
@@ -542,4 +548,15 @@ expect.fail(e); | ||
expect.apply(expect, Array.prototype.slice.call(arguments, 1)); | ||
} else if (value && value._expectIt) { | ||
try { | ||
value(subject); | ||
} catch (e) { | ||
e.createDiff = function (output, diff, inspect, equal) { | ||
return { | ||
diff: output.append(e.output), | ||
inline: false | ||
}; | ||
}; | ||
expect.fail(e); | ||
} | ||
} else if (typeof value === 'function') { | ||
// FIXME: If expect.it, it should be possible to produce a better error message | ||
value(subject); | ||
@@ -598,4 +615,6 @@ } else if (isRegExp(value)) { | ||
var keyDiff = conflicting.createDiff && conflicting.createDiff(output.clone(), diff, inspect, equal); | ||
if (!keyDiff || (keyDiff && !keyDiff.inline)) { | ||
annotation.error('should satisfy: ') | ||
if (value[key] && value[key]._expectIt) { | ||
annotation.append(keyDiff.diff); | ||
} else if (!keyDiff || (keyDiff && !keyDiff.inline)) { | ||
annotation.error(conflicting.label || 'should satisfy').error(':').sp() | ||
.block(inspect(value[key])); | ||
@@ -631,3 +650,3 @@ | ||
if (!annotation.isEmpty()) { | ||
this.block(annotation.prependLinesWith('error', ' // ')); | ||
this.sp().annotationBlock(annotation); | ||
} | ||
@@ -677,2 +696,3 @@ }).nl(); | ||
} catch (e) { | ||
e.label = 'should satisfy'; | ||
expect.fail(wrapDiffWithTypePrefixAndSuffix(e, subjectType)); | ||
@@ -679,0 +699,0 @@ } |
@@ -5,2 +5,5 @@ module.exports = function (expect) { | ||
}); | ||
expect.output.addStyle('success', function (content) { | ||
this.text(content, 'green', 'bold'); | ||
}); | ||
expect.output.addStyle('strings', function (content) { | ||
@@ -7,0 +10,0 @@ this.text(content, '#00A0A0'); |
@@ -114,3 +114,9 @@ var utils = require('./utils'); | ||
if (hasGetter || !hasSetter) { | ||
propertyOutput.sp().append(inspect(obj[key])); | ||
var value = obj[key]; | ||
var inspectedValue = inspect(value); | ||
if (value && value._expectIt) { | ||
propertyOutput.sp().block(inspectedValue); | ||
} else { | ||
propertyOutput.sp().append(inspectedValue); | ||
} | ||
} | ||
@@ -599,3 +605,3 @@ | ||
if (0 < index) { | ||
output.text(').and('); | ||
output.text(')\n .and('); | ||
} | ||
@@ -602,0 +608,0 @@ |
@@ -52,23 +52,48 @@ /*global window*/ | ||
function expectIt(subject) { | ||
var evaluation = expectations.map(function (expectation) { | ||
try { | ||
var args = Array.prototype.slice.call(expectation); | ||
args.unshift(subject); | ||
expect.apply(expect, args); | ||
return null; | ||
} catch (e) { | ||
return e; | ||
var failed = false; | ||
var evaluations = expectations.map(function (expectation) { | ||
var args = Array.prototype.slice.call(expectation); | ||
args.unshift(subject); | ||
var evaluation = { expectation: args }; | ||
if (!failed) { | ||
evaluation.evaluated = true; | ||
try { | ||
expect.apply(expect, args); | ||
} catch (e) { | ||
if (!e._isUnexpected) { | ||
throw e; | ||
} | ||
failed = true; | ||
evaluation.failure = e; | ||
} | ||
} | ||
}).filter(function (e) { | ||
return e; | ||
return evaluation; | ||
}); | ||
if (evaluation.length > 0) { | ||
if (failed) { | ||
expect.fail(function (output) { | ||
evaluation.forEach(function (failedExpectation, index) { | ||
evaluations.forEach(function (evaluation, index) { | ||
if (index > 0) { | ||
output.text(' and').nl(); | ||
output.comment(' and').nl(); | ||
} | ||
output.append(failedExpectation.output); | ||
if (evaluation.failure) { | ||
output.error('⨯ ').append(evaluation.failure.output); | ||
} else { | ||
var style = evaluation.evaluated ? 'success' : 'text'; | ||
var expectation = evaluation.expectation; | ||
if (evaluation.evaluated) { | ||
output.success('✓ '); | ||
} else { | ||
output.sp(2); | ||
} | ||
output[style]('expected '); | ||
output.text(expect.inspect(expectation[0])).sp(); | ||
output[style](expectation[1]); | ||
expectation.slice(2).forEach(function (v) { | ||
output.sp().append(expect.inspect(v)); | ||
}); | ||
} | ||
}); | ||
@@ -305,3 +330,13 @@ }); | ||
function handleNestedExpects(e, assertion) { | ||
function buildDiff(expect, err) { | ||
return err.createDiff && err.createDiff(expect.output.clone(), function (actual, expected) { | ||
return expect.diff(actual, expected); | ||
}, function (v, depth) { | ||
return expect.inspect(v, depth || Infinity); | ||
}, function (actual, expected) { | ||
return expect.equal(actual, expected); | ||
}); | ||
} | ||
function handleNestedExpects(expect, e, assertion) { | ||
switch (assertion.errorMode) { | ||
@@ -318,2 +353,13 @@ case 'nested': | ||
return errorWithMessage(e, e.output); | ||
case 'diff': | ||
return errorWithMessage(e, e.output.clone().append(function (output) { | ||
var comparison = buildDiff(expect, e); | ||
delete e.createDiff; | ||
if (comparison && comparison.diff) { | ||
output.append(comparison.diff); | ||
} else { | ||
output.append(e.output); | ||
} | ||
})); | ||
default: | ||
@@ -353,14 +399,5 @@ throw new Error("Unknown error mode: '" + assertion.errorMode + "'"); | ||
if (err.createDiff) { | ||
var that = this; | ||
var comparison = err.createDiff(message.clone(), function (actual, expected) { | ||
return that.diff(actual, expected); | ||
}, function (v, depth) { | ||
return that.inspect(v, depth || Infinity); | ||
}, function (actual, expected) { | ||
return that.equal(actual, expected); | ||
}); | ||
if (comparison) { | ||
message.nl(2).blue('Diff:').nl(2).append(comparison.diff); | ||
} | ||
var comparison = buildDiff(this.expect, err); | ||
if (comparison) { | ||
message.nl(2).append(comparison.diff); | ||
} | ||
@@ -406,3 +443,3 @@ | ||
if (nestingLevel === 0) { | ||
var wrappedError = handleNestedExpects(e, assertion); | ||
var wrappedError = handleNestedExpects(wrappedExpect, e, assertion); | ||
that.setErrorMessage(wrappedError); | ||
@@ -409,0 +446,0 @@ throw wrappedError; |
{ | ||
"name": "unexpected", | ||
"version": "5.0.0-beta12", | ||
"version": "5.0.0-beta13", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1211922
0.17%15380
0.07%