fixed and added some e2e tests

This commit is contained in:
JonathanFleischmann 2024-06-28 20:54:42 +02:00
parent 576fceb89b
commit 88061275b8
6 changed files with 237 additions and 112 deletions

View File

@ -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();

View File

@ -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?
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}
}

View File

@ -1,10 +1,6 @@
public class Main {
int i;
public Main() {
this.i = 0;
}
public static void main(String[] args) {
this.i = 10;
}

View File

@ -8,4 +8,8 @@ public class Method {
public void method() {
this.x = 10;
}
public void method2(int a, int b) {
this.x = a;
}
}