fix unit tests

This commit is contained in:
Kurt Hutten Irev-Dev
2024-09-09 11:52:23 +10:00
parent 6753a9e9f8
commit 998b194712
3 changed files with 51 additions and 32 deletions

View File

@ -641,13 +641,22 @@ describe('Testing removeSingleConstraintInfo', () => {
const pathToNode = getNodePathFromSourceRange(ast, range) const pathToNode = getNodePathFromSourceRange(ast, range)
let argPosition: SimplifiedVarValue let argPosition: SimplifiedVarValue
if (key === 'arrayIndex' && typeof value === 'number') { if (key === 'arrayIndex' && typeof value === 'number') {
argPosition = { type: 'arrayItem', argIndex: 0, index: value ? 0 : 1 } argPosition = {
type: 'arrayItem',
index: value === 0 ? 0 : 1,
argIndex: 0,
}
} else if (key === 'objectProperty' && typeof value === 'string') { } else if (key === 'objectProperty' && typeof value === 'string') {
argPosition = { argPosition = {
type: 'objectProperty', type: 'objectProperty',
key: value as VarValueKeys, key: value as VarValueKeys,
argIndex: 0, argIndex: 0,
} }
} else if (key === '') {
argPosition = {
type: 'singleValue',
argIndex: 0,
}
} else { } else {
throw new Error('argPosition is undefined') throw new Error('argPosition is undefined')
} }
@ -688,7 +697,11 @@ describe('Testing removeSingleConstraintInfo', () => {
] ]
let argPosition: SimplifiedVarValue let argPosition: SimplifiedVarValue
if (key === 'arrayIndex' && typeof value === 'number') { if (key === 'arrayIndex' && typeof value === 'number') {
argPosition = { type: 'arrayItem', argIndex: 0, index: value ? 0 : 1 } argPosition = {
type: 'arrayItem',
argIndex: 0,
index: value === 0 ? 0 : 1,
}
} else if (key === 'objectProperty' && typeof value === 'string') { } else if (key === 'objectProperty' && typeof value === 'string') {
argPosition = { argPosition = {
type: 'objectProperty', type: 'objectProperty',

View File

@ -131,12 +131,10 @@ const constrainInfo = (
argPosition: argPosition:
g === 'singleValue' g === 'singleValue'
? { type: 'singleValue', argIndex: 0 } ? { type: 'singleValue', argIndex: 0 }
: g === 1 || g === 0 : typeof g === 'number'
? { type: 'arrayItem', index: g, argIndex: 0 } ? { type: 'arrayItem', index: g, argIndex: 0 }
: typeof g === 'string' : typeof g === 'string'
? { type: 'objectProperty', key: g, argIndex: 0 } ? { type: 'objectProperty', key: g, argIndex: 0 }
: g && 'type' in g
? g
: undefined, : undefined,
pathToNode: e, pathToNode: e,
stdLibFnName: f, stdLibFnName: f,
@ -163,8 +161,12 @@ const commonConstraintInfoHelper = (
const firstArg = callExp.arguments?.[0] const firstArg = callExp.arguments?.[0]
const isArr = firstArg.type === 'ArrayExpression' const isArr = firstArg.type === 'ArrayExpression'
if (!isArr && firstArg.type !== 'ObjectExpression') return [] if (!isArr && firstArg.type !== 'ObjectExpression') return []
const pipeExpressionIndex = pathToNode.findIndex(
(x) => x[1] === 'PipeExpression'
)
const pathToBase = pathToNode.slice(0, pipeExpressionIndex + 2)
const pathToArrayExpression: PathToNode = [ const pathToArrayExpression: PathToNode = [
...pathToNode, ...pathToBase,
['arguments', 'CallExpression'], ['arguments', 'CallExpression'],
[0, 'index'], [0, 'index'],
isArr isArr
@ -463,7 +465,7 @@ export const line: SketchLineHelper = {
varExpression: createLiteral(roundOff(to[1] - from[1], 2)), varExpression: createLiteral(roundOff(to[1] - from[1], 2)),
varDetails: { varDetails: {
type: 'arrayItem', type: 'arrayItem',
index: 0, index: 1,
argType: 'yRelative', argType: 'yRelative',
value: createLiteral(roundOff(to[1] - from[1], 2)), value: createLiteral(roundOff(to[1] - from[1], 2)),
argIndex: 0, argIndex: 0,

View File

@ -442,9 +442,8 @@ const setHorVertDistanceForXYLines =
const setHorzVertDistanceConstraintLineCreateNode = const setHorzVertDistanceConstraintLineCreateNode =
(isX: boolean): TransformInfo['createNode'] => (isX: boolean): TransformInfo['createNode'] =>
({ referenceSegName, tag, inputs }) => { ({ referenceSegName, tag, inputs }) => {
const varVal = ( let varVal = isX ? inputs[1].varExpression : inputs[0].varExpression
isX ? inputs[0].varExpression : inputs[1].varExpression varVal = isExprBinaryPart(varVal) ? varVal : createLiteral(0)
) as BinaryPart
const varValBinExp = createBinaryExpressionWithUnary([ const varValBinExp = createBinaryExpressionWithUnary([
createLastSeg(!isX), createLastSeg(!isX),
varVal, varVal,
@ -641,7 +640,7 @@ const transformMap: TransformMap = {
createNode: createNode:
({ tag }) => ({ tag }) =>
(args) => (args) =>
createCallWrapper('yLine', args[1].varExpression, tag), createCallWrapper('yLine', args[0].varExpression, tag),
}, },
setHorzDistance: { setHorzDistance: {
tooltip: 'lineTo', tooltip: 'lineTo',
@ -699,7 +698,7 @@ const transformMap: TransformMap = {
createNode: createNode:
({ tag }) => ({ tag }) =>
(args) => (args) =>
createCallWrapper('yLineTo', args[1].varExpression, tag), createCallWrapper('yLineTo', args[0].varExpression, tag),
}, },
}, },
xAbsolute: { xAbsolute: {
@ -849,7 +848,7 @@ const transformMap: TransformMap = {
createNode: createNode:
({ tag }) => ({ tag }) =>
(args) => (args) =>
createCallWrapper('yLine', args[1].varExpression, tag), createCallWrapper('yLine', args[0].varExpression, tag),
}, },
horizontal: { horizontal: {
tooltip: 'xLine', tooltip: 'xLine',
@ -940,9 +939,12 @@ const transformMap: TransformMap = {
equalLength: { equalLength: {
tooltip: 'angledLineOfXLength', tooltip: 'angledLineOfXLength',
createNode: ({ referenceSegName, inputs, tag }) => { createNode: ({ referenceSegName, inputs, tag }) => {
const input =
inputs.find((a) => a.varDetails.argType === 'xRelative') ||
inputs[0]
const [minVal, legAngle] = getMinAndSegAngVals( const [minVal, legAngle] = getMinAndSegAngVals(
referenceSegName, referenceSegName,
inputs[0].varExpression input.varExpression
) )
return (args) => return (args) =>
createCallWrapper( createCallWrapper(
@ -988,7 +990,7 @@ const transformMap: TransformMap = {
createNode: createNode:
({ tag }) => ({ tag }) =>
(args) => (args) =>
createCallWrapper('yLine', args[1].varExpression, tag), createCallWrapper('yLine', args[0].varExpression, tag),
}, },
}, },
angle: { angle: {
@ -1106,7 +1108,7 @@ const transformMap: TransformMap = {
createNode: createNode:
({ tag }) => ({ tag }) =>
(args) => (args) =>
createCallWrapper('yLineTo', args[1].varExpression, tag), createCallWrapper('yLineTo', args[0].varExpression, tag),
}, },
}, },
angle: { angle: {
@ -1371,22 +1373,23 @@ export function removeSingleConstraint({
if (inputDetails.type === 'arrayItem') { if (inputDetails.type === 'arrayItem') {
const values = inputs.map(({ varDetails: varValue }) => { const values = inputs.map(({ varDetails: varValue }) => {
if ( if (
(varValue.type === 'arrayItem' || !(
varValue.type === 'arrayOrObjItem') && (varValue.type === 'arrayItem' ||
varValue.index === inputDetails.index varValue.type === 'arrayOrObjItem') &&
) { varValue.index === inputDetails.index
const literal = rawValues.find(
(rawValue) =>
(rawValue.varDetails.type === 'arrayItem' ||
rawValue.varDetails.type === 'arrayOrObjItem') &&
rawValue.varDetails.index === inputDetails.index
)?.varDetails?.value
return (
(varValue.index === inputDetails.index && literal) ||
varValue.value
) )
} )
return varValue.value return varValue.value
const literal = rawValues.find(
(rawValue) =>
(rawValue.varDetails.type === 'arrayItem' ||
rawValue.varDetails.type === 'arrayOrObjItem') &&
rawValue.varDetails.index === inputDetails.index
)?.varDetails?.value
return (
(varValue.index === inputDetails.index && literal) ||
varValue.value
)
}) })
return createStdlibCallExpression( return createStdlibCallExpression(
callExp.node.callee.name as any, callExp.node.callee.name as any,
@ -1440,7 +1443,8 @@ export function removeSingleConstraint({
] = rawLiteralArrayInObject.varDetails.value ] = rawLiteralArrayInObject.varDetails.value
} else if ( } else if (
inputDetails.type === 'objectProperty' && inputDetails.type === 'objectProperty' &&
rawLiteralObjProp?.varDetails.type === 'objectProperty' && (rawLiteralObjProp?.varDetails.type === 'objectProperty' ||
rawLiteralObjProp?.varDetails.type === 'arrayOrObjItem') &&
rawLiteralObjProp?.varDetails.key === inputDetails.key && rawLiteralObjProp?.varDetails.key === inputDetails.key &&
varValue.key === inputDetails.key varValue.key === inputDetails.key
) { ) {