Skip to content

Commit 917723b

Browse files
committed
Avoid trailing whitespace when the first change is a moveTarget item.
1 parent b9839d8 commit 917723b

File tree

9 files changed

+118
-112
lines changed

9 files changed

+118
-112
lines changed

documentation/assertions/array-like/when-sorted-by.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ expected [ 2, 1, 3 ]
2020
when sorted by function (a, b) { return a - b; } to equal [ 3, 2, 1 ]
2121
2222
[
23-
┌───▷
23+
┌───▷
2424
│ ┌─▷
2525
│ │ 1,
2626
│ └── 2, // should be moved

documentation/assertions/array-like/when-sorted.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ expect(['c', 'a', 'b'], 'when sorted', 'to equal', ['c', 'b', 'a']);
2121
expected [ 'c', 'a', 'b' ] when sorted to equal [ 'c', 'b', 'a' ]
2222
2323
[
24-
┌───▷
24+
┌───▷
2525
│ ┌─▷
2626
│ │ 'a',
2727
│ └── 'b', // should be moved

lib/assertions.js

+54-52
Original file line numberDiff line numberDiff line change
@@ -1047,61 +1047,63 @@ module.exports = function (expect) {
10471047
var packing = utils.packArrows(changes); // NOTE: Will have side effects in changes if the packing results in too many arrow lanes
10481048
output.arrowsAlongsideChangeOutputs(packing, changes.map(function (diffItem, index) {
10491049
var delimiterOutput = subjectType.delimiter(output.clone(), index, indexOfLastNonInsert + 1);
1050-
return output.clone().block(function () {
1051-
var type = diffItem.type;
1052-
if (type === 'moveSource') {
1053-
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
1054-
.amend(delimiterOutput.sp()).error('// should be moved');
1055-
} else if (type === 'moveTarget') {
1056-
// Leave an empty placeholder
1057-
} else if (type === 'insert') {
1058-
this.annotationBlock(function () {
1059-
var index = typeof diffItem.actualIndex !== 'undefined' ? diffItem.actualIndex : diffItem.expectedIndex;
1060-
if (expect.findTypeOf(diffItem.value).is('function')) {
1061-
this.error('missing: ')
1062-
.property(index, output.clone().block(function () {
1063-
this.omitSubject = undefined;
1064-
var promise = keyPromises[diffItem.expectedIndex];
1065-
if (promise.isRejected()) {
1066-
this.appendErrorMessage(promise.reason());
1067-
} else {
1068-
this.appendInspected(diffItem.value);
1069-
}
1070-
}), true);
1071-
} else {
1072-
this.error('missing ').property(index, inspect(diffItem.value), true);
1073-
}
1074-
});
1075-
} else {
1076-
this.property(diffItem.actualIndex, output.clone().block(function () {
1077-
if (type === 'remove') {
1078-
this.append(inspect(diffItem.value).amend(delimiterOutput.sp()).error('// should be removed'));
1079-
} else if (type === 'equal') {
1080-
this.append(inspect(diffItem.value).amend(delimiterOutput));
1081-
} else {
1082-
var toSatisfyResult = toSatisfyMatrix[diffItem.actualIndex][diffItem.expectedIndex];
1083-
var valueDiff = toSatisfyResult && toSatisfyResult !== true && toSatisfyResult.getDiff({ output: output.clone() });
1084-
if (valueDiff && valueDiff.inline) {
1085-
this.append(valueDiff.amend(delimiterOutput));
1050+
var type = diffItem.type;
1051+
if (type === 'moveTarget') {
1052+
return output.clone();
1053+
} else {
1054+
return output.clone().block(function () {
1055+
if (type === 'moveSource') {
1056+
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
1057+
.amend(delimiterOutput.sp()).error('// should be moved');
1058+
} else if (type === 'insert') {
1059+
this.annotationBlock(function () {
1060+
var index = typeof diffItem.actualIndex !== 'undefined' ? diffItem.actualIndex : diffItem.expectedIndex;
1061+
if (expect.findTypeOf(diffItem.value).is('function')) {
1062+
this.error('missing: ')
1063+
.property(index, output.clone().block(function () {
1064+
this.omitSubject = undefined;
1065+
var promise = keyPromises[diffItem.expectedIndex];
1066+
if (promise.isRejected()) {
1067+
this.appendErrorMessage(promise.reason());
1068+
} else {
1069+
this.appendInspected(diffItem.value);
1070+
}
1071+
}), true);
10861072
} else {
1087-
this.append(inspect(diffItem.value).amend(delimiterOutput)).sp().annotationBlock(function () {
1088-
this.omitSubject = diffItem.value;
1089-
var label = toSatisfyResult.getLabel();
1090-
if (label) {
1091-
this.error(label).sp()
1092-
.block(inspect(diffItem.expected));
1093-
if (valueDiff) {
1094-
this.nl(2).append(valueDiff);
1073+
this.error('missing ').property(index, inspect(diffItem.value), true);
1074+
}
1075+
});
1076+
} else {
1077+
this.property(diffItem.actualIndex, output.clone().block(function () {
1078+
if (type === 'remove') {
1079+
this.append(inspect(diffItem.value).amend(delimiterOutput.sp()).error('// should be removed'));
1080+
} else if (type === 'equal') {
1081+
this.append(inspect(diffItem.value).amend(delimiterOutput));
1082+
} else {
1083+
var toSatisfyResult = toSatisfyMatrix[diffItem.actualIndex][diffItem.expectedIndex];
1084+
var valueDiff = toSatisfyResult && toSatisfyResult !== true && toSatisfyResult.getDiff({ output: output.clone() });
1085+
if (valueDiff && valueDiff.inline) {
1086+
this.append(valueDiff.amend(delimiterOutput));
1087+
} else {
1088+
this.append(inspect(diffItem.value).amend(delimiterOutput)).sp().annotationBlock(function () {
1089+
this.omitSubject = diffItem.value;
1090+
var label = toSatisfyResult.getLabel();
1091+
if (label) {
1092+
this.error(label).sp()
1093+
.block(inspect(diffItem.expected));
1094+
if (valueDiff) {
1095+
this.nl(2).append(valueDiff);
1096+
}
1097+
} else {
1098+
this.appendErrorMessage(toSatisfyResult);
10951099
}
1096-
} else {
1097-
this.appendErrorMessage(toSatisfyResult);
1098-
}
1099-
});
1100+
});
1101+
}
11001102
}
1101-
}
1102-
}), true);
1103-
}
1104-
});
1103+
}), true);
1104+
}
1105+
});
1106+
}
11051107
}));
11061108

11071109
if (subjectType.indent) {

lib/styles.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,22 @@ module.exports = function (expect) {
442442
});
443443

444444
if (arrows.length === 1) {
445-
this.block(arrows[0]).sp();
445+
this.block(arrows[0]);
446446
} else if (arrows.length > 1) {
447447
this.block(function () {
448448
this.merge(arrows);
449-
}).sp();
449+
});
450450
}
451451
} else {
452452
this.i();
453453
}
454454

455455
this.block(function () {
456456
changeOutputs.forEach(function (changeOutput, index) {
457-
this.nl(index > 0 ? 1 : 0).i().append(changeOutput);
457+
this.nl(index > 0 ? 1 : 0);
458+
if (!changeOutput.isEmpty()) {
459+
this.sp(packing ? 1 : 0).append(changeOutput);
460+
}
458461
}, this);
459462
});
460463
});

