Add unit test createPathToNodeForLastVariable

This commit is contained in:
Pierre Jacquier
2025-07-03 08:50:08 -04:00
parent 5b8284e737
commit 5708b8c64b

View File

@ -15,13 +15,14 @@ import {
} from '@src/lang/create' } from '@src/lang/create'
import { import {
addSketchTo, addSketchTo,
createPathToNodeForLastVariable,
createVariableExpressionsArray, createVariableExpressionsArray,
deleteSegmentFromPipeExpression, deleteSegmentFromPipeExpression,
moveValueIntoNewVariable, moveValueIntoNewVariable,
sketchOnExtrudedFace, sketchOnExtrudedFace,
splitPipedProfile, splitPipedProfile,
} from '@src/lang/modifyAst' } from '@src/lang/modifyAst'
import { findUsesOfTagInPipe } from '@src/lang/queryAst' import { findUsesOfTagInPipe, getNodeFromPath } from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils' import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import type { Artifact } from '@src/lang/std/artifactGraph' import type { Artifact } from '@src/lang/std/artifactGraph'
import { codeRefFromRange } from '@src/lang/std/artifactGraph' import { codeRefFromRange } from '@src/lang/std/artifactGraph'
@ -994,3 +995,45 @@ describe('Testing createVariableExpressionsArray', () => {
expect(exprs.elements[1].name.name).toBe('var1') expect(exprs.elements[1].name.name).toBe('var1')
}) })
}) })
describe('Testing createPathToNodeForLastVariable', () => {
it('should create a path to the last variable in the array', () => {
const circleProfileInVar = `sketch001 = startSketchOn(XY)
profile001 = circle(sketch001, center = [0, 0], radius = 1)
extrude001 = extrude(profile001, length = 5)
`
const ast = assertParse(circleProfileInVar)
const path = createPathToNodeForLastVariable(ast, false)
expect(path.length).toEqual(4)
// Verify we can get the right node
const node = getNodeFromPath<any>(ast, path)
if (err(node)) {
throw node
}
// With the expected range
const startOfExtrudeIndex = circleProfileInVar.indexOf('extrude(')
expect(node.node.start).toEqual(startOfExtrudeIndex)
expect(node.node.end).toEqual(circleProfileInVar.length - 1)
})
it('should create a path to the first kwarg in the last expression', () => {
const circleProfileInVar = `sketch001 = startSketchOn(XY)
profile001 = circle(sketch001, center = [0, 0], radius = 1)
extrude001 = extrude(profile001, length = 123)
`
const ast = assertParse(circleProfileInVar)
const path = createPathToNodeForLastVariable(ast, true)
expect(path.length).toEqual(7)
// Verify we can get the right node
const node = getNodeFromPath<any>(ast, path)
if (err(node)) {
throw node
}
// With the expected range
const startOfKwargIndex = circleProfileInVar.indexOf('123')
expect(node.node.start).toEqual(startOfKwargIndex)
expect(node.node.end).toEqual(startOfKwargIndex + 3)
})
})