Files
modeling-app/src/lang/executor.test.ts

91 lines
2.3 KiB
TypeScript
Raw Normal View History

import fs from "node:fs";
2022-11-14 14:04:23 +11:00
import { abstractSyntaxTree } from "./abstractSyntaxTree";
import { lexer } from "./tokeniser";
import { executor } from "./executor";
describe("test", () => {
it("test assigning two variables, the second summing with the first", () => {
const code = `const myVar = 5
const newVar = myVar + 1`;
const programMemory = exe(code);
2022-11-14 14:04:23 +11:00
expect(withoutStdFns(programMemory)).toEqual({
root: {
myVar: 5,
newVar: 6,
},
});
});
it("test assigning a var with a string", () => {
const code = `const myVar = "a str"`;
const programMemory = exe(code);
2022-11-14 14:04:23 +11:00
expect(withoutStdFns(programMemory)).toEqual({
root: {
myVar: "a str",
},
});
});
it("test assigning a var by cont concatenating two strings string", () => {
const code = fs.readFileSync(
"./src/lang/testExamples/variableDeclaration.cado",
"utf-8"
);
const programMemory = exe(code);
2022-11-14 14:04:23 +11:00
expect(withoutStdFns(programMemory)).toEqual({
root: {
myVar: "a str another str",
},
});
});
it("test with function call", () => {
const code = `
const myVar = "hello"
log(5, myVar)`;
2022-11-14 14:04:23 +11:00
const programMemoryOverride = {
log: jest.fn(),
};
const programMemory = executor(abstractSyntaxTree(lexer(code)), {
root: programMemoryOverride,
});
2022-11-14 14:04:23 +11:00
expect(withoutStdFns(programMemory)).toEqual({
root: { myVar: "hello" },
2022-11-14 14:04:23 +11:00
});
expect(programMemoryOverride.log).toHaveBeenCalledWith(5, "hello");
});
it("fn funcN = () => {}", () => {
const programMemory = exe(
[
"fn funcN = (a, b) => {",
" return a + b",
"}",
"const theVar = 5",
"const myVar = funcN(1, theVar)",
].join("\n")
);
expect(withoutStdFns(programMemory, ["funcN"])).toEqual({
root: { theVar: 5, myVar: 6 },
});
});
2022-11-14 14:04:23 +11:00
});
// helpers
function exe(
code: string,
programMemory: { root: { [key: string]: any }; return?: any } = { root: {} }
) {
const tokens = lexer(code);
const ast = abstractSyntaxTree(tokens);
return executor(ast, programMemory);
}
function withoutStdFns(obj: any, toDelete: string[] = []) {
2022-11-14 14:04:23 +11:00
const newRoot = { ...obj.root };
const newObj = { ...obj, root: newRoot };
delete newObj.root.log;
toDelete.forEach((key) => {
delete newObj.root[key];
});
2022-11-14 14:04:23 +11:00
return newObj;
}