text.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { findMatchesInText, parseFlags } 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. test('should parse and use flags', () => {
  31. expect(findMatchesInText(' foo FOO bar ', '(?i)foo')).toEqual([
  32. { length: 3, start: 1, text: 'foo', end: 4 },
  33. { length: 3, start: 5, text: 'FOO', end: 8 },
  34. ]);
  35. expect(findMatchesInText(' foo FOO bar ', '(?i)(?-i)foo')).toEqual([{ length: 3, start: 1, text: 'foo', end: 4 }]);
  36. expect(findMatchesInText('FOO\nfoobar\nbar', '(?ims)^foo.')).toEqual([
  37. { length: 4, start: 0, text: 'FOO\n', end: 4 },
  38. { length: 4, start: 4, text: 'foob', end: 8 },
  39. ]);
  40. expect(findMatchesInText('FOO\nfoobar\nbar', '(?ims)(?-smi)^foo.')).toEqual([]);
  41. });
  42. });
  43. describe('parseFlags()', () => {
  44. it('when no flags or text', () => {
  45. expect(parseFlags('')).toEqual({ cleaned: '', flags: 'g' });
  46. expect(parseFlags('(?is)')).toEqual({ cleaned: '', flags: 'gis' });
  47. expect(parseFlags('foo')).toEqual({ cleaned: 'foo', flags: 'g' });
  48. });
  49. it('when flags present', () => {
  50. expect(parseFlags('(?i)foo')).toEqual({ cleaned: 'foo', flags: 'gi' });
  51. expect(parseFlags('(?ims)foo')).toEqual({ cleaned: 'foo', flags: 'gims' });
  52. });
  53. it('when flags cancel each other', () => {
  54. expect(parseFlags('(?i)(?-i)foo')).toEqual({ cleaned: 'foo', flags: 'g' });
  55. expect(parseFlags('(?i-i)foo')).toEqual({ cleaned: 'foo', flags: 'g' });
  56. expect(parseFlags('(?is)(?-ims)foo')).toEqual({ cleaned: 'foo', flags: 'g' });
  57. expect(parseFlags('(?i)(?-i)(?i)foo')).toEqual({ cleaned: 'foo', flags: 'gi' });
  58. });
  59. });