2024-10-30 16:52:17 -04:00
|
|
|
import { err } from 'lib/trap'
|
2025-01-31 10:45:39 -05:00
|
|
|
import { formatNumber, initPromise, parse, ParseResult } from './wasm'
|
2024-10-30 16:52:17 -04:00
|
|
|
import { enginelessExecutor } from 'lib/testHelpers'
|
2024-12-06 13:57:31 +13:00
|
|
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
|
|
|
import { Program } from '../wasm-lib/kcl/bindings/Program'
|
2024-10-30 16:52:17 -04:00
|
|
|
|
2024-12-10 15:38:51 -05:00
|
|
|
beforeEach(async () => {
|
|
|
|
await initPromise
|
|
|
|
})
|
|
|
|
|
2024-10-30 16:52:17 -04:00
|
|
|
it('can execute parsed AST', async () => {
|
|
|
|
const code = `x = 1
|
|
|
|
// A comment.`
|
2024-12-06 13:57:31 +13:00
|
|
|
const result = parse(code)
|
|
|
|
expect(err(result)).toEqual(false)
|
|
|
|
const pResult = result as ParseResult
|
|
|
|
expect(pResult.errors.length).toEqual(0)
|
|
|
|
expect(pResult.program).not.toEqual(null)
|
|
|
|
const execState = await enginelessExecutor(pResult.program as Node<Program>)
|
|
|
|
expect(err(execState)).toEqual(false)
|
2024-10-30 16:52:17 -04:00
|
|
|
expect(execState.memory.get('x')?.value).toEqual(1)
|
|
|
|
})
|
2025-01-31 10:45:39 -05:00
|
|
|
|
|
|
|
it('formats numbers with units', () => {
|
|
|
|
expect(formatNumber(1, 'None')).toEqual('1')
|
|
|
|
expect(formatNumber(1, 'Count')).toEqual('1_')
|
|
|
|
expect(formatNumber(1, 'Mm')).toEqual('1mm')
|
|
|
|
expect(formatNumber(1, 'Inch')).toEqual('1in')
|
|
|
|
expect(formatNumber(0.5, 'Mm')).toEqual('0.5mm')
|
|
|
|
expect(formatNumber(-0.5, 'Mm')).toEqual('-0.5mm')
|
|
|
|
})
|