Permalink
Browse files

Merge 72e0c71 into bf22cde

  • Loading branch information...
alexjeffburke committed Nov 14, 2017
2 parents bf22cde + 72e0c71 commit 64262a0cc5102e27c4fca95fd62c411db573f9a4
Showing with 136 additions and 4 deletions.
  1. +32 −4 lib/assertions.js
  2. +26 −0 lib/utils.js
  3. +39 −0 test/assertions/to-begin-with.spec.js
  4. +39 −0 test/assertions/to-end-with.spec.js
View
@@ -483,6 +483,19 @@ module.exports = function (expect) {
if (value === '') {
throw new Error("The '" + expect.testDescription + "' assertion does not support a prefix of the empty string");
}
var isTruncated = false;
var outputSubject = utils.truncateSubjectStringForBegin(subject, value);
if (outputSubject === null) {
outputSubject = subject;
} else {
isTruncated = true;
}
expect.subjectOutput = function (output) {
output = output.jsString("'" + outputSubject.replace(/\n/g, '\\n') + "'");
if (isTruncated) {
output.jsComment('...');
}
};
expect.withError(function () {
expect(subject.substr(0, value.length), '[not] to equal', value);
}, function (err) {
@@ -502,7 +515,8 @@ module.exports = function (expect) {
} else {
output
.partialMatch(subject.substr(0, i))
.text(subject.substr(i));
.text(outputSubject.substr(i))
.jsComment(isTruncated ? '...' : '');
}
}
return output;
@@ -515,6 +529,19 @@ module.exports = function (expect) {
if (value === '') {
throw new Error("The '" + expect.testDescription + "' assertion does not support a suffix of the empty string");
}
var isTruncated = false;
var outputSubject = utils.truncateSubjectStringForEnd(subject, value);
if (outputSubject === null) {
outputSubject = subject;
} else {
isTruncated = true;
}
expect.subjectOutput = function (output) {
if (isTruncated) {
output = output.jsComment('...');
}
output.jsString("'" + outputSubject.replace(/\n/g, '\\n') + "'");
};
expect.withError(function () {
expect(subject.substr(-value.length), '[not] to equal', value);
}, function (err) {
@@ -525,16 +552,17 @@ module.exports = function (expect) {
output.text(subject.substr(0, subject.length - value.length)).removedHighlight(value);
} else {
var i = 0;
while (subject[subject.length - 1 - i] === value[value.length - 1 - i]) {
while (outputSubject[outputSubject.length - 1 - i] === value[value.length - 1 - i]) {
i += 1;
}
if (i === 0) {
// No common suffix, omit diff
return null;
}
output
.text(subject.substr(0, subject.length - i))
.partialMatch(subject.substr(subject.length - i, subject.length));
.jsComment(isTruncated ? '...' : '')
.text(outputSubject.substr(0, outputSubject.length - i))
.partialMatch(outputSubject.substr(outputSubject.length - i, outputSubject.length));
}
return output;
}
View
@@ -194,5 +194,31 @@ var utils = module.exports = {
}
return packing;
}
},
truncateSubjectStringForBegin: function (subject, value) {
var contextLength = value.length + 25;
if (subject.length <= contextLength) {
return null;
}
var truncationIndex = subject.indexOf(' ', value.length + 1);
if (truncationIndex > -1 && truncationIndex < contextLength) {
return subject.substring(0, truncationIndex);
} else {
return subject.substring(0, contextLength);
}
},
truncateSubjectStringForEnd: function (subject, value) {
var contextLength = subject.length - value.length - 25;
if (contextLength <= 0) {
return null;
}
var truncationIndex = subject.lastIndexOf(' ', value.length + 1);
if (truncationIndex > -1 && truncationIndex >= contextLength) {
return subject.substring(truncationIndex + 1, subject.length);
} else {
return subject.substring(contextLength, subject.length);
}
}
};
@@ -61,6 +61,45 @@ describe('to begin with assertion', function () {
"^^^"
);
});
it('builds the diff correctly when the string is truncated with no common prefix', function () {
expect(function () {
expect(
'foobarbaz-AtSomePointThisStringWillBeTruncated',
'to begin with',
'barbazquux'
);
}, 'to throw exception',
"expected 'foobarbaz-AtSomePointThisStringWill'... to begin with 'barbazquux'"
);
});
it('builds the diff correctly when the string contains a space and is truncated', function () {
expect(function () {
expect(
'foobarbaz-StringTruncates AtTheSpace',
'to begin with',
'barbazquux'
);
}, 'to throw exception',
"expected 'foobarbaz-StringTruncates'... to begin with 'barbazquux'"
);
});
it('builds the diff correctly when the string is truncated after a partial match', function () {
expect(function () {
expect(
'foobarbaz-ButAtSomePointThisStringWillBeTruncated',
'to begin with',
'foobarquux'
);
}, 'to throw exception',
"expected 'foobarbaz-ButAtSomePointThisStringW'... to begin with 'foobarquux'\n" +
"\n" +
"foobarbaz-ButAtSomePointThisStringW...\n" +
"^^^^^^"
);
});
});
});
@@ -61,6 +61,45 @@ describe('to end with assertion', function () {
"^^^"
);
});
it('builds the diff correctly when the string is truncated with no common prefix', function () {
expect(function () {
expect(
'AtSomePointThisStringWillBeTruncated-foobarbaz',
'to end with',
'barbazquux'
);
}, 'to throw exception',
"expected ...'ThisStringWillBeTruncated-foobarbaz' to end with 'barbazquux'"
);
});
it('builds the diff correctly when the string contains a space and is truncated', function () {
expect(function () {
expect(
'a ThenPleaseTruncateString-foobarbaz',
'to end with',
'barbazquux'
);
}, 'to throw exception',
"expected ...'ThenPleaseTruncateString-foobarbaz' to end with 'barbazquux'"
);
});
it('builds the diff correctly when the string is truncated after a partial match', function () {
expect(function () {
expect(
'ButAtSomePointThisStringWillBeTruncated-bazbarfoo',
'to end with',
'quuxbarfoo'
);
}, 'to throw exception',
"expected ...'ThisStringWillBeTruncated-bazbarfoo' to end with 'quuxbarfoo'\n" +
"\n" +
"...ThisStringWillBeTruncated-bazbarfoo\n" +
" ^^^^^^"
);
});
});
});

0 comments on commit 64262a0

Please sign in to comment.