Skip to content

Enable switch statements #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions src/ast/__tests__/expression-extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,142 @@ describe("extract ClassInstanceCreationExpression correctly", () => {
expect(ast).toEqual(expectedAst);
});
});

describe("extract CastExpression correctly", () => {
it("extract CastExpression int to char correctly", () => {
const programStr = `
class Test {
void test() {
char c = (char) 65;
}
}
`;

const expectedAst: AST = {
kind: "CompilationUnit",
importDeclarations: [],
topLevelClassOrInterfaceDeclarations: [
{
kind: "NormalClassDeclaration",
classModifier: [],
typeIdentifier: "Test",
classBody: [
{
kind: "MethodDeclaration",
methodModifier: [],
methodHeader: {
result: "void",
identifier: "test",
formalParameterList: [],
},
methodBody: {
kind: "Block",
blockStatements: [
{
kind: "LocalVariableDeclarationStatement",
localVariableType: "char",
variableDeclaratorList: [
{
kind: "VariableDeclarator",
variableDeclaratorId: "c",
variableInitializer: {
kind: "CastExpression",
type: "char",
expression: {
kind: "Literal",
literalType: {
kind: "DecimalIntegerLiteral",
value: "65",
},
location: expect.anything(),
},
location: expect.anything(),
},
},
],
location: expect.anything(),
},
],
location: expect.anything(),
},
location: expect.anything(),
},
],
location: expect.anything(),
},
],
location: expect.anything(),
};

const ast = parse(programStr);
expect(ast).toEqual(expectedAst);
});

it("extract CastExpression double to int correctly", () => {
const programStr = `
class Test {
void test() {
int x = (int) 3.14;
}
}
`;

const expectedAst: AST = {
kind: "CompilationUnit",
importDeclarations: [],
topLevelClassOrInterfaceDeclarations: [
{
kind: "NormalClassDeclaration",
classModifier: [],
typeIdentifier: "Test",
classBody: [
{
kind: "MethodDeclaration",
methodModifier: [],
methodHeader: {
result: "void",
identifier: "test",
formalParameterList: [],
},
methodBody: {
kind: "Block",
blockStatements: [
{
kind: "LocalVariableDeclarationStatement",
localVariableType: "int",
variableDeclaratorList: [
{
kind: "VariableDeclarator",
variableDeclaratorId: "x",
variableInitializer: {
kind: "CastExpression",
type: "int",
expression: {
kind: "Literal",
literalType: {
kind: "DecimalFloatingPointLiteral",
value: "3.14",
}
},
location: expect.anything(),
},
},
],
location: expect.anything(),
},
],
location: expect.anything(),
},
location: expect.anything(),
},
],
location: expect.anything(),
},
],
location: expect.anything(),
};

const ast = parse(programStr);
expect(ast).toEqual(expectedAst);
});
});
Loading
Loading