added some e2e tests for CodeGenFiles with reflections

This commit is contained in:
JonathanFleischmann 2024-06-27 20:10:51 +02:00
parent 4be2af92f0
commit 71df6cc1d5
5 changed files with 436 additions and 396 deletions

View File

@ -49,7 +49,7 @@ public class ByteCode_Break {
@Test
public void testMethodNames() {
Assertions.assertEquals("breakMethod", util.getMethodNames().get(0));
Assertions.assertTrue(util.getMethodNames().contains("breakMethod"));
}
@Test
@ -64,8 +64,7 @@ public class ByteCode_Break {
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(1, util.getMethodParameterCount("breakMethod", new Class<?>[]{}));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class<?>[]{}).get(0));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class<?>[]{boolean.class}).get(0));
} catch (Exception e) {
Assertions.fail();
}
@ -78,8 +77,8 @@ public class ByteCode_Break {
@Test
public void testFieldNames() {
Assertions.assertEquals("whileRepetition", util.getFieldNames().get(0));
Assertions.assertEquals("forRepetition", util.getFieldNames().get(1));
Assertions.assertTrue(util.getFieldNames().contains("whileRepetition"));
Assertions.assertTrue(util.getFieldNames().contains("forRepetition"));
}
@Test
@ -115,12 +114,10 @@ public class ByteCode_Break {
@Test
public void testInvokeBreakMethodWithFalse() {
try {
int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition");
int fieldForRepetition = (int) util.getFieldValue("forRepetition");
Assertions.assertEquals(0, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(0, util.getFieldValue("forRepetition"));
util.invokeMethod("breakMethod", new Class<?>[]{boolean.class}, new Object[]{false});
Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(10, util.getFieldValue("whileRepetition"));
Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition"));
Assertions.assertEquals(10, util.getFieldValue("forRepetition"));
} catch (Exception e) {
throw new RuntimeException(e);
@ -130,12 +127,10 @@ public class ByteCode_Break {
@Test
public void testInvokeBreakMethodWithTrue() {
try {
int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition");
int fieldForRepetition = (int) util.getFieldValue("forRepetition");
Assertions.assertEquals(0, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(0, util.getFieldValue("forRepetition"));
util.invokeMethod("breakMethod", new Class<?>[]{boolean.class}, new Object[]{true});
Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(5, util.getFieldValue("whileRepetition"));
Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition"));
Assertions.assertEquals(5, util.getFieldValue("forRepetition"));
} catch (Exception e) {
throw new RuntimeException(e);

View File

@ -33,4 +33,81 @@ public class ByteCode_Constructor {
Assertions.assertEquals("Constructor", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
}
@Test
public void testConstructor2() {
Assertions.assertEquals("Constructor", util.getConstructorNames().get(1));
Assertions.assertEquals(1, util.getConstructorParameterCount(1));
Assertions.assertEquals("int", util.getConstructorParameterTypes(1).get(0));
}
@Test
public void testConstructor3() {
Assertions.assertEquals("Constructor", util.getConstructorNames().get(2));
Assertions.assertEquals(2, util.getConstructorParameterCount(2));
Assertions.assertEquals("int", util.getConstructorParameterTypes(2).get(0));
Assertions.assertEquals("int", util.getConstructorParameterTypes(2).get(1));
}
@Test
public void testMethodCount() {
Assertions.assertEquals(0, util.getMethodCount());
}
@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(1, util.getFieldValue("i"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeConstructor1() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals(instance.getClass().getName(), "Constructor");
Assertions.assertEquals(1, util.getFieldValueOfObject(instance, "i"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeConstructor2() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{int.class}, new Object[]{5});
Assertions.assertEquals(instance.getClass().getName(), "Constructor");
Assertions.assertEquals(5, util.getFieldValueOfObject(instance, "i"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeConstructor3() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{int.class, int.class}, new Object[]{5, 10});
Assertions.assertEquals(instance.getClass().getName(), "Constructor");
Assertions.assertEquals(15, util.getFieldValueOfObject(instance, "i"));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -3,143 +3,132 @@ package testResources.CodeGen.Features;
import de.maishai.ast.Operator;
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_Continue {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"Continue",
List.of(),
List.of(
new TypedMethod(
"continueLoop",
Type.VOID,
List.of(),
List.of(),
new TypedBlock(
List.of(
new TypedLocalVariable(
"i",
Type.INT
)),
List.of(
new TypedFor(
new TypedAssignment(
new TypedIntLiteral(
0,
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Type.INT
),
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Operator.LT,
new TypedIntLiteral(
5,
Type.INT
),
Type.BOOL
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Type.INT
),
new TypedBlock(
List.of(),
List.of(
new TypedIfElse(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Operator.EQ,
new TypedIntLiteral(
3,
Type.INT
),
Type.BOOL
),
new TypedBlock(
List.of(),
List.of(
new TypedContinue()
),
Type.VOID
),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID
)
),
Type.VOID
),
Type.INT
)
),
Type.VOID
)
)
),
List.of(
new TypedConstructor(
"Continue",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("Continue")
)
),
null
);
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Continue.java"), "Continue");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("Continue", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("Continue", 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("continueMethod", util.getMethodNames().get(0));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("void", util.getMethodReturnType("continueMethod", new Class<?>[]{boolean.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@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));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testFieldCount() {
Assertions.assertEquals(1, util.getFieldCount());
}
@Test
public void testFieldNames() {
Assertions.assertEquals("repetitions", util.getFieldNames().get(0));
}
@Test
public void testFieldTypes() {
try {
Assertions.assertEquals("int", util.getFieldTypes().get(0));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testFieldValues() {
try {
Assertions.assertEquals(0, util.getFieldValue("repetitions"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeDefaultConstructor() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals(instance.getClass().getName(), "Continue");
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitions"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodWithFalse() {
try {
int fieldRepetitions = (int) util.getFieldValue("repetitions");
Assertions.assertEquals(0, fieldRepetitions);
util.invokeMethod("continueMethod", new Class<?>[]{boolean.class}, new Object[]{false});
Assertions.assertEquals(5, util.getFieldValue("repetitions"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodWithTrue() {
try {
int fieldRepetitions = (int) util.getFieldValue("repetitions");
Assertions.assertEquals(0, fieldRepetitions);
util.invokeMethod("continueMethod", new Class<?>[]{boolean.class}, new Object[]{true});
Assertions.assertEquals(4, util.getFieldValue("repetitions"));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -2,183 +2,174 @@ 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_DataTypes {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"DataTypes",
List.of(
new TypedDeclaration(
"x",
Type.INT
),
new TypedDeclaration(
"y",
Type.BOOL
),
new TypedDeclaration(
"z",
Type.CHAR
)
),
List.of(
new TypedMethod(
"integer",
Type.INT,
List.of(
new TypedParameter(
"i",
Type.INT
)
),
List.of(),
new TypedBlock(
List.of(
new TypedLocalVariable(
"a",
Type.INT
)
),
List.of(
new TypedAssignment(
new TypedIntLiteral(
1,
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
),
new TypedReturn(
new TypedFieldVarAccess(
false,
null,
"a",
Type.INT
),
Type.INT
)
),
Type.INT
)
),
new TypedMethod(
"bool",
Type.BOOL,
List.of(
new TypedParameter(
"b",
Type.BOOL
)
),
List.of(),
new TypedBlock(
List.of(
new TypedLocalVariable(
"a",
Type.BOOL
)
),
List.of(
new TypedAssignment(
new TypedBoolLiteral(
true,
Type.BOOL
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.BOOL
),
Type.BOOL
),
new TypedReturn(
new TypedFieldVarAccess(
false,
null,
"a",
Type.BOOL
),
Type.BOOL
)
),
Type.BOOL
)
),
new TypedMethod(
"character",
Type.CHAR,
List.of(
new TypedParameter(
"c",
Type.CHAR
)
),
List.of(),
new TypedBlock(
List.of(
new TypedLocalVariable(
"a",
Type.CHAR
)
),
List.of(
new TypedAssignment(
new TypedCharLiteral(
'a',
Type.CHAR
),
new TypedFieldVarAccess(
false,
null,
"a",
Type.CHAR
),
Type.CHAR
),
new TypedReturn(
new TypedFieldVarAccess(
false,
null,
"a",
Type.CHAR
),
Type.CHAR
)
),
Type.CHAR
)
)
),
List.of(
new TypedConstructor(
"DataTypes",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("DataTypes")
)
),
null
);
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/DataTypes.java"), "DataTypes");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("DataTypes", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("DataTypes", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(3, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("integer"));
Assertions.assertTrue(util.getMethodNames().contains("bool"));
Assertions.assertTrue(util.getMethodNames().contains("character"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int", util.getMethodReturnType("integer", new Class<?>[]{int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("bool", new Class<?>[]{boolean.class}));
Assertions.assertEquals("char", util.getMethodReturnType("character", new Class<?>[]{char.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(1,
util.getMethodParameterCount("integer", new Class<?>[]{int.class}));
Assertions.assertEquals("int",
util.getMethodParameterTypes("integer", new Class<?>[]{int.class}).get(0));
Assertions.assertEquals(1,
util.getMethodParameterCount("bool", new Class<?>[]{boolean.class}));
Assertions.assertEquals("boolean",
util.getMethodParameterTypes("bool", new Class<?>[]{boolean.class}).get(0));
Assertions.assertEquals(1,
util.getMethodParameterCount("character", new Class<?>[]{char.class}));
Assertions.assertEquals("char",
util.getMethodParameterTypes("character", new Class<?>[]{char.class}).get(0));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testFieldCount() {
Assertions.assertEquals(3, util.getFieldCount());
}
@Test
public void testFieldNames() {
Assertions.assertTrue(util.getFieldNames().contains("x"));
Assertions.assertTrue(util.getFieldNames().contains("y"));
Assertions.assertTrue(util.getFieldNames().contains("z"));
}
@Test
public void testFieldTypes() {
Assertions.assertEquals("int", util.getFieldTypes().get(0));
Assertions.assertEquals("boolean", util.getFieldTypes().get(1));
Assertions.assertEquals("char", util.getFieldTypes().get(2));
}
@Test
public void testFieldValues() {
try {
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals(false, util.getFieldValue("y"));
Assertions.assertEquals('\u0000', util.getFieldValue("z"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeDefaultConstructor() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals(instance.getClass().getName(), "DataTypes");
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "x"));
Assertions.assertEquals(false, util.getFieldValueOfObject(instance, "y"));
Assertions.assertEquals('\u0000', util.getFieldValueOfObject(instance, "z"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodIntegerWith3() {
try {
Assertions.assertEquals(0, (int) util.getFieldValue("x"));
Assertions.assertFalse((boolean) util.getFieldValue("y"));
Assertions.assertEquals('\u0000', (char) util.getFieldValue("z"));
int returnValue = (int) util.invokeMethod("integer", new Class<?>[]{int.class}, new Object[]{3});
Assertions.assertEquals(1, returnValue);
Assertions.assertEquals(1, util.getFieldValue("x"));
Assertions.assertFalse((boolean) util.getFieldValue("y"));
Assertions.assertEquals('\u0000', util.getFieldValue("z"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodBoolWithTrue() {
try {
Assertions.assertEquals(0, (int) util.getFieldValue("x"));
Assertions.assertFalse((boolean) util.getFieldValue("y"));
Assertions.assertEquals('\u0000', (char) util.getFieldValue("z"));
boolean returnValue = (boolean) util.invokeMethod("bool", new Class<?>[]{boolean.class}, new Object[]{true});
Assertions.assertTrue(returnValue);
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertTrue((boolean) util.getFieldValue("y"));
Assertions.assertEquals('\u0000', util.getFieldValue("z"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethodCharacterWithB() {
try {
Assertions.assertEquals(0, (int) util.getFieldValue("x"));
Assertions.assertFalse((boolean) util.getFieldValue("y"));
Assertions.assertEquals('\u0000', (char) util.getFieldValue("z"));
char returnValue = (char) util.invokeMethod("character", new Class<?>[]{char.class}, new Object[]{'B'});
Assertions.assertEquals('a', returnValue);
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertFalse((boolean) util.getFieldValue("y"));
Assertions.assertEquals('a', util.getFieldValue("z"));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -2,85 +2,73 @@ 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_Field {
public static TypedProgram get() {
return new TypedProgram(
List.of(
new TypedClass(
"Field",
List.of(
new TypedDeclaration(
"x",
Type.INT
),
new TypedDeclaration(
"c",
Type.CHAR
)
),
List.of(
new TypedMethod(
"fieldAccess",
Type.VOID,
List.of(),
List.of(),
new TypedBlock(
List.of(),
List.of(
new TypedAssignment(
new TypedIntLiteral(
0,
Type.INT
),
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT
),
Type.INT
),
new TypedAssignment(
new TypedCharLiteral(
'a',
Type.CHAR
),
new TypedFieldVarAccess(
true,
null,
"c",
Type.CHAR
),
Type.CHAR
)
),
Type.VOID
)
)
),
List.of(
new TypedConstructor(
"Field",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("Field")
)
),
null
);
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Field.java"), "Field");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("Field", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("Field", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(3, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("fieldAccess"));
Assertions.assertTrue(util.getMethodNames().contains("setX"));
Assertions.assertTrue(util.getMethodNames().contains("setC"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("void", util.getMethodReturnType("fieldAccess", new Class<?>[]{}));
Assertions.assertEquals("void", util.getMethodReturnType("setX", new Class<?>[]{int.class}));
Assertions.assertEquals("void", util.getMethodReturnType("setC", new Class<?>[]{char.class}));
} catch (Exception e) {
Assertions.fail();
}
}
// @Test
// public void testFieldCount() {
// Assertions.assertEquals(2, util.getFieldCount());
// }
//
// @Test
// public void testFieldCount() {
// Assertions.assertEquals(2, util.getFieldCount());
// }
}