Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3874573

Browse files
committed
chore(handler): add more tests for search and highlights
1 parent 8d14a34 commit 3874573

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/__tests__/handler/highlights.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Neovim } from '@chemzqm/neovim'
22
import { Disposable, Position, Range } from 'vscode-languageserver-protocol'
33
import Highlights from '../../handler/highlights'
44
import languages from '../../languages'
5+
import workspace from '../../workspace'
56
import { disposeAll } from '../../util'
67
import helper from '../helper'
78

@@ -106,7 +107,21 @@ describe('document highlights', () => {
106107
expect(res).toBeNull()
107108
})
108109

109-
it('should not throw when document is not valid', async () => {
110+
it('should not throw when document is command line', async () => {
111+
await nvim.call('feedkeys', ['q:', 'in'])
112+
let doc = await workspace.document
113+
expect(doc.isCommandLine).toBe(true)
114+
let err
115+
try {
116+
await highlights.highlight()
117+
} catch (e) {
118+
err = e
119+
}
120+
await nvim.input('<C-c>')
121+
expect(err).toBeUndefined()
122+
})
123+
124+
it('should not throw when provide not found', async () => {
110125
disposeAll(disposables)
111126
await helper.createDocument()
112127
await nvim.setLine(' oo')

src/__tests__/handler/search.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Neovim } from '@chemzqm/neovim'
22
import Refactor from '../../handler/refactor'
3-
import Search from '../../handler/search'
3+
import Search, { getPathFromArgs } from '../../handler/search'
44
import helper from '../helper'
55
import path from 'path'
66

@@ -25,17 +25,39 @@ afterEach(async () => {
2525
await helper.reset()
2626
})
2727

28+
describe('getPathFromArgs', () => {
29+
it('should get undefined path', async () => {
30+
let res = getPathFromArgs(['a'])
31+
expect(res).toBeUndefined()
32+
res = getPathFromArgs(['a', 'b', '-c'])
33+
expect(res).toBeUndefined()
34+
res = getPathFromArgs(['a', '-b', 'c'])
35+
expect(res).toBeUndefined()
36+
})
37+
})
38+
2839
describe('search', () => {
2940

3041
it('should open refactor window', async () => {
3142
let search = new Search(nvim, cmd)
3243
let buf = await refactor.createRefactorBuffer()
3344
await search.run([], cwd, buf)
45+
await helper.wait(50)
3446
let fileItems = buf.fileItems
3547
expect(fileItems.length).toBe(2)
3648
expect(fileItems[0].ranges.length).toBe(2)
3749
})
3850

51+
it('should abort task', async () => {
52+
let search = new Search(nvim, cmd)
53+
let buf = await refactor.createRefactorBuffer()
54+
let p = search.run(['--sleep', '1000'], cwd, buf)
55+
search.abort()
56+
await p
57+
let fileItems = buf.fileItems
58+
expect(fileItems.length).toBe(0)
59+
})
60+
3961
it('should work with CocAction search', async () => {
4062
await helper.doAction('search', ['CocAction'])
4163
let bufnr = await nvim.call('bufnr', ['%'])

src/__tests__/rg

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ let content = `\x1b[30mmodules/cursors.test.ts\x1b[m
2727
\x1b[32m46\x1b[m- expect(res).toEqual({ line: 0, character: 3 })
2828
`
2929

30-
process.stdout.write(content)
30+
let idx = process.argv.findIndex(s => s == '--sleep')
31+
if (idx !== -1) {
32+
let ms = process.argv[idx + 1]
33+
setTimeout(() => {
34+
process.stdout.write(content)
35+
}, ms)
36+
} else {
37+
process.stdout.write(content)
38+
}

src/handler/search.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ export default class Search {
185185
})
186186
})
187187
}
188+
189+
public abort(): void {
190+
this.task?.dispose()
191+
}
188192
}
189193

190194
// rg requires `-- [path]` at the end
191-
function getPathFromArgs(args: string[]): string | undefined {
195+
export function getPathFromArgs(args: string[]): string | undefined {
192196
if (args.length < 2) return undefined
193197
let len = args.length
194198
if (args[len - 1].startsWith('-')) return undefined

0 commit comments

Comments
 (0)