findInContent.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @author: oldj
  3. * @homepage: https://oldj.net
  4. */
  5. import { describe, expect, it } from 'vitest'
  6. import { default as findInContent } from '../../src/main/actions/find/findPositionsInContent'
  7. describe('find in content test', () => {
  8. it('basic test 1', () => {
  9. const content = `abc12 abc123 abc`
  10. const matches = findInContent(content, /bc/ig)
  11. expect(matches).toHaveLength(3)
  12. expect(matches[0]).toMatchObject({
  13. line: 1,
  14. start: 1,
  15. end: 3,
  16. before: 'a',
  17. match: 'bc',
  18. })
  19. expect(matches[0].after).toEqual(expect.any(String))
  20. expect(matches[1]).toMatchObject({
  21. line: 1,
  22. start: 7,
  23. end: 9,
  24. before: 'abc12 a',
  25. match: 'bc',
  26. after: '123 abc',
  27. })
  28. expect(matches[2]).toMatchObject({
  29. line: 1,
  30. start: 14,
  31. end: 16,
  32. before: 'abc12 abc123 a',
  33. match: 'bc',
  34. after: '',
  35. })
  36. })
  37. it('basic test 2', () => {
  38. const content = `abc12 abc123 abc\nxyza3b`
  39. const matches = findInContent(content, /a\w*3/ig)
  40. expect(matches).toHaveLength(2)
  41. expect(matches[0]).toMatchObject({
  42. line: 1,
  43. start: 6,
  44. end: 12,
  45. before: 'abc12 ',
  46. match: 'abc123',
  47. after: ' abc',
  48. })
  49. expect(matches[1]).toMatchObject({
  50. line: 2,
  51. start: 20,
  52. end: 22,
  53. before: 'xyz',
  54. match: 'a3',
  55. after: 'b',
  56. })
  57. })
  58. })