chore: adding unit test to test getNodeFromPath
failures (#5134)
* chore: adding unit test to test the parsing of the code * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * chore: trying to console error more content for getNodeByPath since it is cryptic * fix: removing testing unit test * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * fix: fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * bump * fix: ... * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * bump * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * bump * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * bump * fix: found da bug --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Binary file not shown.
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
Binary file not shown.
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 47 KiB |
@ -5,7 +5,11 @@ import {
|
|||||||
PathToNode,
|
PathToNode,
|
||||||
Identifier,
|
Identifier,
|
||||||
topLevelRange,
|
topLevelRange,
|
||||||
|
PipeExpression,
|
||||||
|
CallExpression,
|
||||||
|
VariableDeclarator,
|
||||||
} from './wasm'
|
} from './wasm'
|
||||||
|
import { ProgramMemory } from 'lang/wasm'
|
||||||
import {
|
import {
|
||||||
findAllPreviousVariables,
|
findAllPreviousVariables,
|
||||||
isNodeSafeToReplace,
|
isNodeSafeToReplace,
|
||||||
@ -25,9 +29,11 @@ import {
|
|||||||
createCallExpression,
|
createCallExpression,
|
||||||
createLiteral,
|
createLiteral,
|
||||||
createPipeSubstitution,
|
createPipeSubstitution,
|
||||||
|
createCallExpressionStdLib,
|
||||||
} from './modifyAst'
|
} from './modifyAst'
|
||||||
import { err } from 'lib/trap'
|
import { err } from 'lib/trap'
|
||||||
import { codeRefFromRange } from './std/artifactGraph'
|
import { codeRefFromRange } from './std/artifactGraph'
|
||||||
|
import { addCallExpressionsToPipe, addCloseToPipe } from 'lang/std/sketch'
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await initPromise
|
await initPromise
|
||||||
@ -680,3 +686,115 @@ myNestedVar = [
|
|||||||
expect(pathToNode).toEqual(pathToNode2)
|
expect(pathToNode).toEqual(pathToNode2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Testing specific sketch getNodeFromPath workflow', () => {
|
||||||
|
it('should parse the code', () => {
|
||||||
|
const openSketch = `sketch001 = startSketchOn('XZ')
|
||||||
|
|> startProfileAt([0.02, 0.22], %)
|
||||||
|
|> xLine(0.39, %)
|
||||||
|
|> line([0.02, -0.17], %)
|
||||||
|
|> yLine(-0.15, %)
|
||||||
|
|> line([-0.21, -0.02], %)
|
||||||
|
|> xLine(-0.15, %)
|
||||||
|
|> line([-0.02, 0.21], %)
|
||||||
|
|> line([-0.08, 0.05], %)`
|
||||||
|
const ast = assertParse(openSketch)
|
||||||
|
expect(ast.start).toEqual(0)
|
||||||
|
expect(ast.end).toEqual(227)
|
||||||
|
})
|
||||||
|
it('should find the location to add new lineTo', () => {
|
||||||
|
const openSketch = `sketch001 = startSketchOn('XZ')
|
||||||
|
|> startProfileAt([0.02, 0.22], %)
|
||||||
|
|> xLine(0.39, %)
|
||||||
|
|> line([0.02, -0.17], %)
|
||||||
|
|> yLine(-0.15, %)
|
||||||
|
|> line([-0.21, -0.02], %)
|
||||||
|
|> xLine(-0.15, %)
|
||||||
|
|> line([-0.02, 0.21], %)
|
||||||
|
|> line([-0.08, 0.05], %)`
|
||||||
|
const ast = assertParse(openSketch)
|
||||||
|
|
||||||
|
const sketchSnippet = `startProfileAt([0.02, 0.22], %)`
|
||||||
|
const sketchRange = topLevelRange(
|
||||||
|
openSketch.indexOf(sketchSnippet),
|
||||||
|
openSketch.indexOf(sketchSnippet) + sketchSnippet.length
|
||||||
|
)
|
||||||
|
const sketchPathToNode = getNodePathFromSourceRange(ast, sketchRange)
|
||||||
|
const modifiedAst = addCallExpressionsToPipe({
|
||||||
|
node: ast,
|
||||||
|
programMemory: ProgramMemory.empty(),
|
||||||
|
pathToNode: sketchPathToNode,
|
||||||
|
expressions: [
|
||||||
|
createCallExpressionStdLib(
|
||||||
|
'lineTo', // We are forcing lineTo!
|
||||||
|
[
|
||||||
|
createArrayExpression([
|
||||||
|
createCallExpressionStdLib('profileStartX', [
|
||||||
|
createPipeSubstitution(),
|
||||||
|
]),
|
||||||
|
createCallExpressionStdLib('profileStartY', [
|
||||||
|
createPipeSubstitution(),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
createPipeSubstitution(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
if (err(modifiedAst)) throw modifiedAst
|
||||||
|
const recasted = recast(modifiedAst)
|
||||||
|
const expectedCode = `sketch001 = startSketchOn('XZ')
|
||||||
|
|> startProfileAt([0.02, 0.22], %)
|
||||||
|
|> xLine(0.39, %)
|
||||||
|
|> line([0.02, -0.17], %)
|
||||||
|
|> yLine(-0.15, %)
|
||||||
|
|> line([-0.21, -0.02], %)
|
||||||
|
|> xLine(-0.15, %)
|
||||||
|
|> line([-0.02, 0.21], %)
|
||||||
|
|> line([-0.08, 0.05], %)
|
||||||
|
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||||
|
`
|
||||||
|
expect(recasted).toEqual(expectedCode)
|
||||||
|
})
|
||||||
|
it('it should find the location to add close', () => {
|
||||||
|
const openSketch = `sketch001 = startSketchOn('XZ')
|
||||||
|
|> startProfileAt([0.02, 0.22], %)
|
||||||
|
|> xLine(0.39, %)
|
||||||
|
|> line([0.02, -0.17], %)
|
||||||
|
|> yLine(-0.15, %)
|
||||||
|
|> line([-0.21, -0.02], %)
|
||||||
|
|> xLine(-0.15, %)
|
||||||
|
|> line([-0.02, 0.21], %)
|
||||||
|
|> line([-0.08, 0.05], %)
|
||||||
|
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||||
|
`
|
||||||
|
const ast = assertParse(openSketch)
|
||||||
|
const sketchSnippet = `startProfileAt([0.02, 0.22], %)`
|
||||||
|
const sketchRange = topLevelRange(
|
||||||
|
openSketch.indexOf(sketchSnippet),
|
||||||
|
openSketch.indexOf(sketchSnippet) + sketchSnippet.length
|
||||||
|
)
|
||||||
|
const sketchPathToNode = getNodePathFromSourceRange(ast, sketchRange)
|
||||||
|
const modifiedAst = addCloseToPipe({
|
||||||
|
node: ast,
|
||||||
|
programMemory: ProgramMemory.empty(),
|
||||||
|
pathToNode: sketchPathToNode,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (err(modifiedAst)) throw modifiedAst
|
||||||
|
const recasted = recast(modifiedAst)
|
||||||
|
const expectedCode = `sketch001 = startSketchOn('XZ')
|
||||||
|
|> startProfileAt([0.02, 0.22], %)
|
||||||
|
|> xLine(0.39, %)
|
||||||
|
|> line([0.02, -0.17], %)
|
||||||
|
|> yLine(-0.15, %)
|
||||||
|
|> line([-0.21, -0.02], %)
|
||||||
|
|> xLine(-0.15, %)
|
||||||
|
|> line([-0.02, 0.21], %)
|
||||||
|
|> line([-0.08, 0.05], %)
|
||||||
|
|> lineTo([profileStartX(%), profileStartY(%)], %)
|
||||||
|
|> close(%)
|
||||||
|
`
|
||||||
|
expect(recasted).toEqual(expectedCode)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
@ -21,6 +21,7 @@ import {
|
|||||||
topLevelRange,
|
topLevelRange,
|
||||||
VariableDeclaration,
|
VariableDeclaration,
|
||||||
VariableDeclarator,
|
VariableDeclarator,
|
||||||
|
recast,
|
||||||
} from './wasm'
|
} from './wasm'
|
||||||
import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
|
import { getNodePathFromSourceRange } from 'lang/queryAstNodePathUtils'
|
||||||
import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
|
import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
|
||||||
@ -68,7 +69,28 @@ export function getNodeFromPath<T>(
|
|||||||
deepPath: successfulPaths,
|
deepPath: successfulPaths,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Error('not an object')
|
const stackTraceError = new Error()
|
||||||
|
const sourceCode = recast(node)
|
||||||
|
const levels = stackTraceError.stack?.split('\n')
|
||||||
|
const aFewFunctionNames: string[] = []
|
||||||
|
let tree = ''
|
||||||
|
levels?.forEach((val, index) => {
|
||||||
|
const fnName = val.trim().split(' ')[1]
|
||||||
|
const ending = index === levels.length - 1 ? ' ' : ' > '
|
||||||
|
tree += fnName + ending
|
||||||
|
if (index < 3) {
|
||||||
|
aFewFunctionNames.push(fnName)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const error = new Error(
|
||||||
|
`Failed to stopAt ${stopAt}, ${aFewFunctionNames
|
||||||
|
.filter((a) => a)
|
||||||
|
.join(' > ')}`
|
||||||
|
)
|
||||||
|
console.error(tree)
|
||||||
|
console.error(sourceCode)
|
||||||
|
console.error(error.stack)
|
||||||
|
return error
|
||||||
}
|
}
|
||||||
currentNode = currentNode?.[pathItem[0]]
|
currentNode = currentNode?.[pathItem[0]]
|
||||||
successfulPaths.push(pathItem)
|
successfulPaths.push(pathItem)
|
||||||
|
@ -2561,7 +2561,7 @@ export const modelingMachine = setup({
|
|||||||
|
|
||||||
'Delete segment': {
|
'Delete segment': {
|
||||||
reenter: false,
|
reenter: false,
|
||||||
actions: ['Delete segment', 'Set sketchDetails'],
|
actions: ['Delete segment', 'Set sketchDetails', 'reset selections'],
|
||||||
},
|
},
|
||||||
'code edit during sketch': '.clean slate',
|
'code edit during sketch': '.clean slate',
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user