Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ozhanefemeral committed Jul 26, 2024
1 parent 86a5d27 commit 2b06c54
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/generator/__tests__/code-generation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { CodeBlock } from "../../types/blocks";
import { FunctionInfo } from "../../types/common";
import { createFunctionCallBlock } from "../blocks/function-call";
import { createIfBlock } from "../blocks/if-block";
import { generateCode } from "../code-generator";

describe("Code Generation", () => {
test("generates correct code for function calls and if-block", () => {
// Define function infos
const func1Info: FunctionInfo = {
name: "getGreeting",
returnType: "string",
parameters: [],
};

const func2Info: FunctionInfo = {
name: "processGreeting",
returnType: "void",
parameters: [{ name: "greeting", type: "string" }],
};

const func3Info: FunctionInfo = {
name: "handleTrueCondition",
returnType: "void",
parameters: [],
};

const func4Info: FunctionInfo = {
name: "handleFalseCondition",
returnType: "void",
parameters: [],
};

// Create blocks
const blocks: CodeBlock[] = [
createFunctionCallBlock(func1Info, {
blocks: [],
variables: [],
isAsync: false,
}),
createFunctionCallBlock(func2Info, {
blocks: [],
variables: [{ name: "greeting", type: "string", index: 0 }],
isAsync: false,
}),
createIfBlock(
"greeting.length > 0",
[
createFunctionCallBlock(func3Info, {
blocks: [],
variables: [],
isAsync: false,
}),
],
{ blocks: [], variables: [], isAsync: false },
undefined,
{
blocks: [
createFunctionCallBlock(func4Info, {
blocks: [],
variables: [],
isAsync: false,
}),
],
}
),
];

// Generate code
const generatedCode = generateCode(blocks);

// Expected code (formatted for readability)
const expectedCode = `
function generatedFunction() {
const getgreeting = getGreeting();
const processgreeting = processGreeting(greeting);
if (greeting.length > 0) {
const handletruecondition = handleTrueCondition();
}
else {
const handlefalsecondition = handleFalseCondition();
}
}
`.trim();

// Compare generated code with expected code
expect(generatedCode.replace(/\s+/g, " ").trim()).toBe(
expectedCode.replace(/\s+/g, " ").trim()
);
});
});
130 changes: 130 additions & 0 deletions src/generator/__tests__/if.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { CodeBlock } from "../../types/blocks";
import { FunctionInfo } from "../../types/common";
import { CodeGeneratorState } from "../../types/generator";
import { createFunctionCallBlock } from "../blocks/function-call";
import { createIfBlock } from "../blocks/if-block";

describe("If Block Generator", () => {
let initialState: CodeGeneratorState;
let dummyFunctionInfo: FunctionInfo;

beforeEach(() => {
initialState = {
blocks: [],
variables: [],
isAsync: false,
};
dummyFunctionInfo = {
name: "dummyFunction",
returnType: "void",
};
});

test("generates simple if block successfully", () => {
const condition = "x > 0";
const thenBlocks: CodeBlock[] = [
createFunctionCallBlock(dummyFunctionInfo, initialState),
];

const result = createIfBlock(
condition,
thenBlocks,
initialState,
undefined,
undefined
);

expect(result).toBeDefined();
expect(result.blockType).toBe("if");
expect(result.condition).toBe(condition);
expect(result.thenBlocks).toEqual(thenBlocks);
expect(result.elseIfBlocks).toBeUndefined();
expect(result.elseBlock).toBeUndefined();
expect(result.index).toBe(1); // 0 is the dummy function block
});

test("generates if-else if-else block successfully", () => {
const condition = "x > 0";
const thenBlocks: CodeBlock[] = [
createFunctionCallBlock(dummyFunctionInfo, initialState),
];
const elseIfBlocks = [
{
condition: "x < 0",
blocks: [createFunctionCallBlock(dummyFunctionInfo, initialState)],
},
];
const elseBlock = {
blocks: [createFunctionCallBlock(dummyFunctionInfo, initialState)],
};

const result = createIfBlock(
condition,
thenBlocks,
initialState,
elseIfBlocks,
elseBlock
);

expect(result).toBeDefined();
expect(result.blockType).toBe("if");
expect(result.condition).toBe(condition);
expect(result.thenBlocks).toEqual(thenBlocks);
expect(result.elseIfBlocks).toEqual(elseIfBlocks);
expect(result.elseBlock).toEqual(elseBlock);
expect(result.index).toBe(3); // 0, 1, 2 are the dummy function blocks
});

test("generates nested if block successfully", () => {
const outerCondition = "x > 0";
const innerIfBlock = createIfBlock(
"y > 0",
[createFunctionCallBlock(dummyFunctionInfo, initialState)],
initialState,
[
{
condition: "y < 0",
blocks: [createFunctionCallBlock(dummyFunctionInfo, initialState)],
},
],
{
blocks: [createFunctionCallBlock(dummyFunctionInfo, initialState)],
}
);

const result = createIfBlock(
outerCondition,
[innerIfBlock],
initialState,
undefined,
undefined
);

expect(result).toBeDefined();
expect(result.blockType).toBe("if");
expect(result.condition).toBe(outerCondition);
expect(result.thenBlocks).toHaveLength(1);
expect(result.thenBlocks[0]).toBe(innerIfBlock);
expect(result.elseIfBlocks).toBeUndefined();
expect(result.elseBlock).toBeUndefined();
expect(result.index).toBe(4); // 0, 1, 2, 3 are the dummy function blocks and inner if block
});

test("updates state correctly after creating if block", () => {
const condition = "x > 0";
const thenBlocks: CodeBlock[] = [
createFunctionCallBlock(dummyFunctionInfo, initialState),
];

const result = createIfBlock(
condition,
thenBlocks,
initialState,
undefined,
undefined
);

expect(initialState.blocks).toHaveLength(2);
expect(initialState.blocks[1]).toBe(result);
});
});

0 comments on commit 2b06c54

Please sign in to comment.