Files
superwork/vscode/src/codeReader/editor/definitions.test.ts
T

43 lines
1.9 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { definitionOffset } from './definitions'
vi.mock('vscode', () => ({
commands: { executeCommand: vi.fn() },
EventEmitter: class EventEmitter { event = vi.fn(); fire = vi.fn() },
Range: class Range {},
Uri: { parse: vi.fn() },
window: { createOutputChannel: vi.fn(() => ({ appendLine: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() })) },
workspace: { openTextDocument: vi.fn(), asRelativePath: vi.fn() },
}))
const SOURCE = [
'export class DeployActionError extends Error {',
' status = 1',
'}',
'export const fetchAppDeployments = async () => 1',
].join('\n')
describe('定义范围的起点不在名字上时,用范围里那个名字', () => {
const nameAt = SOURCE.indexOf('fetchAppDeployments')
const whole = { start: 0, end: SOURCE.length }
it('没有选区:在大范围里找到和点击的名字一致的那一处,不用大范围的起点', () => {
expect(definitionOffset(SOURCE, whole, undefined, 'fetchAppDeployments')).toBe(nameAt)
})
it('选区起点已经在名字上:用选区,不再往范围更前面找', () => {
const earlier = SOURCE.indexOf('DeployActionError')
expect(definitionOffset(SOURCE, whole, { start: nameAt, end: nameAt + 'fetchAppDeployments'.length }, 'fetchAppDeployments')).toBe(nameAt)
expect(definitionOffset(SOURCE, whole, { start: earlier, end: earlier + 'DeployActionError'.length }, 'DeployActionError')).toBe(earlier)
})
it('选区是一整段、起点不在名字上:仍在这段里找名字', () => {
expect(definitionOffset(SOURCE, whole, whole, 'fetchAppDeployments')).toBe(nameAt)
})
it('只是更长名字的前缀不算', () => {
const text = 'const fetchAppDeploymentsExtra = fetchAppDeployments'
expect(definitionOffset(text, { start: 0, end: text.length }, undefined, 'fetchAppDeployments')).toBe(text.lastIndexOf('fetchAppDeployments'))
})
})