get-matches.spec.js (3031B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import getMatches from "../get-matches"; 6 7 describe("search", () => { 8 describe("getMatches", () => { 9 it("gets basic string match", () => { 10 const text = "the test string with test in it multiple times test."; 11 const query = "test"; 12 const matchLocations = getMatches(query, text, { 13 caseSensitive: true, 14 wholeWord: false, 15 regexMatch: false, 16 }); 17 expect(matchLocations).toHaveLength(3); 18 }); 19 20 it("gets basic string match case-sensitive", () => { 21 const text = "the Test string with test in it multiple times test."; 22 const query = "Test"; 23 const matchLocations = getMatches(query, text, { 24 caseSensitive: true, 25 wholeWord: false, 26 regexMatch: false, 27 }); 28 expect(matchLocations).toHaveLength(1); 29 }); 30 31 it("gets whole word string match", () => { 32 const text = "the test string test in it multiple times whoatestthe."; 33 const query = "test"; 34 const matchLocations = getMatches(query, text, { 35 caseSensitive: true, 36 wholeWord: true, 37 regexMatch: false, 38 }); 39 expect(matchLocations).toHaveLength(2); 40 }); 41 42 it("gets regex match", () => { 43 const text = "the test string test in it multiple times whoatestthe."; 44 const query = "(\\w+)\\s+(\\w+)"; 45 const matchLocations = getMatches(query, text, { 46 caseSensitive: true, 47 wholeWord: false, 48 regexMatch: true, 49 }); 50 expect(matchLocations).toHaveLength(4); 51 }); 52 53 it("doesnt fail on empty data", () => { 54 const text = ""; 55 const query = ""; 56 const matchLocations = getMatches(query, text, { 57 caseSensitive: true, 58 wholeWord: false, 59 regexMatch: true, 60 }); 61 expect(matchLocations).toHaveLength(0); 62 }); 63 64 it("fails gracefully when the line is too long", () => { 65 const text = Array(100002).join("x"); 66 const query = "query"; 67 const matchLocations = getMatches(query, text, { 68 caseSensitive: true, 69 wholeWord: false, 70 regexMatch: true, 71 }); 72 expect(matchLocations).toHaveLength(0); 73 }); 74 75 // regression test for #6896 76 it("doesn't crash on the regex 'a*'", () => { 77 const text = "abc"; 78 const query = "a*"; 79 const matchLocations = getMatches(query, text, { 80 caseSensitive: true, 81 wholeWord: false, 82 regexMatch: true, 83 }); 84 expect(matchLocations).toHaveLength(4); 85 }); 86 87 // regression test for #6896 88 it("doesn't crash on the regex '^'", () => { 89 const text = "012"; 90 const query = "^"; 91 const matchLocations = getMatches(query, text, { 92 caseSensitive: true, 93 wholeWord: false, 94 regexMatch: true, 95 }); 96 expect(matchLocations).toHaveLength(1); 97 }); 98 }); 99 });