diff --git a/src/test/java/main/E2EReflectionsTest.java b/src/test/java/main/E2EReflectionsTest.java new file mode 100644 index 0000000..453d7b3 --- /dev/null +++ b/src/test/java/main/E2EReflectionsTest.java @@ -0,0 +1,177 @@ +package main; + +import ast.ASTNode; +import ast.ProgramNode; +import bytecode.ByteCodeGenerator; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTree; +import parser.astBuilder.ASTBuilder; +import parser.generated.SimpleJavaLexer; +import parser.generated.SimpleJavaParser; +import semantic.SemanticAnalyzer; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.lang.reflect.Method; +import java.util.ArrayList; + +public class E2EReflectionsTest { + + + private CharStream mockInputCharStream; + private String outputDirectoryPath; + private SimpleJavaLexer mockLexer; + private CommonTokenStream mockTokenStream; + private SimpleJavaParser mockParser; + private ParseTree mockParseTree; + private ASTBuilder mockASTBuilder; + private ASTNode mockASTNode; + private SemanticAnalyzer mockSemanticAnalyzer; + private ASTNode mockTypedAST; + private ByteCodeGenerator mockByteCodeGenerator; + + @BeforeEach + public void setUp() { + mockInputCharStream = mock(CharStream.class); + outputDirectoryPath = "path/to/output"; + mockLexer = mock(SimpleJavaLexer.class); + mockTokenStream = mock(CommonTokenStream.class); + mockParser = mock(SimpleJavaParser.class); + mockParseTree = mock(ParseTree.class); + mockASTBuilder = mock(ASTBuilder.class); + mockASTNode = mock(ASTNode.class); + mockSemanticAnalyzer = mock(SemanticAnalyzer.class); + mockTypedAST = mock(ASTNode.class); + mockByteCodeGenerator = mock(ByteCodeGenerator.class); + } + + @Test + public void testCompileFile() throws Exception { + // Mock the dependencies + SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class); + CommonTokenStream mockTokenStream = mock(CommonTokenStream.class); + SimpleJavaParser mockParser = mock(SimpleJavaParser.class); + ParseTree mockParseTree = mock(ParseTree.class); + ASTBuilder mockASTBuilder = mock(ASTBuilder.class); + ASTNode mockASTNode = mock(ASTNode.class); + SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class); + ASTNode mockTypedAST = mock(ASTNode.class); + ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class); + + // Mock the behavior + when(mockLexer.nextToken()).thenReturn(null); + when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>()); + when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree); + when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode); + when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST); + + // Use reflection to invoke the compileFile method + Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class); + compileFileMethod.setAccessible(true); + + compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath); + + // Verify each step + verify(mockLexer, times(1)).nextToken(); + verify(mockTokenStream, times(1)).getTokens(); + verify(mockParser, times(1)).program(); + verify(mockASTBuilder, times(1)).visit(mockParseTree); + verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode); + verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST); + } + + @Test + public void testCompileFile2() throws Exception { + // Mock the behavior + when(mockLexer.nextToken()).thenReturn(null); + when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>()); + when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree); + when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode); + when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST); + + // Use reflection to invoke the compileFile method + Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class); + compileFileMethod.setAccessible(true); + + compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath); + + // Verify each step + verify(mockLexer, times(1)).nextToken(); + verify(mockTokenStream, times(1)).getTokens(); + verify(mockParser, times(1)).program(); + verify(mockASTBuilder, times(1)).visit(mockParseTree); + verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode); + verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST); + } + + + + + + + @Test + public void testLexer() { + // Mock the behavior + when(mockLexer.nextToken()).thenReturn(null); + + // Test the lexer + SimpleJavaLexer lexer = new SimpleJavaLexer(mockInputCharStream); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + tokenStream.fill(); + + assertNotNull(tokenStream.getTokens()); + verify(mockLexer, atLeastOnce()).nextToken(); + } + + @Test + public void testParser() { + // Mock the behavior + when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree); + + // Test the parser + SimpleJavaParser parser = new SimpleJavaParser(mockTokenStream); + ParseTree parseTree = parser.program(); + + assertNotNull(parseTree); + verify(mockParser, times(1)).program(); + } + + @Test + public void testASTBuilder() { + // Mock the behavior + when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode); + + // Test the AST builder + ASTBuilder astBuilder = new ASTBuilder(); + ASTNode abstractSyntaxTree = astBuilder.visit(mockParseTree); + + assertNotNull(abstractSyntaxTree); + verify(mockASTBuilder, times(1)).visit(mockParseTree); + } + + @Test + public void testSemanticAnalyzer() { + // Mock the behavior + when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST); + + // Test the semantic analyzer + ASTNode typedAst = SemanticAnalyzer.generateTast(mockASTNode); + + assertNotNull(typedAst); + verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode); + } + + @Test + public void testByteCodeGenerator() { + // Test the bytecode generator + ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath); + byteCodeGenerator.visit((ProgramNode) mockTypedAST); + + verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST); + } +} + diff --git a/src/test/java/main/E2E_Reflections_Test.java b/src/test/java/main/E2E_Reflections_Test.java deleted file mode 100644 index 8b13819..0000000 --- a/src/test/java/main/E2E_Reflections_Test.java +++ /dev/null @@ -1,70 +0,0 @@ -package main; - -import ast.ASTNode; -import ast.ProgramNode; -import bytecode.ByteCodeGenerator; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.tree.ParseTree; -import parser.astBuilder.ASTBuilder; -import parser.generated.SimpleJavaLexer; -import parser.generated.SimpleJavaParser; -import semantic.SemanticAnalyzer; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import java.lang.reflect.Method; -import java.util.ArrayList; - -public class E2E_Reflections_Test { - - - private CharStream mockInputCharStream; - private String outputDirectoryPath; - - @BeforeEach - public void setUp() { - mockInputCharStream = mock(CharStream.class); - outputDirectoryPath = "path/to/output"; - } - - @Test - public void testCompileFile() throws Exception { - // Mock the dependencies - SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class); - CommonTokenStream mockTokenStream = mock(CommonTokenStream.class); - SimpleJavaParser mockParser = mock(SimpleJavaParser.class); - ParseTree mockParseTree = mock(ParseTree.class); - ASTBuilder mockASTBuilder = mock(ASTBuilder.class); - ASTNode mockASTNode = mock(ASTNode.class); - SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class); - ASTNode mockTypedAST = mock(ASTNode.class); - ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class); - - // Mock the behavior - when(mockLexer.nextToken()).thenReturn(null); - when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>()); - when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree); - when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode); - when(mockSemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST); - - // Use reflection to invoke the compileFile method - Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class); - compileFileMethod.setAccessible(true); - - compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath); - - // Verify each step - verify(mockLexer, times(1)).nextToken(); - verify(mockTokenStream, times(1)).getTokens(); - verify(mockParser, times(1)).program(); - verify(mockASTBuilder, times(1)).visit(mockParseTree); - verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode); - verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST); - } -} -