Skip to content

Commit

Permalink
Merge pull request #139 from unexpectedjs/issue132
Browse files Browse the repository at this point in the history
Fix #132
  • Loading branch information
papandreou committed Apr 5, 2015
2 parents a295c97 + 3cc4e7d commit 3068c1f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
16 changes: 11 additions & 5 deletions lib/assertions.js
Expand Up @@ -672,16 +672,18 @@ module.exports = function (expect) {
}
conflicting = e;
}
var arrayItemOutOfRange = bothAreArrays && (index >= subject.length || index >= value.length);

var isInlineDiff = true;
if (conflicting) {
if (conflicting || arrayItemOutOfRange) {
if (!(key in value)) {
if (type.is('array-like') || flags.exhaustively) {
annotation.error('should be removed');
} else {
conflicting = null;
}
} else {
var keyDiff = conflicting.createDiff && conflicting.createDiff(output.clone(), diff, inspect, equal);
var keyDiff = conflicting && conflicting.createDiff && conflicting.createDiff(output.clone(), diff, inspect, equal);
isInlineDiff = !keyDiff || keyDiff.inline ;
if (value[key] && value[key]._expectIt) {
if (keyDiff && keyDiff.diff) {
Expand All @@ -695,7 +697,7 @@ module.exports = function (expect) {
}
}
} else if (!keyDiff || (keyDiff && !keyDiff.inline)) {
annotation.error(conflicting.label || 'should satisfy').sp()
annotation.error((conflicting && conflicting.label) || 'should satisfy').sp()
.block(inspect(value[key]));

if (keyDiff) {
Expand All @@ -709,7 +711,11 @@ module.exports = function (expect) {

var last = index === keys.length - 1;
if (!valueOutput) {
valueOutput = inspect(subject[key], conflicting ? Infinity : 1);
if (bothAreArrays && key >= subject.length) {
valueOutput = output.clone();
} else {
valueOutput = inspect(subject[key], conflicting ? Infinity : 1);
}
}

if (!bothAreArrays) {
Expand All @@ -733,7 +739,7 @@ module.exports = function (expect) {
this.block(valueOutput);
}
if (!annotation.isEmpty()) {
this.sp().annotationBlock(annotation);
this.sp(valueOutput.isEmpty() ? 0 : 1).annotationBlock(annotation);
}
}).nl();
});
Expand Down
26 changes: 25 additions & 1 deletion test/unexpected.spec.js
Expand Up @@ -1703,6 +1703,30 @@ describe('unexpected', function () {
expect([1, 2, 3], 'to satisfy', [1, 2, 3]);
});

it('should produce a diff when an undefined item in the subject is found at a position outside of the value array', function () {
expect(function () {
expect([ undefined ], 'to satisfy', []);
}, 'to throw',
'expected [ undefined ] to satisfy []\n' +
'\n' +
'[\n' +
' undefined // should be removed\n' +
']'
);
});

it('should produce a diff when the value has more items than the subject', function () {
expect(function () {
expect([], 'to satisfy', [ undefined ]);
}, 'to throw',
'expected [] to satisfy [ undefined ]\n' +
'\n' +
'[\n' +
' // should satisfy undefined\n' +
']'
);
});

it('should fail if the value does not include all the indices of the subject', function () {
expect(function () {
expect([1, 2, 3], 'to satisfy', [1, 2]);
Expand All @@ -1726,7 +1750,7 @@ describe('unexpected', function () {
' 1,\n' +
' 2,\n' +
' 3,\n' +
' undefined // should equal 4\n' +
' // should equal 4\n' +
']');
});
});
Expand Down

0 comments on commit 3068c1f

Please sign in to comment.