cps-interpreter/test/interpreter.spec.ts

48 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

import { expect, test } from 'bun:test';
import { TestPrograms } from './programs';
import { peggyParse } from '@/parser';
import { evaluate } from '@/interpreter';
import { testingLogger } from './logger';
test('Add (1 real) and (3 int) => (4 real)', async () => {
const ast = peggyParse(await TestPrograms.AddOneThree);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'real', value: 4 });
});
test('Add (1 real) and (3 int) -> result => (real 1 - result) = -3 done with correct lexical scope', async () => {
const ast = peggyParse(await TestPrograms.PrimopScope);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'real', value: -3 });
});
2024-02-28 15:41:53 -05:00
test('Branching', async () => {
const ast = peggyParse(await TestPrograms.Branching);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'real', value: 2 });
});
test('String equality', async () => {
const ast = peggyParse(await TestPrograms.StringEquality);
const result = await evaluate(ast, testingLogger);
2024-02-28 17:06:00 -05:00
expect(result).toEqual({ type: 'bool', value: 1 });
});
test('String inequality', async () => {
const ast = peggyParse(await TestPrograms.StringInEquality);
const result = await evaluate(ast, testingLogger);
2024-02-28 17:06:00 -05:00
expect(result).toEqual({ type: 'bool', value: 0 });
});
test('Application of identity function', async () => {
const ast = peggyParse(await TestPrograms.Application);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'int', value: 3 });
});