2023-02-12 10:56:45 +11:00
|
|
|
import {
|
|
|
|
changeSketchArguments,
|
|
|
|
addTagForSketchOnFace,
|
2023-02-21 10:50:45 +11:00
|
|
|
addNewSketchLn,
|
2023-02-12 10:56:45 +11:00
|
|
|
getYComponent,
|
|
|
|
getXComponent,
|
2023-09-14 09:34:37 -04:00
|
|
|
addCloseToPipe,
|
2024-05-24 20:54:42 +10:00
|
|
|
getConstraintInfo,
|
2023-02-12 10:56:45 +11:00
|
|
|
} from './sketch'
|
2024-05-24 20:54:42 +10:00
|
|
|
import {
|
2024-12-06 13:57:31 +13:00
|
|
|
assertParse,
|
2024-05-24 20:54:42 +10:00
|
|
|
recast,
|
|
|
|
initPromise,
|
|
|
|
CallExpression,
|
2025-01-17 14:34:36 -05:00
|
|
|
topLevelRange,
|
2024-05-24 20:54:42 +10:00
|
|
|
} from '../wasm'
|
|
|
|
import { getNodeFromPath, getNodePathFromSourceRange } from '../queryAst'
|
2023-07-10 15:15:07 +10:00
|
|
|
import { enginelessExecutor } from '../../lib/testHelpers'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
2023-02-12 10:56:45 +11:00
|
|
|
|
|
|
|
const eachQuad: [number, [number, number]][] = [
|
|
|
|
[-315, [1, 1]],
|
|
|
|
[-225, [-1, 1]],
|
|
|
|
[-135, [-1, -1]],
|
|
|
|
[-45, [1, -1]],
|
|
|
|
[45, [1, 1]],
|
|
|
|
[135, [-1, 1]],
|
|
|
|
[225, [-1, -1]],
|
|
|
|
[315, [1, -1]],
|
|
|
|
[405, [1, 1]],
|
|
|
|
[495, [-1, 1]],
|
|
|
|
[585, [-1, -1]],
|
|
|
|
[675, [1, -1]],
|
|
|
|
]
|
|
|
|
|
2024-04-19 14:24:40 -07:00
|
|
|
beforeAll(async () => {
|
|
|
|
await initPromise
|
|
|
|
})
|
2023-09-29 11:11:01 -07:00
|
|
|
|
2023-02-12 10:56:45 +11:00
|
|
|
describe('testing getYComponent', () => {
|
|
|
|
it('should return the vertical component of a vector correctly when given angles in each quadrant (and with angles < 0, or > 360)', () => {
|
|
|
|
const expected: [number, number][] = []
|
|
|
|
const results: [number, number][] = []
|
|
|
|
eachQuad.forEach(([angle, expectedResult]) => {
|
|
|
|
results.push(
|
|
|
|
getYComponent(angle, 1).map((a) => Math.round(a)) as [number, number]
|
|
|
|
)
|
|
|
|
expected.push(expectedResult)
|
|
|
|
})
|
|
|
|
expect(results).toEqual(expected)
|
|
|
|
})
|
|
|
|
it('return extreme values on the extremes', () => {
|
|
|
|
let result: [number, number]
|
|
|
|
result = getYComponent(0, 1)
|
|
|
|
expect(result[0]).toBe(1)
|
|
|
|
expect(result[1]).toBe(0)
|
|
|
|
|
|
|
|
result = getYComponent(90, 1)
|
|
|
|
expect(result[0]).toBe(1)
|
|
|
|
expect(result[1]).toBeGreaterThan(100000)
|
|
|
|
|
|
|
|
result = getYComponent(180, 1)
|
|
|
|
expect(result[0]).toBe(-1)
|
|
|
|
expect(result[1]).toBeCloseTo(0)
|
|
|
|
|
|
|
|
result = getYComponent(270, 1)
|
|
|
|
expect(result[0]).toBe(-1)
|
|
|
|
expect(result[1]).toBeLessThan(100000)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('testing getXComponent', () => {
|
|
|
|
it('should return the horizontal component of a vector correctly when given angles in each quadrant (and with angles < 0, or > 360)', () => {
|
|
|
|
const expected: [number, number][] = []
|
|
|
|
const results: [number, number][] = []
|
|
|
|
eachQuad.forEach(([angle, expectedResult]) => {
|
|
|
|
results.push(
|
|
|
|
getXComponent(angle, 1).map((a) => Math.round(a)) as [number, number]
|
|
|
|
)
|
|
|
|
expected.push(expectedResult)
|
|
|
|
})
|
|
|
|
expect(results).toEqual(expected)
|
|
|
|
})
|
|
|
|
it('return extreme values on the extremes', () => {
|
|
|
|
let result: [number, number]
|
|
|
|
result = getXComponent(0, 1)
|
|
|
|
expect(result[0]).toBeGreaterThan(100000)
|
|
|
|
expect(result[1]).toBe(1)
|
|
|
|
|
|
|
|
result = getXComponent(90, 1)
|
|
|
|
expect(result[0]).toBeCloseTo(0)
|
|
|
|
expect(result[1]).toBe(1)
|
|
|
|
|
|
|
|
result = getXComponent(180, 1)
|
|
|
|
expect(result[0]).toBeLessThan(100000)
|
|
|
|
expect(result[1]).toBe(1)
|
|
|
|
|
|
|
|
result = getXComponent(270, 1)
|
|
|
|
expect(result[0]).toBeCloseTo(0)
|
|
|
|
expect(result[1]).toBe(-1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('testing changeSketchArguments', () => {
|
|
|
|
const lineToChange = 'lineTo([-1.59, -1.54], %)'
|
|
|
|
const lineAfterChange = 'lineTo([2, 3], %)'
|
2023-06-22 16:43:33 +10:00
|
|
|
test('changeSketchArguments', async () => {
|
2023-07-10 15:15:07 +10:00
|
|
|
// Enable rotations #152
|
2024-10-02 14:19:40 -05:00
|
|
|
const genCode = (line: string) => `mySketch001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0], %)
|
2023-09-05 16:02:27 -07:00
|
|
|
|> ${line}
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
2024-04-22 17:14:20 -07:00
|
|
|
// |> rx(45, %)
|
2023-09-11 17:14:41 -07:00
|
|
|
`
|
2023-02-12 10:56:45 +11:00
|
|
|
const code = genCode(lineToChange)
|
|
|
|
const expectedCode = genCode(lineAfterChange)
|
2024-12-06 13:57:31 +13:00
|
|
|
const ast = assertParse(code)
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
const execState = await enginelessExecutor(ast)
|
2023-02-12 10:56:45 +11:00
|
|
|
const sourceStart = code.indexOf(lineToChange)
|
2024-06-24 11:45:40 -04:00
|
|
|
const changeSketchArgsRetVal = changeSketchArguments(
|
2023-02-12 10:56:45 +11:00
|
|
|
ast,
|
2024-10-09 19:38:40 -04:00
|
|
|
execState.memory,
|
2024-09-23 22:42:51 +10:00
|
|
|
{
|
|
|
|
type: 'sourceRange',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(
|
|
|
|
sourceStart,
|
|
|
|
sourceStart + lineToChange.length
|
|
|
|
),
|
2024-09-23 22:42:51 +10:00
|
|
|
},
|
2024-09-13 21:14:14 +10:00
|
|
|
{
|
|
|
|
type: 'straight-segment',
|
|
|
|
from: [0, 0],
|
|
|
|
to: [2, 3],
|
|
|
|
}
|
2023-02-12 10:56:45 +11:00
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(changeSketchArgsRetVal)) return changeSketchArgsRetVal
|
|
|
|
expect(recast(changeSketchArgsRetVal.modifiedAst)).toBe(expectedCode)
|
2023-02-12 10:56:45 +11:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-02-21 10:50:45 +11:00
|
|
|
describe('testing addNewSketchLn', () => {
|
2023-02-12 10:56:45 +11:00
|
|
|
const lineToChange = 'lineTo([-1.59, -1.54], %)'
|
2023-06-22 16:43:33 +10:00
|
|
|
test('addNewSketchLn', async () => {
|
2023-07-10 15:15:07 +10:00
|
|
|
// Enable rotations #152
|
2023-02-12 10:56:45 +11:00
|
|
|
const code = `
|
2024-10-02 14:19:40 -05:00
|
|
|
mySketch001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0], %)
|
2023-07-10 15:15:07 +10:00
|
|
|
// |> rx(45, %)
|
2023-02-12 10:56:45 +11:00
|
|
|
|> lineTo([-1.59, -1.54], %)
|
2024-03-01 17:16:18 -08:00
|
|
|
|> lineTo([0.46, -5.82], %)`
|
2024-12-06 13:57:31 +13:00
|
|
|
const ast = assertParse(code)
|
2024-06-24 11:45:40 -04:00
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
const execState = await enginelessExecutor(ast)
|
2023-02-12 10:56:45 +11:00
|
|
|
const sourceStart = code.indexOf(lineToChange)
|
2024-10-02 14:19:40 -05:00
|
|
|
expect(sourceStart).toBe(89)
|
2024-06-24 11:45:40 -04:00
|
|
|
const newSketchLnRetVal = addNewSketchLn({
|
2023-02-21 10:50:45 +11:00
|
|
|
node: ast,
|
2024-10-09 19:38:40 -04:00
|
|
|
programMemory: execState.memory,
|
2024-09-13 21:14:14 +10:00
|
|
|
input: {
|
|
|
|
type: 'straight-segment',
|
|
|
|
from: [0, 0],
|
|
|
|
to: [2, 3],
|
|
|
|
},
|
2023-02-21 10:50:45 +11:00
|
|
|
fnName: 'lineTo',
|
2023-04-01 16:47:00 +11:00
|
|
|
pathToNode: [
|
|
|
|
['body', ''],
|
|
|
|
[0, 'index'],
|
2024-12-07 07:16:04 +13:00
|
|
|
['declaration', 'VariableDeclaration'],
|
2023-04-01 16:47:00 +11:00
|
|
|
['init', 'VariableDeclarator'],
|
|
|
|
],
|
2023-02-12 10:56:45 +11:00
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(newSketchLnRetVal)) return newSketchLnRetVal
|
|
|
|
|
2023-07-10 15:15:07 +10:00
|
|
|
// Enable rotations #152
|
2024-10-02 14:19:40 -05:00
|
|
|
let expectedCode = `mySketch001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0], %)
|
2023-07-10 15:15:07 +10:00
|
|
|
// |> rx(45, %)
|
2023-02-12 10:56:45 +11:00
|
|
|
|> lineTo([-1.59, -1.54], %)
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
|
|
|
|> lineTo([2, 3], %)
|
2023-09-14 09:34:37 -04:00
|
|
|
`
|
2024-06-24 11:45:40 -04:00
|
|
|
|
|
|
|
const { modifiedAst } = newSketchLnRetVal
|
2023-09-14 09:34:37 -04:00
|
|
|
expect(recast(modifiedAst)).toBe(expectedCode)
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
const modifiedAst2 = addCloseToPipe({
|
2023-09-14 09:34:37 -04:00
|
|
|
node: ast,
|
2024-10-09 19:38:40 -04:00
|
|
|
programMemory: execState.memory,
|
2023-09-14 09:34:37 -04:00
|
|
|
pathToNode: [
|
|
|
|
['body', ''],
|
|
|
|
[0, 'index'],
|
2024-12-07 07:16:04 +13:00
|
|
|
['declaration', 'VariableDeclaration'],
|
2023-09-14 09:34:37 -04:00
|
|
|
['init', 'VariableDeclarator'],
|
|
|
|
],
|
|
|
|
})
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(modifiedAst2)) return modifiedAst2
|
2023-09-14 09:34:37 -04:00
|
|
|
|
2024-10-02 14:19:40 -05:00
|
|
|
expectedCode = `mySketch001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0], %)
|
2023-09-14 09:34:37 -04:00
|
|
|
// |> rx(45, %)
|
|
|
|
|> lineTo([-1.59, -1.54], %)
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
|
|
|
|> close(%)
|
2023-09-11 17:14:41 -07:00
|
|
|
`
|
2024-06-24 11:45:40 -04:00
|
|
|
expect(recast(modifiedAst2)).toBe(expectedCode)
|
2023-02-12 10:56:45 +11:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('testing addTagForSketchOnFace', () => {
|
2023-06-22 16:43:33 +10:00
|
|
|
it('needs to be in it', async () => {
|
2023-02-21 09:42:41 +11:00
|
|
|
const originalLine = 'lineTo([-1.59, -1.54], %)'
|
2023-07-10 15:15:07 +10:00
|
|
|
// Enable rotations #152
|
2024-10-02 14:19:40 -05:00
|
|
|
const genCode = (line: string) => `mySketch001 = startSketchOn('XY')
|
2023-10-05 14:27:48 -07:00
|
|
|
|> startProfileAt([0, 0], %)
|
2023-09-05 16:02:27 -07:00
|
|
|
// |> rx(45, %)
|
|
|
|
|> ${line}
|
|
|
|
|> lineTo([0.46, -5.82], %)
|
2023-09-11 17:14:41 -07:00
|
|
|
`
|
2023-02-21 09:42:41 +11:00
|
|
|
const code = genCode(originalLine)
|
2024-12-06 13:57:31 +13:00
|
|
|
const ast = assertParse(code)
|
2024-07-29 08:41:02 -07:00
|
|
|
await enginelessExecutor(ast)
|
2023-02-21 09:42:41 +11:00
|
|
|
const sourceStart = code.indexOf(originalLine)
|
2025-01-17 14:34:36 -05:00
|
|
|
const sourceRange = topLevelRange(
|
2023-02-21 09:42:41 +11:00
|
|
|
sourceStart,
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceStart + originalLine.length
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast)) return ast
|
2023-02-21 09:42:41 +11:00
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
2024-06-24 11:45:40 -04:00
|
|
|
const sketchOnFaceRetVal = addTagForSketchOnFace(
|
2023-02-21 09:42:41 +11:00
|
|
|
{
|
2024-10-09 19:38:40 -04:00
|
|
|
// previousProgramMemory: execState.memory, // redundant?
|
2023-02-21 09:42:41 +11:00
|
|
|
pathToNode,
|
|
|
|
node: ast,
|
|
|
|
},
|
2024-09-26 18:25:05 +10:00
|
|
|
'lineTo',
|
|
|
|
null
|
2023-02-21 09:42:41 +11:00
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(sketchOnFaceRetVal)) return sketchOnFaceRetVal
|
|
|
|
|
|
|
|
const { modifiedAst } = sketchOnFaceRetVal
|
2024-06-24 22:39:04 -07:00
|
|
|
const expectedCode = genCode('lineTo([-1.59, -1.54], %, $seg01)')
|
2023-02-21 09:42:41 +11:00
|
|
|
expect(recast(modifiedAst)).toBe(expectedCode)
|
|
|
|
})
|
2024-09-26 18:25:05 +10:00
|
|
|
const chamferTestCases = [
|
|
|
|
{
|
|
|
|
desc: 'chamfer in pipeExpr',
|
|
|
|
originalChamfer: ` |> chamfer({
|
2024-11-25 09:21:55 +13:00
|
|
|
length = 30,
|
|
|
|
tags = [seg01, getOppositeEdge(seg01)]
|
2024-09-26 18:25:05 +10:00
|
|
|
}, %)`,
|
|
|
|
expectedChamfer: ` |> chamfer({
|
2024-11-25 09:21:55 +13:00
|
|
|
length = 30,
|
|
|
|
tags = [getOppositeEdge(seg01)]
|
2024-09-26 18:25:05 +10:00
|
|
|
}, %, $seg03)
|
2024-11-25 09:21:55 +13:00
|
|
|
|> chamfer({ length = 30, tags = [seg01] }, %)`,
|
2024-09-26 18:25:05 +10:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: 'chamfer with its own variable',
|
2024-10-02 14:19:40 -05:00
|
|
|
originalChamfer: `chamf = chamfer({
|
2024-11-25 09:21:55 +13:00
|
|
|
length = 30,
|
|
|
|
tags = [seg01, getOppositeEdge(seg01)]
|
2024-09-26 18:25:05 +10:00
|
|
|
}, extrude001)`,
|
2024-10-02 14:19:40 -05:00
|
|
|
expectedChamfer: `chamf = chamfer({
|
2024-11-25 09:21:55 +13:00
|
|
|
length = 30,
|
|
|
|
tags = [getOppositeEdge(seg01)]
|
2024-09-26 18:25:05 +10:00
|
|
|
}, extrude001, $seg03)
|
2024-11-25 09:21:55 +13:00
|
|
|
|> chamfer({ length = 30, tags = [seg01] }, %)`,
|
2024-09-26 18:25:05 +10:00
|
|
|
},
|
|
|
|
// Add more test cases here if needed
|
|
|
|
] as const
|
|
|
|
|
|
|
|
chamferTestCases.forEach(({ originalChamfer, expectedChamfer, desc }) => {
|
2024-09-27 06:47:18 +10:00
|
|
|
it(`can break up chamfers in order to add tags - ${desc}`, async () => {
|
2024-10-02 14:19:40 -05:00
|
|
|
const genCode = (insertCode: string) => `sketch001 = startSketchOn('XZ')
|
2024-09-26 18:25:05 +10:00
|
|
|
|> startProfileAt([75.8, 317.2], %) // [$startCapTag, $EndCapTag]
|
|
|
|
|> angledLine([0, 268.43], %, $rectangleSegmentA001)
|
|
|
|
|> angledLine([
|
|
|
|
segAng(rectangleSegmentA001) - 90,
|
|
|
|
217.26
|
|
|
|
], %, $seg01)
|
|
|
|
|> angledLine([
|
|
|
|
segAng(rectangleSegmentA001),
|
|
|
|
-segLen(rectangleSegmentA001)
|
|
|
|
], %)
|
|
|
|
|> lineTo([profileStartX(%), profileStartY(%)], %, $seg02)
|
|
|
|
|> close(%)
|
2024-10-02 14:19:40 -05:00
|
|
|
extrude001 = extrude(100, sketch001)
|
2024-09-26 18:25:05 +10:00
|
|
|
${insertCode}
|
|
|
|
`
|
|
|
|
const code = genCode(originalChamfer)
|
2024-12-06 13:57:31 +13:00
|
|
|
const ast = assertParse(code)
|
2024-09-26 18:25:05 +10:00
|
|
|
await enginelessExecutor(ast)
|
|
|
|
const sourceStart = code.indexOf(originalChamfer)
|
|
|
|
const extraChars = originalChamfer.indexOf('chamfer')
|
2025-01-17 14:34:36 -05:00
|
|
|
const sourceRange = topLevelRange(
|
2024-09-26 18:25:05 +10:00
|
|
|
sourceStart + extraChars,
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceStart + originalChamfer.length - extraChars
|
|
|
|
)
|
2024-09-26 18:25:05 +10:00
|
|
|
|
|
|
|
if (err(ast)) throw ast
|
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
|
|
|
console.log('pathToNode', pathToNode)
|
|
|
|
const sketchOnFaceRetVal = addTagForSketchOnFace(
|
|
|
|
{
|
|
|
|
pathToNode,
|
|
|
|
node: ast,
|
|
|
|
},
|
|
|
|
'chamfer',
|
|
|
|
{
|
|
|
|
type: 'edgeCut',
|
|
|
|
subType: 'opposite',
|
|
|
|
tagName: 'seg01',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if (err(sketchOnFaceRetVal)) throw sketchOnFaceRetVal
|
|
|
|
expect(recast(sketchOnFaceRetVal.modifiedAst)).toBe(
|
|
|
|
genCode(expectedChamfer)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2023-02-12 10:56:45 +11:00
|
|
|
})
|
2024-05-24 20:54:42 +10:00
|
|
|
|
|
|
|
describe('testing getConstraintInfo', () => {
|
|
|
|
describe('object notation', () => {
|
|
|
|
const code = `const part001 = startSketchOn('-XZ')
|
|
|
|
|> startProfileAt([0,0], %)
|
|
|
|
|> line([3, 4], %)
|
|
|
|
|> angledLine({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 3.14,
|
|
|
|
length = 3.14,
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> lineTo([6.14, 3.14], %)
|
|
|
|
|> xLineTo(8, %)
|
|
|
|
|> yLineTo(5, %)
|
2024-12-06 13:57:31 +13:00
|
|
|
|> yLine(3.14, %, $a)
|
2024-05-24 20:54:42 +10:00
|
|
|
|> xLine(3.14, %)
|
|
|
|
|> angledLineOfXLength({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 3.14,
|
|
|
|
length = 3.14,
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> angledLineOfYLength({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 30,
|
|
|
|
length = 3,
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> angledLineToX({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 12.14,
|
|
|
|
to = 12,
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> angledLineToY({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 30,
|
|
|
|
to = 10.14,
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> angledLineThatIntersects({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 3.14,
|
2024-12-06 13:57:31 +13:00
|
|
|
intersectTag = a,
|
2024-11-25 09:21:55 +13:00
|
|
|
offset = 0
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> tangentialArcTo([3.14, 13.14], %)`
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'line',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(78, 79),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'line',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '4',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(81, 82),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'line',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
`angledLine(`,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(118, 122),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'length',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(137, 141),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'lineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '6.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(164, 168),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'lineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(170, 174),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'lineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'xLineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'horizontal',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'xLineTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(185, 192),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '8',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(193, 194),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'yLineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'vertical',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'yLineTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(204, 211),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '5',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(212, 213),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'yLine(',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'vertical',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'yLine',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(223, 228),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(229, 233),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'xLine(',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'horizontal',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'xLine',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(247, 252),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(253, 257),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfXLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(301, 305),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(320, 324),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfYLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '30',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(373, 375),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(390, 391),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToX',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '12.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(434, 439),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '12',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(450, 452),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'to' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToY',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '30',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(495, 497),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '10.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(508, 513),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'to' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineThatIntersects',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(567, 571),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'intersectionOffset',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(608, 609),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'offset' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'intersectionTag',
|
|
|
|
isConstrained: false,
|
2024-12-06 13:57:31 +13:00
|
|
|
value: 'a',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(592, 593),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: {
|
|
|
|
key: 'intersectTag',
|
|
|
|
type: 'objectProperty',
|
|
|
|
},
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'tangentialArcTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'tangentialWithPrevious',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'tangentialArcTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(623, 638),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(640, 644),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '13.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(646, 651),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])('testing %s when inputs are unconstrained', (functionName, expected) => {
|
2025-01-17 14:34:36 -05:00
|
|
|
const ast = assertParse(code)
|
|
|
|
const sourceRange = topLevelRange(
|
2024-05-24 20:54:42 +10:00
|
|
|
code.indexOf(functionName),
|
2025-01-17 14:34:36 -05:00
|
|
|
code.indexOf(functionName) + functionName.length
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast)) return ast
|
2024-05-24 20:54:42 +10:00
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
2024-10-30 16:52:17 -04:00
|
|
|
const callExp = getNodeFromPath<Node<CallExpression>>(
|
2024-05-24 20:54:42 +10:00
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'CallExpression'
|
2024-06-24 11:45:40 -04:00
|
|
|
)
|
|
|
|
if (err(callExp)) return callExp
|
|
|
|
const result = getConstraintInfo(callExp.node, code, pathToNode)
|
2024-05-24 20:54:42 +10:00
|
|
|
expect(result).toEqual(expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('array notation', () => {
|
|
|
|
const code = `const part001 = startSketchOn('-XZ')
|
|
|
|
|> startProfileAt([0, 0], %)
|
|
|
|
|> line([3, 4], %)
|
|
|
|
|> angledLine([3.14, 3.14], %)
|
|
|
|
|> lineTo([6.14, 3.14], %)
|
|
|
|
|> xLineTo(8, %)
|
|
|
|
|> yLineTo(5, %)
|
2024-12-06 13:57:31 +13:00
|
|
|
|> yLine(3.14, %, $a)
|
2024-05-24 20:54:42 +10:00
|
|
|
|> xLine(3.14, %)
|
|
|
|
|> angledLineOfXLength([3.14, 3.14], %)
|
|
|
|
|> angledLineOfYLength([30, 3], %)
|
|
|
|
|> angledLineToX([12, 12], %)
|
|
|
|
|> angledLineToY([30, 10], %)
|
|
|
|
|> angledLineThatIntersects({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 3.14,
|
2024-12-06 13:57:31 +13:00
|
|
|
intersectTag = a,
|
2024-11-25 09:21:55 +13:00
|
|
|
offset = 0
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> tangentialArcTo([3.14, 13.14], %)`
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
`angledLine(`,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(112, 116),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'length',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(118, 122),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfXLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(277, 281),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3.14',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(283, 287),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfYLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '30',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(321, 323),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '3',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(325, 326),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToX',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '12',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(354, 356),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '12',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(358, 360),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToY',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '30',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(388, 390),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: false,
|
|
|
|
value: '10',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(392, 394),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])('testing %s when inputs are unconstrained', (functionName, expected) => {
|
2025-01-17 14:34:36 -05:00
|
|
|
const ast = assertParse(code)
|
|
|
|
const sourceRange = topLevelRange(
|
2024-05-24 20:54:42 +10:00
|
|
|
code.indexOf(functionName),
|
2025-01-17 14:34:36 -05:00
|
|
|
code.indexOf(functionName) + functionName.length
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast)) return ast
|
2024-05-24 20:54:42 +10:00
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
2024-10-30 16:52:17 -04:00
|
|
|
const callExp = getNodeFromPath<Node<CallExpression>>(
|
2024-05-24 20:54:42 +10:00
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'CallExpression'
|
2024-06-24 11:45:40 -04:00
|
|
|
)
|
|
|
|
if (err(callExp)) return callExp
|
|
|
|
const result = getConstraintInfo(callExp.node, code, pathToNode)
|
2024-05-24 20:54:42 +10:00
|
|
|
expect(result).toEqual(expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('constrained', () => {
|
|
|
|
const code = `const part001 = startSketchOn('-XZ')
|
|
|
|
|> startProfileAt([0, 0], %)
|
|
|
|
|> line([3 + 0, 4 + 0], %)
|
2024-11-25 09:21:55 +13:00
|
|
|
|> angledLine({ angle = 3.14 + 0, length = 3.14 + 0 }, %)
|
2024-05-24 20:54:42 +10:00
|
|
|
|> lineTo([6.14 + 0, 3.14 + 0], %)
|
|
|
|
|> xLineTo(8 + 0, %)
|
|
|
|
|> yLineTo(5 + 0, %)
|
2024-12-06 13:57:31 +13:00
|
|
|
|> yLine(3.14 + 0, %, $a)
|
2024-05-24 20:54:42 +10:00
|
|
|
|> xLine(3.14 + 0, %)
|
2024-11-25 09:21:55 +13:00
|
|
|
|> angledLineOfXLength({ angle = 3.14 + 0, length = 3.14 + 0 }, %)
|
|
|
|
|> angledLineOfYLength({ angle = 30 + 0, length = 3 + 0 }, %)
|
|
|
|
|> angledLineToX({ angle = 12.14 + 0, to = 12 + 0 }, %)
|
|
|
|
|> angledLineToY({ angle = 30 + 0, to = 10.14 + 0 }, %)
|
2024-05-24 20:54:42 +10:00
|
|
|
|> angledLineThatIntersects({
|
2024-11-25 09:21:55 +13:00
|
|
|
angle = 3.14 + 0,
|
2024-12-06 13:57:31 +13:00
|
|
|
intersectTag = a,
|
2024-11-25 09:21:55 +13:00
|
|
|
offset = 0 + 0
|
2024-05-24 20:54:42 +10:00
|
|
|
}, %)
|
|
|
|
|> tangentialArcTo([3.14 + 0, 13.14 + 0], %)`
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'line',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(83, 88),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'line',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '4 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(90, 95),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'line',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
`angledLine(`,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(129, 137),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'length',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(148, 156),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'lineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '6.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(178, 186),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'lineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(188, 196),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'lineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'xLineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'horizontal',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'xLineTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(209, 216),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '8 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(217, 222),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'yLineTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'vertical',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'yLineTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(234, 241),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLineTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '5 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(242, 247),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLineTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'yLine(',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'vertical',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'yLine',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(259, 264),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(265, 273),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'yLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'xLine(',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'horizontal',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'xLine',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(289, 294),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLine',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(295, 303),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'singleValue' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'xLine',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfXLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(345, 353),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(364, 372),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfXLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineOfYLength',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '30 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(416, 422),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yRelative',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(433, 438),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'length' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineOfYLength',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToX',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '12.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(476, 485),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '12 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(492, 498),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'to' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToX',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineToY',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '30 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(536, 542),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '10.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(549, 558),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'to' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineToY',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'angledLineThatIntersects',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'angle',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(616, 624),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'angle' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'intersectionOffset',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '0 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(671, 676),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'objectProperty', key: 'offset' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'intersectionTag',
|
|
|
|
isConstrained: false,
|
2024-12-06 13:57:31 +13:00
|
|
|
value: 'a',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(650, 651),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { key: 'intersectTag', type: 'objectProperty' },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'angledLineThatIntersects',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'tangentialArcTo',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'tangentialWithPrevious',
|
|
|
|
isConstrained: true,
|
|
|
|
value: 'tangentialArcTo',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(697, 712),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: undefined,
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'xAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '3.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(714, 722),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 0 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yAbsolute',
|
|
|
|
isConstrained: true,
|
|
|
|
value: '13.14 + 0',
|
2025-01-17 14:34:36 -05:00
|
|
|
sourceRange: topLevelRange(724, 733),
|
2024-05-24 20:54:42 +10:00
|
|
|
argPosition: { type: 'arrayItem', index: 1 },
|
|
|
|
pathToNode: expect.any(Array),
|
|
|
|
stdLibFnName: 'tangentialArcTo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])('testing %s when inputs are unconstrained', (functionName, expected) => {
|
2025-01-17 14:34:36 -05:00
|
|
|
const ast = assertParse(code)
|
|
|
|
const sourceRange = topLevelRange(
|
2024-05-24 20:54:42 +10:00
|
|
|
code.indexOf(functionName),
|
2025-01-17 14:34:36 -05:00
|
|
|
code.indexOf(functionName) + functionName.length
|
|
|
|
)
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast)) return ast
|
2024-05-24 20:54:42 +10:00
|
|
|
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
2024-10-30 16:52:17 -04:00
|
|
|
const callExp = getNodeFromPath<Node<CallExpression>>(
|
2024-05-24 20:54:42 +10:00
|
|
|
ast,
|
|
|
|
pathToNode,
|
|
|
|
'CallExpression'
|
2024-06-24 11:45:40 -04:00
|
|
|
)
|
|
|
|
if (err(callExp)) return callExp
|
|
|
|
|
|
|
|
const result = getConstraintInfo(callExp.node, code, pathToNode)
|
2024-05-24 20:54:42 +10:00
|
|
|
expect(result).toEqual(expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|