text.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { findMatchesInText } from './text';
  2. describe('findMatchesInText()', () => {
  3. it('gets no matches for when search and or line are empty', () => {
  4. expect(findMatchesInText('', '')).toEqual([]);
  5. expect(findMatchesInText('foo', '')).toEqual([]);
  6. expect(findMatchesInText('', 'foo')).toEqual([]);
  7. });
  8. it('gets no matches for unmatched search string', () => {
  9. expect(findMatchesInText('foo', 'bar')).toEqual([]);
  10. });
  11. it('gets matches for matched search string', () => {
  12. expect(findMatchesInText('foo', 'foo')).toEqual([{ length: 3, start: 0, text: 'foo', end: 3 }]);
  13. expect(findMatchesInText(' foo ', 'foo')).toEqual([{ length: 3, start: 1, text: 'foo', end: 4 }]);
  14. });
  15. test('should find all matches for a complete regex', () => {
  16. expect(findMatchesInText(' foo foo bar ', 'foo|bar')).toEqual([
  17. { length: 3, start: 1, text: 'foo', end: 4 },
  18. { length: 3, start: 5, text: 'foo', end: 8 },
  19. { length: 3, start: 9, text: 'bar', end: 12 },
  20. ]);
  21. });
  22. test('not fail on incomplete regex', () => {
  23. expect(findMatchesInText(' foo foo bar ', 'foo|')).toEqual([
  24. { length: 3, start: 1, text: 'foo', end: 4 },
  25. { length: 3, start: 5, text: 'foo', end: 8 },
  26. ]);
  27. expect(findMatchesInText('foo foo bar', '(')).toEqual([]);
  28. expect(findMatchesInText('foo foo bar', '(foo|')).toEqual([]);
  29. });
  30. });