-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.test.ts
More file actions
164 lines (144 loc) · 4.5 KB
/
Copy pathsearch.test.ts
File metadata and controls
164 lines (144 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { describe, expect, it } from 'vitest'
import { DEFAULT_OPTIONS } from '@/features/miscellaneous/find-replace/constants'
import {
applyReplacement,
buildSearchRegex,
findMatches,
getLineAndColumn
} from '@/features/miscellaneous/find-replace/utils/search'
import type { TFindOptions } from '@/features/miscellaneous/find-replace/types'
function options(overrides: Partial<TFindOptions> = {}): TFindOptions {
return { ...DEFAULT_OPTIONS, ...overrides }
}
describe('buildSearchRegex', () => {
it('escapes special characters in literal mode', () => {
const built = buildSearchRegex('a.b(c)', options())
expect(built.ok).toBe(true)
if (built.ok) expect(built.regex.test('a.b(c)')).toBe(true)
})
it('reports invalid regex patterns', () => {
const built = buildSearchRegex('(unclosed', options({ regex: true }))
expect(built.ok).toBe(false)
})
it('respects case sensitivity', () => {
const insensitive = findMatches('Foo foo FOO', 'foo', options())
const sensitive = findMatches(
'Foo foo FOO',
'foo',
options({ caseSensitive: true })
)
expect(insensitive.ok && insensitive.matches.length).toBe(3)
expect(sensitive.ok && sensitive.matches.length).toBe(1)
})
it('matches whole words only when enabled', () => {
const result = findMatches(
'cat category cat',
'cat',
options({ wholeWord: true })
)
expect(result.ok && result.matches.length).toBe(2)
})
})
describe('findMatches', () => {
it('returns no matches for an empty query', () => {
const result = findMatches('anything', '', options())
expect(result.ok && result.matches).toEqual([])
})
it('does not loop forever on zero-length regex matches', () => {
const result = findMatches('abc', 'x*', options({ regex: true }))
expect(result.ok).toBe(true)
})
})
describe('applyReplacement', () => {
it('replaces all occurrences', () => {
const result = applyReplacement('a b a', 'a', 'x', options(), 'all')
expect(result.ok && result.text).toBe('x b x')
expect(result.ok && result.count).toBe(2)
})
it('replaces only the first occurrence', () => {
const result = applyReplacement('a b a', 'a', 'x', options(), 'first')
expect(result.ok && result.text).toBe('x b a')
})
it('replaces only the targeted match', () => {
const result = applyReplacement('a b a', 'a', 'x', options(), {
nth: 1
})
expect(result.ok && result.text).toBe('a b x')
})
it('replaces only the last occurrence', () => {
const result = applyReplacement('a b a', 'a', 'x', options(), 'last')
expect(result.ok && result.text).toBe('a b x')
})
it('replaces matches after a given occurrence index', () => {
const result = applyReplacement('a a a a', 'a', 'x', options(), {
after: 1
})
expect(result.ok && result.text).toBe('a a x x')
})
it('replaces the nth match counting from the end', () => {
const result = applyReplacement('a b a b a', 'a', 'x', options(), {
fromLast: 1
})
expect(result.ok && result.text).toBe('a b x b a')
})
it('replaces all matches except ones containing a substring', () => {
const result = applyReplacement(
'foo1 foo2 bar1',
'\\w+',
'X',
options({ regex: true }),
{ allExcept: 'bar' }
)
expect(result.ok && result.text).toBe('X X bar1')
})
it('expands capture groups in regex mode', () => {
const result = applyReplacement(
'john.doe@example.com',
'(\\w+)\\.(\\w+)@',
'$2.$1@',
options({ regex: true }),
'all'
)
expect(result.ok && result.text).toBe('doe.john@example.com')
})
it('keeps $1 literal outside regex mode', () => {
const result = applyReplacement('abc', 'b', '$1', options(), 'all')
expect(result.ok && result.text).toBe('a$1c')
})
it('preserves case of the matched text', () => {
const result = applyReplacement(
'Foo foo FOO',
'foo',
'bar',
options({ preserveCase: true }),
'all'
)
expect(result.ok && result.text).toBe('Bar bar BAR')
})
it('trims trailing whitespace when requested', () => {
const result = applyReplacement(
'a \nb\t\n',
'a',
'x',
options({ trimWhitespace: true }),
'all'
)
expect(result.ok && result.text).toBe('x\nb')
})
it('supports multiline anchors', () => {
const result = applyReplacement(
'one\ntwo\nthree',
'^t',
'T',
options({ regex: true, multiline: true }),
'all'
)
expect(result.ok && result.text).toBe('one\nTwo\nThree')
})
})
describe('getLineAndColumn', () => {
it('reports 1-based line and column', () => {
expect(getLineAndColumn('ab\ncd', 0)).toEqual({ line: 1, column: 1 })
expect(getLineAndColumn('ab\ncd', 4)).toEqual({ line: 2, column: 2 })
})
})