unexpected
Advanced tools
Comparing version 5.0.0-beta14 to 5.0.0-beta15
@@ -90,2 +90,3 @@ var utils = require('./utils'); | ||
expect.addAssertion('[not] to match', function (expect, subject, regexp) { | ||
subject = String(subject); | ||
try { | ||
@@ -429,3 +430,3 @@ expect(String(subject).match(regexp), '[not] to be truthy'); | ||
this.errorMode = 'bubble'; | ||
expect.apply(expect, [subject, 'to be an map whose values satisfy'].concat(extraArgs)); | ||
expect.apply(expect, [subject, 'to be a map whose values satisfy'].concat(extraArgs)); | ||
}); | ||
@@ -432,0 +433,0 @@ |
@@ -602,4 +602,12 @@ var utils = require('./utils'); | ||
output.text('expect.it('); | ||
var orBranch = false; | ||
f._expectations.forEach(function (expectation, index) { | ||
if (0 < index) { | ||
if (expectation === f._OR) { | ||
orBranch = true; | ||
return; | ||
} | ||
if (orBranch) { | ||
output.text(')\n .or('); | ||
} else if (0 < index) { | ||
output.text(')\n .and('); | ||
@@ -615,2 +623,3 @@ } | ||
}); | ||
orBranch = false; | ||
}); | ||
@@ -617,0 +626,0 @@ |
@@ -50,52 +50,102 @@ /*global window*/ | ||
function createExpectIt(expect, expectations) { | ||
function expectIt(subject) { | ||
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; | ||
var OR = {}; | ||
function getOrGroups(expectations) { | ||
var orGroups = [[]]; | ||
expectations.forEach(function (expectation) { | ||
if (expectation === OR) { | ||
orGroups.push([]); | ||
} else { | ||
orGroups[orGroups.length - 1].push(expectation); | ||
} | ||
}); | ||
return orGroups; | ||
} | ||
function evaluateGroup(expect, subject, orGroup) { | ||
var failedGroup = false; | ||
return orGroup.map(function (expectation) { | ||
var args = Array.prototype.slice.call(expectation); | ||
args.unshift(subject); | ||
var evaluation = { expectation: args }; | ||
if (!failedGroup) { | ||
evaluation.evaluated = true; | ||
try { | ||
expect.apply(expect, args); | ||
} catch (e) { | ||
if (!e._isUnexpected) { | ||
throw e; | ||
} | ||
failedGroup = true; | ||
evaluation.failure = e; | ||
} | ||
} | ||
return evaluation; | ||
return evaluation; | ||
}); | ||
} | ||
function writeGroupEvaluationsToOutput(expect, output, groupEvaluations) { | ||
var hasOrClauses = groupEvaluations.length > 1; | ||
var hasAndClauses = groupEvaluations.some(function (groupEvaluation) { | ||
return groupEvaluation.length > 1; | ||
}); | ||
groupEvaluations.forEach(function (groupEvaluation, i) { | ||
if (i > 0) { | ||
if (hasAndClauses) { | ||
output.nl(); | ||
} else { | ||
output.sp(); | ||
} | ||
output.comment('or').nl(); | ||
} | ||
groupEvaluation.forEach(function (evaluation, j) { | ||
if (j > 0) { | ||
output.comment(' and').nl(); | ||
} | ||
if (evaluation.failure) { | ||
if (hasAndClauses || hasOrClauses) { | ||
output.error('⨯ '); | ||
} | ||
output.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)); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
if (failed) { | ||
expect.fail(function (output) { | ||
evaluations.forEach(function (evaluation, index) { | ||
if (index > 0) { | ||
output.comment(' and').nl(); | ||
} | ||
function createExpectIt(expect, expectations) { | ||
var orGroups = getOrGroups(expectations); | ||
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); | ||
} | ||
function expectIt(subject) { | ||
var groupEvaluations = orGroups.map(function (orGroup) { | ||
return evaluateGroup(expect, subject, orGroup); | ||
}); | ||
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)); | ||
}); | ||
} | ||
}); | ||
var failed = groupEvaluations.every(function (groupEvaluation) { | ||
return groupEvaluation.some(function (evaluation) { | ||
return evaluation.failure; | ||
}); | ||
}); | ||
if (failed) { | ||
expect.fail(function (output) { | ||
writeGroupEvaluationsToOutput(expect, output, groupEvaluations); | ||
}); | ||
} | ||
@@ -105,2 +155,3 @@ } | ||
expectIt._expectations = expectations; | ||
expectIt._OR = OR; | ||
expectIt.and = function () { | ||
@@ -111,2 +162,7 @@ var copiedExpectations = expectations.slice(); | ||
}; | ||
expectIt.or = function () { | ||
var copiedExpectations = expectations.slice(); | ||
copiedExpectations.push(OR, arguments); | ||
return createExpectIt(expect, copiedExpectations); | ||
}; | ||
return expectIt; | ||
@@ -113,0 +169,0 @@ } |
{ | ||
"name": "unexpected", | ||
"version": "5.0.0-beta14", | ||
"version": "5.0.0-beta15", | ||
"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
1219690
15585