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

View File

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

View File

@ -2,85 +2,73 @@ package testResources.CodeGen.Features;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*; 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; import java.util.List;
public class ByteCode_Field { public class ByteCode_Field {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Field", public void setUp() {
List.of( try {
new TypedDeclaration( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Field.java"), "Field");
"x", } catch (Exception e) {
Type.INT throw new RuntimeException(e);
), }
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
);
} }
@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());
// }
} }