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)
let argPosition: SimplifiedVarValue
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') {
argPosition = {
type: 'objectProperty',
key: value as VarValueKeys,
argIndex: 0,
}
} else if (key === '') {
argPosition = {
type: 'singleValue',
argIndex: 0,
}
} else {
throw new Error('argPosition is undefined')
}
@ -688,7 +697,11 @@ describe('Testing removeSingleConstraintInfo', () => {
]
let argPosition: SimplifiedVarValue
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') {
argPosition = {
type: 'objectProperty',

View File

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

View File

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