From 88061275b83489cfd3db6b088d62e44aabc4527f Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Fri, 28 Jun 2024 20:54:42 +0200 Subject: [PATCH] fixed and added some e2e tests --- .../CodeGen/Features/ByteCode_Continue.java | 12 +- .../CodeGen/Features/ByteCode_Main.java | 159 ++++++++++------- .../CodeGen/Features/ByteCode_Method.java | 166 +++++++++++++----- .../testFiles/CodeGenFeatures/Continue.java | 4 +- src/test/testFiles/CodeGenFeatures/Main.java | 4 - .../testFiles/CodeGenFeatures/Method.java | 4 + 6 files changed, 237 insertions(+), 112 deletions(-) diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java index 76f8ad9..802a466 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java @@ -47,13 +47,13 @@ public class ByteCode_Continue { @Test public void testMethodNames() { - Assertions.assertEquals("continueMethod", util.getMethodNames().get(0)); + Assertions.assertEquals("continueLoop", util.getMethodNames().get(0)); } @Test public void testMethodReturnType() { try { - Assertions.assertEquals("void", util.getMethodReturnType("continueMethod", new Class[]{boolean.class})); + Assertions.assertEquals("void", util.getMethodReturnType("continueLoop", new Class[]{boolean.class})); } catch (Exception e) { Assertions.fail(); } @@ -62,8 +62,8 @@ public class ByteCode_Continue { @Test public void testMethodParameters() { try { - Assertions.assertEquals(1, util.getMethodParameterCount("continueMethod", new Class[]{boolean.class})); - Assertions.assertEquals("boolean", util.getMethodParameterTypes("continueMethod", new Class[]{boolean.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("continueLoop", new Class[]{boolean.class})); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("continueLoop", new Class[]{boolean.class}).get(0)); } catch (Exception e) { Assertions.fail(); } @@ -113,7 +113,7 @@ public class ByteCode_Continue { try { int fieldRepetitions = (int) util.getFieldValue("repetitions"); Assertions.assertEquals(0, fieldRepetitions); - util.invokeMethod("continueMethod", new Class[]{boolean.class}, new Object[]{false}); + util.invokeMethod("continueLoop", new Class[]{boolean.class}, new Object[]{false}); Assertions.assertEquals(5, util.getFieldValue("repetitions")); } catch (Exception e) { Assertions.fail(); @@ -125,7 +125,7 @@ public class ByteCode_Continue { try { int fieldRepetitions = (int) util.getFieldValue("repetitions"); Assertions.assertEquals(0, fieldRepetitions); - util.invokeMethod("continueMethod", new Class[]{boolean.class}, new Object[]{true}); + util.invokeMethod("continueLoop", new Class[]{boolean.class}, new Object[]{true}); Assertions.assertEquals(4, util.getFieldValue("repetitions")); } catch (Exception e) { Assertions.fail(); diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java index d33d24f..0951d47 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java @@ -2,71 +2,110 @@ package testResources.CodeGen.Features; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_Main { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "Main", - List.of(), - List.of(), - List.of( - new TypedConstructor( - "Main", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - new TypedMain( - Type.VOID, - new TypedMethod( - "main", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 0, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ) - ), - Type.VOID - ) - ) + //TODO: fix this test - ), - null, - null, - Type.REFERENCE("Main") - ) - ), - null - ); + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Main.java"), "Main"); + } catch (Exception e) { + throw new RuntimeException(e); + } } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Main", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Main", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertEquals("main", util.getMethodNames().get(0)); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("main", new Class[]{String[].class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(1, + util.getMethodParameterCount("main", new Class[]{String[].class})); + Assertions.assertEquals("String[]", + util.getMethodParameterTypes("main", new Class[]{String[].class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(1, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("i", util.getFieldNames().get(0)); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("i")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Main"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "i")); + } catch (Exception e) { + Assertions.fail(); + } + } + + //TODO: Add test for invoking main method, how to? } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java index a18dd24..499ca68 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java @@ -2,49 +2,135 @@ package testResources.CodeGen.Features; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_Method { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "Method", - List.of(), - List.of( - new TypedMethod( - "method", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ) - ) - ), - List.of( - new TypedConstructor( - "Method", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("Method") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Method.java"), "Method"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testConstructor() { + Assertions.assertEquals("Method", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Method", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(2, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("method")); + Assertions.assertTrue(util.getMethodNames().contains("method2")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("method", new Class[]{})); + Assertions.assertEquals("void", util.getMethodReturnType("method2", new Class[]{int.class, int.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(0, util.getMethodParameterCount("method", new Class[]{})); + Assertions.assertEquals(2, util.getMethodParameterCount("method2", new Class[]{int.class, int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class[]{int.class, int.class}).get(0)); + Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class[]{int.class, int.class}).get(1)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(1, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("x", util.getFieldNames().get(0)); + } + + @Test + public void testFieldTypes() { + try { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testInvokeConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Method"); + Assertions.assertEquals(0, util.getFieldValue("x")); + } catch (Exception e) { + Assertions.fail(); + } + } + + + @Test + public void testInvokeMethod() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + util.invokeMethod("method", new Class[]{}, new Object[]{}); + Assertions.assertEquals(10, util.getFieldValue("x")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethod2() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + util.invokeMethod("method2", new Class[]{int.class, int.class}, new Object[]{5, 5}); + Assertions.assertEquals(5, util.getFieldValue("x")); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Continue.java b/src/test/testFiles/CodeGenFeatures/Continue.java index f979665..41b1d08 100644 --- a/src/test/testFiles/CodeGenFeatures/Continue.java +++ b/src/test/testFiles/CodeGenFeatures/Continue.java @@ -4,11 +4,11 @@ public class Continue { this.repetitions = 0; for (int i = 0; i < 5; i+=1) { // the second condition has to be in brackets - if (shouldContinue && i == 3) { + if (shouldContinue && (i == 3)) { continue; } // repetition is a field variable, it should be retrieved with this - repetitions += 1; + this.repetitions += 1; } } } \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Main.java b/src/test/testFiles/CodeGenFeatures/Main.java index 5fab2d3..e243729 100644 --- a/src/test/testFiles/CodeGenFeatures/Main.java +++ b/src/test/testFiles/CodeGenFeatures/Main.java @@ -1,10 +1,6 @@ public class Main { int i; - public Main() { - this.i = 0; - } - public static void main(String[] args) { this.i = 10; } diff --git a/src/test/testFiles/CodeGenFeatures/Method.java b/src/test/testFiles/CodeGenFeatures/Method.java index f19481b..97a88a3 100644 --- a/src/test/testFiles/CodeGenFeatures/Method.java +++ b/src/test/testFiles/CodeGenFeatures/Method.java @@ -8,4 +8,8 @@ public class Method { public void method() { this.x = 10; } + + public void method2(int a, int b) { + this.x = a; + } } \ No newline at end of file