2025-03-19 11:58:53 -04:00
|
|
|
import { assertParse, initPromise } from 'lang/wasm'
|
|
|
|
import { getIdentifiersInProgram } from './getIndentifiersInProgram'
|
|
|
|
|
|
|
|
function identifier(name: string, start: number, end: number) {
|
|
|
|
return {
|
|
|
|
type: 'Identifier',
|
|
|
|
name,
|
|
|
|
start,
|
|
|
|
end,
|
2025-03-20 16:23:20 +13:00
|
|
|
commentStart: start,
|
2025-03-19 11:58:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await initPromise
|
|
|
|
})
|
|
|
|
|
|
|
|
describe(`getIdentifiersInProgram`, () => {
|
|
|
|
it(`finds no identifiers in an empty program`, () => {
|
|
|
|
const identifiers = getIdentifiersInProgram(assertParse(''))
|
|
|
|
expect(identifiers).toEqual([])
|
|
|
|
})
|
|
|
|
it(`finds a single identifier in an expression`, () => {
|
|
|
|
const identifiers = getIdentifiersInProgram(assertParse('55 + a'))
|
|
|
|
expect(identifiers).toEqual([identifier('a', 5, 6)])
|
|
|
|
})
|
|
|
|
it(`finds multiple identifiers in an expression`, () => {
|
|
|
|
const identifiers = getIdentifiersInProgram(assertParse('a + b + c'))
|
|
|
|
expect(identifiers).toEqual([
|
|
|
|
identifier('a', 0, 1),
|
|
|
|
identifier('b', 4, 5),
|
|
|
|
identifier('c', 8, 9),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
it(`finds all the identifiers in a normal program`, () => {
|
|
|
|
const program = assertParse(`x = 5 + 2
|
|
|
|
y = x * 2
|
|
|
|
z = y + 1`)
|
|
|
|
const identifiers = getIdentifiersInProgram(program)
|
|
|
|
expect(identifiers).toEqual([
|
|
|
|
identifier('x', 14, 15),
|
|
|
|
identifier('y', 24, 25),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|