ProgramMemory refactor - eliminate copies of memory (#5273)
* refactor program memory for encapsulation and remove special treatment of return values Signed-off-by: Nick Cameron <nrc@ncameron.org> * Refactor ProgramMemory to isolate mutation Signed-off-by: Nick Cameron <nrc@ncameron.org> * Refactor ProgramMemory Signed-off-by: Nick Cameron <nrc@ncameron.org> * Generated output Signed-off-by: Nick Cameron <nrc@ncameron.org> * Cache the result of executing modules for their items Signed-off-by: Nick Cameron <nrc@ncameron.org> * Compress envs when popped Signed-off-by: Nick Cameron <nrc@ncameron.org> * Remove the last traces of geometry from the memory module Signed-off-by: Nick Cameron <nrc@ncameron.org> * docs Signed-off-by: Nick Cameron <nrc@ncameron.org> * Fixups Signed-off-by: Nick Cameron <nrc@ncameron.org> * Frontend Signed-off-by: Nick Cameron <nrc@ncameron.org> * Improve Environment GC Signed-off-by: Nick Cameron <nrc@ncameron.org> * Fixup mock execution frontend and interpreter, misc bug fixes Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -1,67 +1,55 @@
|
||||
import { ParseResult, ProgramMemory } from 'lang/wasm'
|
||||
import { ParseResult, VariableMap } from 'lang/wasm'
|
||||
import { getCalculatedKclExpressionValue } from './kclHelpers'
|
||||
|
||||
describe('KCL expression calculations', () => {
|
||||
it('calculates a simple expression', async () => {
|
||||
const actual = await getCalculatedKclExpressionValue({
|
||||
value: '1 + 2',
|
||||
programMemory: ProgramMemory.empty(),
|
||||
})
|
||||
const actual = await getCalculatedKclExpressionValue('1 + 2', {})
|
||||
const coercedActual = actual as Exclude<typeof actual, Error | ParseResult>
|
||||
expect(coercedActual).not.toHaveProperty('errors')
|
||||
expect(coercedActual.valueAsString).toEqual('3')
|
||||
expect(coercedActual?.astNode).toBeDefined()
|
||||
})
|
||||
it('calculates a simple expression with a variable', async () => {
|
||||
const programMemory = ProgramMemory.empty()
|
||||
programMemory.set('x', {
|
||||
const variables: VariableMap = {}
|
||||
variables['x'] = {
|
||||
type: 'Number',
|
||||
value: 2,
|
||||
__meta: [],
|
||||
})
|
||||
const actual = await getCalculatedKclExpressionValue({
|
||||
value: '1 + x',
|
||||
programMemory,
|
||||
})
|
||||
}
|
||||
const actual = await getCalculatedKclExpressionValue('1 + x', variables)
|
||||
const coercedActual = actual as Exclude<typeof actual, Error | ParseResult>
|
||||
expect(coercedActual.valueAsString).toEqual('3')
|
||||
expect(coercedActual.astNode).toBeDefined()
|
||||
})
|
||||
it('returns NAN for an invalid expression', async () => {
|
||||
const actual = await getCalculatedKclExpressionValue({
|
||||
value: '1 + x',
|
||||
programMemory: ProgramMemory.empty(),
|
||||
})
|
||||
const actual = await getCalculatedKclExpressionValue('1 + x', {})
|
||||
const coercedActual = actual as Exclude<typeof actual, Error | ParseResult>
|
||||
expect(coercedActual.valueAsString).toEqual('NAN')
|
||||
expect(coercedActual.astNode).toBeDefined()
|
||||
})
|
||||
it('returns NAN for an expression with an invalid variable', async () => {
|
||||
const programMemory = ProgramMemory.empty()
|
||||
programMemory.set('y', {
|
||||
const variables: VariableMap = {}
|
||||
variables['y'] = {
|
||||
type: 'Number',
|
||||
value: 2,
|
||||
__meta: [],
|
||||
})
|
||||
const actual = await getCalculatedKclExpressionValue({
|
||||
value: '1 + x',
|
||||
programMemory,
|
||||
})
|
||||
}
|
||||
const actual = await getCalculatedKclExpressionValue('1 + x', variables)
|
||||
const coercedActual = actual as Exclude<typeof actual, Error | ParseResult>
|
||||
expect(coercedActual.valueAsString).toEqual('NAN')
|
||||
expect(coercedActual.astNode).toBeDefined()
|
||||
})
|
||||
it('calculates a more complex expression with a variable', async () => {
|
||||
const programMemory = ProgramMemory.empty()
|
||||
programMemory.set('x', {
|
||||
const variables: VariableMap = {}
|
||||
variables['x'] = {
|
||||
type: 'Number',
|
||||
value: 2,
|
||||
__meta: [],
|
||||
})
|
||||
const actual = await getCalculatedKclExpressionValue({
|
||||
value: '(1 + x * x) * 2',
|
||||
programMemory,
|
||||
})
|
||||
}
|
||||
const actual = await getCalculatedKclExpressionValue(
|
||||
'(1 + x * x) * 2',
|
||||
variables
|
||||
)
|
||||
const coercedActual = actual as Exclude<typeof actual, Error | ParseResult>
|
||||
expect(coercedActual.valueAsString).toEqual('10')
|
||||
expect(coercedActual.astNode).toBeDefined()
|
||||
|
Reference in New Issue
Block a user