Files
superwork/vscode/src/codeReader/localSymbols.test.ts
T

34 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { LocalSymbol, SourceRange } from './contract'
import { describe, expect, it } from 'vitest'
import { definitionFor, symbolAt, symbolOccurrences } from './localSymbols'
function range(startIndex: number, endIndex: number): SourceRange {
return { startIndex, endIndex }
}
// out=1(0-3);echo $out(10-13);out=2(15-18);echo $out(25-28)
const OUT: LocalSymbol = { definitions: [range(0, 3), range(15, 18)], occurrences: [range(0, 3), range(10, 13), range(15, 18), range(25, 28)] }
const URL: LocalSymbol = { definitions: [range(40, 43)], occurrences: [range(30, 33), range(40, 43)] }
describe('语言插件认出的同一文件里的名字', () => {
it('找盖住位置的名字;名字外面没有', () => {
expect(symbolAt([URL, OUT], 11)).toBe(OUT)
expect(symbolAt([URL, OUT], 13)).toBeUndefined()
})
it('跳到之前最近的一处定义;前面没有定义就跳到第一处', () => {
expect(definitionFor(OUT, 11)).toEqual(range(0, 3))
expect(definitionFor(OUT, 26)).toEqual(range(15, 18))
expect(definitionFor(URL, 31)).toEqual(range(40, 43))
})
it('各处出现:定义处算写,别处算读', () => {
expect(symbolOccurrences(OUT)).toEqual([
{ start: 0, end: 3, kind: 'write' },
{ start: 10, end: 13, kind: 'read' },
{ start: 15, end: 18, kind: 'write' },
{ start: 25, end: 28, kind: 'read' },
])
})
})