lib/types.js

+43-42
Original file line numberDiff line numberDiff line change
@@ -560,49 +560,50 @@ module.exports = function (expect) {
560560
var packing = utils.packArrows(changes); // NOTE: Will have side effects in changes if the packing results in too many arrow lanes
561561
output.arrowsAlongsideChangeOutputs(packing, changes.map(function (diffItem, index) {
562562
var delimiterOutput = type.delimiter(output.clone(), index, indexOfLastNonInsert + 1);
563-
return output.clone().block(function () {
564-
var type = diffItem.type;
565-
if (type === 'moveSource') {
566-
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
567-
.amend(delimiterOutput.sp()).error('// should be moved');
568-
} else if (type === 'moveTarget') {
569-
// Leave an empty placeholder
570-
} else if (type === 'insert') {
571-
this.annotationBlock(function () {
572-
this.error('missing ').block(function () {
573-
var index = typeof diffItem.actualIndex !== 'undefined' ? diffItem.actualIndex : diffItem.expectedIndex;
574-
this.property(index, inspect(diffItem.value), true);
575-
});
576-
});
577-
} else if (type === 'remove') {
578-
this.block(function () {
579-
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
580-
.amend(delimiterOutput.sp()).error('// should be removed');
581-
});
582-
} else if (type === 'equal') {
583-
this.block(function () {
563+
if (diffItem.type === 'moveTarget') {
564+
return output.clone();
565+
} else {
566+
return output.clone().block(function () {
567+
if (diffItem.type === 'moveSource') {
584568
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
585-
.amend(delimiterOutput);
586-
});
587-
} else {
588-
this.block(function () {
589-
var valueDiff = diff(diffItem.value, diffItem.expected);
590-
this.property(diffItem.actualIndex, output.clone().block(function () {
591-
if (valueDiff && valueDiff.inline) {
592-
this.append(valueDiff.amend(delimiterOutput));
593-
} else if (valueDiff) {
594-
this.append(inspect(diffItem.value).amend(delimiterOutput.sp())).annotationBlock(function () {
595-
this.shouldEqualError(diffItem.expected, inspect).nl(2).append(valueDiff);
596-
});
597-
} else {
598-
this.append(inspect(diffItem.value).amend(delimiterOutput.sp())).annotationBlock(function () {
599-
this.shouldEqualError(diffItem.expected, inspect);
600-
});
601-
}
602-
}), true);
603-
});
604-
}
605-
});
569+
.amend(delimiterOutput.sp()).error('// should be moved');
570+
} else if (diffItem.type === 'insert') {
571+
this.annotationBlock(function () {
572+
this.error('missing ').block(function () {
573+
var index = typeof diffItem.actualIndex !== 'undefined' ? diffItem.actualIndex : diffItem.expectedIndex;
574+
this.property(index, inspect(diffItem.value), true);
575+
});
576+
});
577+
} else if (diffItem.type === 'remove') {
578+
this.block(function () {
579+
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
580+
.amend(delimiterOutput.sp()).error('// should be removed');
581+
});
582+
} else if (diffItem.type === 'equal') {
583+
this.block(function () {
584+
this.property(diffItem.actualIndex, inspect(diffItem.value), true)
585+
.amend(delimiterOutput);
586+
});
587+
} else {
588+
this.block(function () {
589+
var valueDiff = diff(diffItem.value, diffItem.expected);
590+
this.property(diffItem.actualIndex, output.clone().block(function () {
591+
if (valueDiff && valueDiff.inline) {
592+
this.append(valueDiff.amend(delimiterOutput));
593+
} else if (valueDiff) {
594+
this.append(inspect(diffItem.value).amend(delimiterOutput.sp())).annotationBlock(function () {
595+
this.shouldEqualError(diffItem.expected, inspect).nl(2).append(valueDiff);
596+
});
597+
} else {
598+
this.append(inspect(diffItem.value).amend(delimiterOutput.sp())).annotationBlock(function () {
599+
this.shouldEqualError(diffItem.expected, inspect);
600+
});
601+
}
602+
}), true);
603+
});
604+
}
605+
});
606+
}
606607
}));
607608

608609
if (this.indent) {

test/api/addAssertion.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ describe('addAssertion', function () {
298298
'expected [ 3, 2, 1 ] to equal [ 1, 2, 3 ]\n' +
299299
'\n' +
300300
'[\n' +
301-
'┌───▷ \n' +
301+
'┌───▷\n' +
302302
'│ ┌─▷\n' +
303303
'│ │ 3,\n' +
304304
'│ └── 2, // should be moved\n' +
@@ -312,7 +312,7 @@ describe('addAssertion', function () {
312312
clonedExpect([3, 2, 1], 'to be sorted');
313313
}, 'to throw',
314314
'[\n' +
315-
'┌───▷ \n' +
315+
'┌───▷\n' +
316316
'│ ┌─▷\n' +
317317
'│ │ 3,\n' +
318318
'│ └── 2, // should be moved\n' +
@@ -397,7 +397,7 @@ describe('addAssertion', function () {
397397
clonedExpect([3, 2, 1], 'to be sorted after delay', 1, function (err) {
398398
expect(err, 'to have message',
399399
'[\n' +
400-
'┌───▷ \n' +
400+
'┌───▷\n' +
401401
'│ ┌─▷\n' +
402402
'│ │ 3,\n' +
403403
'│ └── 2, // should be moved\n' +
@@ -508,7 +508,7 @@ describe('addAssertion', function () {
508508
clonedExpect([3, 2, 1], 'to be sorted after delay', 1),
509509
'to be rejected with',
510510
'[\n' +
511-
'┌───▷ \n' +
511+
'┌───▷\n' +
512512
'│ ┌─▷\n' +
513513
'│ │ 3,\n' +
514514
'│ └── 2, // should be moved\n' +

test/assertions/to-satisfy.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ describe('to satisfy assertion', function () {
15811581
"to satisfy [ 1, 2, 3, bar: 456, baz: 789, quux: false ]\n" +
15821582
"\n" +
15831583
"[\n" +
1584-
"┌─▷ \n" +
1584+
"┌─▷\n" +
15851585
"│ 2,\n" +
15861586
"│ 3,\n" +
15871587
"└── 1, // should be moved\n" +

0 commit comments

Comments
 (0)