Commit 6cae566e authored by Robert Knight's avatar Robert Knight

Add test case that causes `text` argument to `textMatchScore` to be empty

This could happen in the real application if text near the end of the
document was annotated and an update to the document removes the text
between the end of the annotated text and the end of the document.

I also added a note about the preconditions for when the `search`
function is guaranteed to return at least one match.
parent 6ab4d156
......@@ -53,10 +53,13 @@ function search(text, str, maxErrors) {
* @param {string} str
*/
function textMatchScore(text, str) {
/* istanbul ignore next - `scoreMatch` will never pass an empty string */
// `search` will return no matches if either the text or pattern is empty,
// otherwise it will return at least one match if the max allowed error count
// is at least `str.length`.
if (str.length === 0 || text.length === 0) {
return 0.0;
}
const matches = search(text, str, str.length);
// prettier-ignore
......
......@@ -196,13 +196,22 @@ describe('matchQuote', () => {
assert.equal(matchNoHint.start, posA, 'Wrong match with no hint');
});
it('matches with a context prefix longer than the text', () => {
const match = matchQuote(fixtures.solitude, 'years later', {
prefix: 'It used to be many',
});
it('matches when prefix length is greater than the match start offset', () => {
const context = { prefix: 'It used to be many' };
const match = matchQuote(fixtures.solitude, 'years later', context);
assert.isAbove(context.prefix.length, match.start);
assert.equal(
fixtures.solitude.slice(match.start, match.end),
'years later'
);
});
it('matches when match ends at end of text and there is a non-empty suffix', () => {
const text = 'Some document text';
const match = matchQuote(text, 'text', {
suffix: 'missing',
});
assert.equal(text.slice(match.start, match.end), 'text');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment