Files
modeling-app/src/lang/queryAst/getIdentifiersInProgram.test.ts
Jess Frazelle c0aa763c3b initPromise in main.ts (#6285)
* initPromise in main.ts

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* move initPromise so dep tree doesnt shit the bed

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
2025-04-11 18:17:46 +00:00

56 lines
1.4 KiB
TypeScript

import { getIdentifiersInProgram } from '@src/lang/queryAst/getIndentifiersInProgram'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
function identifier(name: string, start: number, end: number) {
return {
type: 'Name',
start,
end,
commentStart: start,
abs_path: false,
path: [],
name: {
start,
end,
commentStart: start,
type: 'Identifier',
name,
},
}
}
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),
])
})
})