added some e2e tests

This commit is contained in:
JonathanFleischmann 2024-06-28 22:57:43 +02:00
parent 1b87dfb78e
commit d11197173e
14 changed files with 750 additions and 705 deletions

View File

@ -24,3 +24,6 @@
- Zugriff auf Felder nur über `this`-Referenz möglich - Zugriff auf Felder nur über `this`-Referenz möglich
- `print()`statt `System.out.println()` - `print()`statt `System.out.println()`
- keine Accessmodifier/alles ist public - keine Accessmodifier/alles ist public
- logische Statements MÜSSEN geklammert werden, ansonsten wird ununterbrochen von links nach rechts berechnet
(so würde z.B. (true || false == false) false zurückgeben)
- i++ und i-- sind nicht erlaubt, stattdessen i = i + 1 und i = i - 1 bzw i += 1 und i -= 1

View File

@ -81,7 +81,12 @@ public class BytecodeTestUtil {
} }
public String getMethodReturnType(String methodName, Class<?>... params) throws Exception { public String getMethodReturnType(String methodName, Class<?>... params) throws Exception {
return clazz.getMethod(methodName, params).getReturnType().getSimpleName(); try {
return clazz.getMethod(methodName, params).getReturnType().getSimpleName();
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
} }
public int getMethodParameterCount(String methodName, Class<?>... params) throws Exception { public int getMethodParameterCount(String methodName, Class<?>... params) throws Exception {

View File

@ -60,21 +60,13 @@ public class ByteCode_LogicExpr {
@Test @Test
public void testMethodReturnType() { public void testMethodReturnType() {
try { try {
Assertions.assertEquals(1, util.getMethodParameterCount("testSmaller", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testSmaller", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testSmaller", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testLarger", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testLarger", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testLarger", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testEqual", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testNotEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testNotEqual", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testNotEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testSmallerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testSmallerEqual", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testSmallerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testLargerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testLargerEqual", new Class<?>[]{int.class, int.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testLargerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testAND", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testAND", new Class<?>[]{boolean.class, boolean.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testAND", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testOR", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testOR", new Class<?>[]{boolean.class, boolean.class})); Assertions.assertEquals("boolean", util.getMethodReturnType("testOR", new Class<?>[]{boolean.class, boolean.class}));
} catch (Exception e) { } catch (Exception e) {
Assertions.fail(); Assertions.fail();

View File

@ -1,7 +1,5 @@
package testResources.CodeGen.Features; 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.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -10,9 +8,6 @@ import testResources.CodeGen.BytecodeTestUtil;
import java.util.List; import java.util.List;
public class ByteCode_Main { public class ByteCode_Main {
//TODO: fix this test
private BytecodeTestUtil util; private BytecodeTestUtil util;
@BeforeEach @BeforeEach

View File

@ -1,125 +1,127 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.ast.Operator; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.Type; import org.junit.jupiter.api.BeforeEach;
import de.maishai.typedast.typedclass.*; import org.junit.jupiter.api.Test;
import testResources.CodeGen.BytecodeTestUtil;
import java.util.List; import java.util.List;
public class ByteCode_Overloaded { public class ByteCode_Overloaded {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Overloaded", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Overloaded.java"), "Overloaded");
new TypedMethod( } catch (Exception e) {
"overloadedMethod", throw new RuntimeException(e);
Type.INT, }
List.of(), }
List.of(),
new TypedBlock( @Test
List.of(), public void testConstructorCount() {
List.of( Assertions.assertEquals(1, util.getConstructorCount());
new TypedReturn( // default constructor
new TypedIntLiteral( }
0,
Type.INT @Test
), public void testDefaultConstructor() {
Type.INT Assertions.assertEquals("Overloaded", util.getConstructorNames().get(0));
) Assertions.assertEquals(0, util.getConstructorParameterCount(0));
), try {
Type.INT Assertions.assertEquals("Overloaded", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
) } catch (Exception e) {
), throw new RuntimeException(e);
new TypedMethod( }
"overloadedMethod", }
Type.INT,
List.of( @Test
new TypedParameter( public void testMethodCount() {
"x", Assertions.assertEquals(3, util.getMethodCount());
Type.INT }
)
), @Test
List.of(), public void testMethodNames() {
new TypedBlock( Assertions.assertTrue(util.getMethodNames().contains("overloadedMethod"));
List.of(), }
List.of(
new TypedReturn( @Test
new TypedFieldVarAccess( public void testMethodReturnType() {
false, try {
null, Assertions.assertEquals("int", util.getMethodReturnType("overloadedMethod", new Class<?>[]{}));
"x", Assertions.assertEquals("int", util.getMethodReturnType("overloadedMethod", new Class<?>[]{int.class}));
Type.INT Assertions.assertEquals("int", util.getMethodReturnType("overloadedMethod", new Class<?>[]{int.class, int.class}));
), } catch (Exception e) {
Type.INT Assertions.fail();
) }
), }
Type.INT
) @Test
), public void testMethodParameters() {
new TypedMethod( try {
"overloadedMethod", Assertions.assertEquals(0, util.getMethodParameterCount("overloadedMethod", new Class<?>[]{}));
Type.INT, Assertions.assertEquals(1, util.getMethodParameterCount("overloadedMethod", new Class<?>[]{int.class}));
List.of( Assertions.assertEquals(2, util.getMethodParameterCount("overloadedMethod", new Class<?>[]{int.class, int.class}));
new TypedParameter( } catch (Exception e) {
"x", Assertions.fail();
Type.INT }
), }
new TypedParameter(
"y", @Test
Type.INT public void testMethodParameterTypes() {
) try {
), Assertions.assertEquals("int", util.getMethodParameterTypes("overloadedMethod", new Class<?>[]{int.class}).get(0));
List.of(), Assertions.assertEquals("int", util.getMethodParameterTypes("overloadedMethod", new Class<?>[]{int.class, int.class}).get(0));
new TypedBlock( Assertions.assertEquals("int", util.getMethodParameterTypes("overloadedMethod", new Class<?>[]{int.class, int.class}).get(1));
List.of(), } catch (Exception e) {
List.of( Assertions.fail();
new TypedReturn( }
new TypedBinary( }
new TypedFieldVarAccess(
false, @Test
null, public void testFieldCount() {
"x", Assertions.assertEquals(0, util.getFieldCount());
Type.INT }
),
Operator.ADD, @Test
new TypedFieldVarAccess( public void testInvokeDefaultConstructor() {
false, try {
null, Object instance = util.invokeConstructor(new Class<?>[]{}, null);
"y", Assertions.assertEquals(instance.getClass().getName(), "Overloaded");
Type.INT } catch (Exception e) {
), Assertions.fail();
Type.INT }
), }
Type.INT
) @Test
), public void testInvokeOverloadedMethod1() {
Type.INT try {
) Object returnValue = util.invokeMethod("overloadedMethod", new Class<?>[]{}, null);
) Assertions.assertEquals(0, returnValue);
), } catch (Exception e) {
List.of( Assertions.fail();
new TypedConstructor( }
"Overloaded", }
List.of(),
new TypedBlock( @Test
List.of(), public void testInvokeOverloadedMethod2() {
List.of(), try {
Type.VOID Object returnValue = util.invokeMethod("overloadedMethod", new Class<?>[]{int.class}, new Object[]{1});
), Assertions.assertEquals(1, returnValue);
Type.VOID, } catch (Exception e) {
List.of() Assertions.fail();
) }
), }
null,
null, @Test
null, public void testInvokeOverloadedMethod3() {
Type.REFERENCE("Overloaded") try {
) Object returnValue = util.invokeMethod("overloadedMethod", new Class<?>[]{int.class, int.class}, new Object[]{1, 2});
), Assertions.assertEquals(3, returnValue);
null } catch (Exception e) {
); Assertions.fail();
}
} }
} }

View File

@ -1,106 +1,125 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.ast.Operator; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.Type; import org.junit.jupiter.api.BeforeEach;
import de.maishai.typedast.typedclass.*; import org.junit.jupiter.api.Test;
import testResources.CodeGen.BytecodeTestUtil;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List; import java.util.List;
public class ByteCode_Print { public class ByteCode_Print {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Print", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Print.java"), "Print");
new TypedMethod( } catch (Exception e) {
"printIt", throw new RuntimeException(e);
Type.VOID, }
List.of( }
new TypedParameter(
"c", @Test
Type.CHAR public void testConstructorCount() {
) Assertions.assertEquals(1, util.getConstructorCount());
), // default constructor
List.of(), }
new TypedBlock(
List.of(), @Test
List.of( public void testDefaultConstructor() {
new TypedPrint( Assertions.assertEquals("Print", util.getConstructorNames().get(0));
new TypedFieldVarAccess( Assertions.assertEquals(0, util.getConstructorParameterCount(0));
false, try {
null, Assertions.assertEquals("Print", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
"c", } catch (Exception e) {
Type.CHAR throw new RuntimeException(e);
), }
Type.VOID }
)
), @Test
Type.VOID public void testMethodCount() {
) Assertions.assertEquals(2, util.getMethodCount());
), }
new TypedMethod(
"printMoreComplex", @Test
Type.VOID, public void testMethodNames() {
List.of( Assertions.assertTrue(util.getMethodNames().contains("printIt"));
new TypedParameter( Assertions.assertTrue(util.getMethodNames().contains("printMoreComplex"));
"x", }
Type.INT
), @Test
new TypedParameter( public void testMethodReturnType() {
"y", try {
Type.INT Assertions.assertEquals("void", util.getMethodReturnType("printIt", new Class<?>[]{char.class}));
) Assertions.assertEquals("void", util.getMethodReturnType("printMoreComplex", new Class<?>[]{int.class, int.class}));
), } catch (Exception e) {
List.of(), Assertions.fail();
new TypedBlock( }
List.of(), }
List.of(
new TypedPrint( @Test
new TypedBinary( public void testMethodParameters() {
new TypedFieldVarAccess( try {
false, Assertions.assertEquals(1, util.getMethodParameterCount("printIt", new Class<?>[]{char.class}));
null, Assertions.assertEquals(2, util.getMethodParameterCount("printMoreComplex", new Class<?>[]{int.class, int.class}));
"x", Assertions.assertEquals("int", util.getMethodParameterTypes("printMoreComplex", new Class<?>[]{int.class, int.class}).get(0));
Type.INT Assertions.assertEquals("int", util.getMethodParameterTypes("printMoreComplex", new Class<?>[]{int.class, int.class}).get(1));
), } catch (Exception e) {
Operator.LT, Assertions.fail();
new TypedFieldVarAccess( }
false, }
null,
"y", @Test
Type.INT public void testFieldCount() {
), Assertions.assertEquals(0, util.getFieldCount());
Type.BOOL }
),
Type.VOID @Test
) public void testInvokeDefaultConstructor() {
), try {
Type.VOID Object instance = util.invokeConstructor(new Class<?>[]{}, null);
) Assertions.assertEquals(instance.getClass().getName(), "Print");
) } catch (Exception e) {
), Assertions.fail();
List.of( }
new TypedConstructor( }
"Print",
List.of(), @Test
new TypedBlock( public void testInvokePrintIt() {
List.of(), try {
List.of(), PrintStream originalOut = System.out;
Type.VOID ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
), PrintStream newOut = new PrintStream(byteArrayOutputStream);
Type.VOID, System.setOut(newOut);
List.of() Object returnValue = util.invokeMethod("printIt", new Class<?>[]{char.class}, new Object[]{'a'});
) Assertions.assertNull(returnValue);
), Assertions.assertEquals(3, byteArrayOutputStream.toString().length());
null, // durch die newline wird die Ausgabe um 2 Zeichen länger
null, Assertions.assertEquals("a", byteArrayOutputStream.toString().substring(0, byteArrayOutputStream.toString().length() - 2));
null, System.setOut(originalOut);
Type.REFERENCE("Print") } catch (Exception e) {
) Assertions.fail();
), }
null }
);
@Test
public void testInvokePrintMoreComplex() {
try {
PrintStream originalOut = System.out;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream newOut = new PrintStream(byteArrayOutputStream);
System.setOut(newOut);
Object returnValue = util.invokeMethod("printMoreComplex", new Class<?>[]{int.class, int.class}, new Object[]{5, 10});
Assertions.assertNull(returnValue);
Assertions.assertEquals(6, byteArrayOutputStream.toString().length());
// durch die newline wird die Ausgabe um 2 Zeichen länger
Assertions.assertEquals("true", byteArrayOutputStream.toString().substring(0, byteArrayOutputStream.toString().length() - 2));
System.setOut(originalOut);
} catch (Exception e) {
Assertions.fail();
}
} }
} }

View File

@ -1,131 +1,156 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.typedast.Type; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.typedclass.*; 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_Return { public class ByteCode_Return {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Return", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Return.java"), "Return");
new TypedMethod( } catch (Exception e) {
"returnInt", throw new RuntimeException(e);
Type.INT, }
List.of(), }
List.of(),
new TypedBlock( @Test
List.of(), public void testConstructorCount() {
List.of( Assertions.assertEquals(1, util.getConstructorCount());
new TypedReturn( // default constructor
new TypedIntLiteral( }
10,
Type.INT @Test
), public void testDefaultConstructor() {
Type.INT Assertions.assertEquals("Return", util.getConstructorNames().get(0));
) Assertions.assertEquals(0, util.getConstructorParameterCount(0));
), try {
Type.INT Assertions.assertEquals("Return", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
) } catch (Exception e) {
), Assertions.fail();
new TypedMethod( }
"returnVoid", }
Type.VOID,
List.of(), @Test
List.of(), public void testMethodCount() {
new TypedBlock( Assertions.assertEquals(6, util.getMethodCount());
List.of(), }
List.of(
new TypedReturn( @Test
null, public void testMethodNames() {
Type.VOID Assertions.assertTrue(util.getMethodNames().contains("returnInt"));
) Assertions.assertTrue(util.getMethodNames().contains("returnVoid"));
), Assertions.assertTrue(util.getMethodNames().contains("returnBoolean"));
Type.VOID Assertions.assertTrue(util.getMethodNames().contains("returnChar"));
) Assertions.assertTrue(util.getMethodNames().contains("returnObject"));
), Assertions.assertTrue(util.getMethodNames().contains("returnAndInterruptFor"));
new TypedMethod( }
"returnBoolean",
Type.BOOL, @Test
List.of(), public void testMethodReturnType() {
List.of(), try {
new TypedBlock( Assertions.assertEquals("int", util.getMethodReturnType("returnInt", new Class<?>[]{}));
List.of(), Assertions.assertEquals("void", util.getMethodReturnType("returnVoid", new Class<?>[]{}));
List.of( Assertions.assertEquals("boolean", util.getMethodReturnType("returnBoolean", new Class<?>[]{}));
new TypedReturn( Assertions.assertEquals("char", util.getMethodReturnType("returnChar", new Class<?>[]{}));
new TypedBoolLiteral( Assertions.assertEquals("Return", util.getMethodReturnType("returnObject", new Class<?>[]{}));
true, Assertions.assertEquals("int", util.getMethodReturnType("returnAndInterruptFor", new Class<?>[]{}));
Type.BOOL } catch (Exception e) {
), Assertions.fail();
Type.BOOL }
) }
),
Type.BOOL @Test
) public void testMethodParameters() {
), try {
new TypedMethod( Assertions.assertEquals(0, util.getMethodParameterCount("returnInt", new Class<?>[]{}));
"returnChar", Assertions.assertEquals(0, util.getMethodParameterCount("returnVoid", new Class<?>[]{}));
Type.CHAR, Assertions.assertEquals(0, util.getMethodParameterCount("returnBoolean", new Class<?>[]{}));
List.of(), Assertions.assertEquals(0, util.getMethodParameterCount("returnChar", new Class<?>[]{}));
List.of(), Assertions.assertEquals(0, util.getMethodParameterCount("returnObject", new Class<?>[]{}));
new TypedBlock( Assertions.assertEquals(0, util.getMethodParameterCount("returnAndInterruptFor", new Class<?>[]{}));
List.of(), } catch (Exception e) {
List.of( Assertions.fail();
new TypedReturn( }
new TypedCharLiteral( }
'a',
Type.CHAR @Test
), public void testFieldCount() {
Type.CHAR Assertions.assertEquals(0, util.getFieldCount());
) }
),
Type.CHAR @Test
) public void testInvokeDefaultConstructor() {
), try {
new TypedMethod( Assertions.assertEquals("Return",
"returnClass", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
Type.REFERENCE("Return"), } catch (Exception e) {
List.of(), Assertions.fail();
List.of(), }
new TypedBlock( }
List.of(),
List.of( @Test
new TypedReturn( public void testReturnInt() {
new TypedNew( try {
Type.REFERENCE("Return"), Assertions.assertEquals(10,
List.of() util.invokeMethod("returnInt", new Class<?>[]{}, null));
), } catch (Exception e) {
Type.REFERENCE("Return") Assertions.fail();
) }
), }
Type.REFERENCE("Return")
) @Test
) public void testReturnVoid() {
), try {
List.of( Assertions.assertNull(util.invokeMethod("returnVoid", new Class<?>[]{}, null));
new TypedConstructor( } catch (Exception e) {
"Return", Assertions.fail();
List.of(), }
new TypedBlock( }
List.of(),
List.of(), @Test
Type.VOID public void testReturnBoolean() {
), try {
Type.VOID, Assertions.assertTrue((boolean)
List.of() util.invokeMethod("returnBoolean", new Class<?>[]{}, null));
) } catch (Exception e) {
), Assertions.fail();
null, }
null, }
null,
Type.REFERENCE("Return") @Test
) public void testReturnChar() {
), try {
null Assertions.assertEquals('a',
); util.invokeMethod("returnChar", new Class<?>[]{}, null));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testReturnObject() {
try {
Assertions.assertEquals("Return",
util.invokeMethod("returnObject", new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testReturnAndInterruptFor() {
try {
Assertions.assertEquals(5,
util.invokeMethod("returnAndInterruptFor", new Class<?>[]{}, new Object[]{}));
} catch (Exception e) {
Assertions.fail();
}
} }
} }

View File

@ -3,100 +3,113 @@ package testResources.CodeGen.Features;
import de.maishai.ast.UnaryOperator; import de.maishai.ast.UnaryOperator;
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_Unary { public class ByteCode_Unary {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Unary", public void setUp() {
List.of(), try {
List.of(), util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Unary.java"), "Unary");
List.of( } catch (Exception e) {
new TypedConstructor( throw new RuntimeException(e);
"Unary", }
List.of(), }
new TypedBlock(
List.of( @Test
new TypedLocalVariable( public void testConstructorCount() {
"a", Assertions.assertEquals(1, util.getConstructorCount());
Type.INT // default constructor
), }
new TypedLocalVariable(
"b", @Test
Type.INT public void testDefaultConstructor() {
), Assertions.assertEquals("Unary", util.getConstructorNames().get(0));
new TypedLocalVariable( Assertions.assertEquals(0, util.getConstructorParameterCount(0));
"c", try {
Type.BOOL Assertions.assertEquals("Unary", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
) } catch (Exception e) {
), Assertions.fail();
List.of( }
new TypedAssignment( }
new TypedIntLiteral(
5, @Test
Type.INT public void testMethodCount() {
), Assertions.assertEquals(2, util.getMethodCount());
new TypedFieldVarAccess( }
false,
null, @Test
"a", public void testMethodNames() {
Type.INT Assertions.assertTrue(util.getMethodNames().contains("testMinus"));
), Assertions.assertTrue(util.getMethodNames().contains("testNot"));
Type.INT }
),
new TypedAssignment( @Test
new TypedUnary( public void testMethodReturnType() {
UnaryOperator.SUB, try {
new TypedFieldVarAccess( Assertions.assertEquals("int",
false, util.getMethodReturnType("testMinus", new Class<?>[]{int.class}));
null, Assertions.assertEquals("boolean",
"a", util.getMethodReturnType("testNot", new Class<?>[]{boolean.class}));
Type.INT } catch (Exception e) {
), Assertions.fail();
Type.INT }
), }
new TypedFieldVarAccess(
false, @Test
null, public void testMethodParameters() {
"b", try {
Type.INT Assertions.assertEquals(1,
), util.getMethodParameterCount("testMinus", new Class<?>[]{int.class}));
Type.INT Assertions.assertEquals("int",
), util.getMethodParameterTypes("testMinus", new Class<?>[]{int.class}).get(0));
new TypedAssignment( Assertions.assertEquals(1,
new TypedUnary( util.getMethodParameterCount("testNot", new Class<?>[]{boolean.class}));
UnaryOperator.NOT, Assertions.assertEquals("boolean",
new TypedBoolLiteral( util.getMethodParameterTypes("testNot", new Class<?>[]{boolean.class}).get(0));
true, } catch (Exception e) {
Type.BOOL Assertions.fail();
), }
Type.BOOL }
),
new TypedFieldVarAccess( @Test
false, public void testFieldCount() {
null, Assertions.assertEquals(0, util.getFieldCount());
"c", }
Type.BOOL
), @Test
Type.BOOL public void testInvokeDefaultConstructor() {
) try {
), Assertions.assertEquals("Unary", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
Type.VOID } catch (Exception e) {
), Assertions.fail();
Type.VOID, }
List.of() }
)
), @Test
null, public void testInvokeTestMinus() {
null, try {
null, Assertions.assertEquals(-5,
Type.REFERENCE("Unary") util.invokeMethod("testMinus", new Class<?>[]{int.class}, 5));
) } catch (Exception e) {
), Assertions.fail();
null }
); }
@Test
public void testInvokeTestNot() {
try {
Assertions.assertEquals(true,
util.invokeMethod("testNot", new Class<?>[]{boolean.class}, false));
} catch (Exception e) {
Assertions.fail();
}
} }
} }

View File

@ -1,74 +1,108 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.typedast.Type; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.typedclass.*; 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_VariableDefWithDecl { public class ByteCode_VariableDefWithDecl {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"VariableDefWithDecl", public void setUp() {
List.of(), try {
List.of(), util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/VariableDefWithDecl.java"),
List.of( "VariableDefWithDecl");
new TypedConstructor( } catch (Exception e) {
"VariableDefWithDecl", throw new RuntimeException(e);
List.of(), }
new TypedBlock( }
List.of(
new TypedLocalVariable( @Test
"a", public void testConstructorCount() {
Type.INT Assertions.assertEquals(1, util.getConstructorCount());
), // default constructor
new TypedLocalVariable( }
"b",
Type.INT @Test
) public void testDefaultConstructor() {
), Assertions.assertEquals("VariableDefWithDecl", util.getConstructorNames().get(0));
List.of( Assertions.assertEquals(0, util.getConstructorParameterCount(0));
new TypedAssignment( try {
new TypedIntLiteral( Assertions.assertEquals("VariableDefWithDecl", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
10, } catch (Exception e) {
Type.INT throw new RuntimeException(e);
), }
new TypedFieldVarAccess( }
false,
null, @Test
"a", public void testMethodCount() {
Type.INT Assertions.assertEquals(2, util.getMethodCount());
), }
Type.INT
), @Test
new TypedAssignment( public void testMethodNames() {
new TypedIntLiteral( Assertions.assertTrue(util.getMethodNames().contains("testDefWithDeclInOne"));
20, Assertions.assertTrue(util.getMethodNames().contains("testDefWithDeclSeparate"));
Type.INT }
),
new TypedFieldVarAccess( @Test
false, public void testMethodReturnType() {
null, try {
"b", Assertions.assertEquals("int", util.getMethodReturnType("testDefWithDeclInOne", new Class<?>[]{}));
Type.INT Assertions.assertEquals("int", util.getMethodReturnType("testDefWithDeclSeparate", new Class<?>[]{}));
), } catch (Exception e) {
Type.INT Assertions.fail();
) }
), }
Type.VOID
), @Test
Type.VOID, public void testMethodParameters() {
List.of() try {
) Assertions.assertEquals(0,
), util.getMethodParameterCount("testDefWithDeclInOne", new Class<?>[]{}));
null, Assertions.assertEquals(0,
null, util.getMethodParameterCount("testDefWithDeclSeparate", new Class<?>[]{}));
null, } catch (Exception e) {
Type.REFERENCE("VariableDefWithDecl") Assertions.fail();
) }
), }
null
); @Test
public void testFieldCount() {
Assertions.assertEquals(0, util.getFieldCount());
}
@Test
public void testInvokeDefaultConstructor() {
try {
Assertions.assertEquals("VariableDefWithDecl",
util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testDefWithDeclInOne() {
try {
Assertions.assertEquals(10,
util.invokeMethod("testDefWithDeclInOne", new Class<?>[]{}, null));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testDefWithDeclSeparate() {
try {
Assertions.assertEquals(10,
util.invokeMethod("testDefWithDeclSeparate", new Class<?>[]{}, null));
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -1,190 +1,135 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.ast.Operator; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.Type; import org.junit.jupiter.api.BeforeEach;
import de.maishai.typedast.typedclass.*; import org.junit.jupiter.api.Test;
import testResources.CodeGen.BytecodeTestUtil;
import java.util.List; import java.util.List;
public class ByteCode_While { public class ByteCode_While {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"While", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/While.java"), "While");
new TypedMethod( } catch (Exception e) {
"whileLoop", throw new RuntimeException(e);
Type.VOID, }
List.of(), }
List.of(),
new TypedBlock( @Test
List.of( public void testConstructorCount() {
new TypedLocalVariable( Assertions.assertEquals(1, util.getConstructorCount());
"i", }
Type.INT
) @Test
), public void testConstructor() {
List.of( Assertions.assertEquals("While", util.getConstructorNames().get(0));
new TypedAssignment( Assertions.assertEquals(0, util.getConstructorParameterCount(0));
new TypedIntLiteral( try {
0, Assertions.assertEquals("While", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
Type.INT } catch (Exception e) {
), throw new RuntimeException(e);
new TypedFieldVarAccess( }
false, }
null,
"i", @Test
Type.INT public void testMethodCount() {
), Assertions.assertEquals(2, util.getMethodCount());
Type.INT }
),
new TypedWhile( @Test
new TypedBinary( public void testMethodNames() {
new TypedFieldVarAccess( Assertions.assertTrue(util.getMethodNames().contains("whileLoop"));
false, Assertions.assertTrue(util.getMethodNames().contains("doWhileLoop"));
null, }
"i",
Type.INT @Test
), public void testMethodReturnType() {
Operator.LT, try {
new TypedIntLiteral( Assertions.assertEquals("void", util.getMethodReturnType("whileLoop", new Class<?>[]{}));
5, Assertions.assertEquals("void", util.getMethodReturnType("doWhileLoop", new Class<?>[]{}));
Type.INT } catch (Exception e) {
), Assertions.fail();
Type.BOOL }
), }
new TypedBlock(
List.of(),
List.of( @Test
new TypedAssignment( public void testMethodParameters() {
new TypedBinary( try {
new TypedFieldVarAccess( Assertions.assertEquals(0, util.getMethodParameterCount("whileLoop", new Class<?>[]{}));
false, Assertions.assertEquals(0, util.getMethodParameterCount("doWhileLoop", new Class<?>[]{}));
null, } catch (Exception e) {
"i", Assertions.fail();
Type.INT }
), }
Operator.ADD,
new TypedIntLiteral( @Test
1, public void testFieldCount() {
Type.INT Assertions.assertEquals(2, util.getFieldCount());
), }
Type.INT
), @Test
new TypedFieldVarAccess( public void testFieldNames() {
false, Assertions.assertTrue(util.getFieldNames().contains("whileRepetitions"));
null, Assertions.assertTrue(util.getFieldNames().contains("doWhileRepetitions"));
"i", }
Type.INT
), @Test
Type.INT public void testFieldTypes() {
) Assertions.assertEquals("int", util.getFieldTypes().get(0));
), Assertions.assertEquals("int", util.getFieldTypes().get(1));
Type.VOID }
),
Type.VOID @Test
) public void testFieldValues() {
) try {
) Assertions.assertEquals(1, util.getFieldValue("whileRepetitions"));
), Assertions.assertEquals(1, util.getFieldValue("doWhileRepetitions"));
new TypedMethod( } catch (Exception e) {
"doWhileLoop", Assertions.fail();
Type.VOID, }
List.of(), }
List.of(),
new TypedBlock( @Test
List.of( public void testInvokeConstructor() {
new TypedLocalVariable( try {
"i", Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Type.INT Assertions.assertEquals("While", instance.getClass().getName());
) Assertions.assertEquals(1, util.getFieldValueOfObject(instance, "whileRepetitions"));
), Assertions.assertEquals(1, util.getFieldValueOfObject(instance, "doWhileRepetitions"));
List.of( } catch (Exception e) {
new TypedAssignment( Assertions.fail();
new TypedIntLiteral( }
0, }
Type.INT
), @Test
new TypedFieldVarAccess( public void testInvokeWhileLoop() {
false, try {
null, Assertions.assertEquals(1, util.getFieldValue("whileRepetitions"));
"i", Assertions.assertEquals(1, util.getFieldValue("doWhileRepetitions"));
Type.INT util.invokeMethod("whileLoop", new Class<?>[]{});
), Assertions.assertEquals(5, util.getFieldValue("whileRepetitions"));
Type.INT Assertions.assertEquals(1, util.getFieldValue("doWhileRepetitions"));
), } catch (Exception e) {
new TypedDoWhile( Assertions.fail();
new TypedBlock( }
List.of(), }
List.of(
new TypedAssignment( @Test
new TypedBinary( public void testInvokeDoWhileLoop() {
new TypedFieldVarAccess( try {
false, Assertions.assertEquals(1, util.getFieldValue("whileRepetitions"));
null, Assertions.assertEquals(1, util.getFieldValue("doWhileRepetitions"));
"i", util.invokeMethod("doWhileLoop", new Class<?>[]{});
Type.INT Assertions.assertEquals(1, util.getFieldValue("whileRepetitions"));
), Assertions.assertEquals(6, util.getFieldValue("doWhileRepetitions"));
Operator.ADD, } catch (Exception e) {
new TypedIntLiteral( Assertions.fail();
1, }
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Type.INT
)
),
Type.VOID
),
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Operator.LT,
new TypedIntLiteral(
5,
Type.INT
),
Type.BOOL
),
Type.VOID
)
)
)
)
),
List.of(
new TypedConstructor(
"While",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
null,
Type.REFERENCE("While")
)
),
null
);
} }
} }

View File

@ -2,6 +2,9 @@ public class Main {
int i; int i;
public static void main(String[] args) { public static void main(String[] args) {
this.i = 10; Main mainObject;
mainObject = new Main();
mainObject.i = 3;
print(mainObject.i);
} }
} }

View File

@ -15,7 +15,16 @@ public class Return {
return 'a'; return 'a';
} }
public Return returnClass() { public Return returnObject() {
return new Return(); return new Return();
} }
public int returnAndInterruptFor() {
for (int i = 10; i > 0; i -= 1) {
if (i == 5) {
return i;
}
}
return 0;
}
} }

View File

@ -4,7 +4,7 @@ public class Unary {
return -a; return -a;
} }
public boolean testNot(boolean a) { public boolean testNot(boolean b) {
return !a; return !b;
} }
} }

View File

@ -1,24 +1,24 @@
public class While { public class While {
int whileRepititions; int whileRepetitions;
int doWhileRepititions; int doWhileRepetitions;
public While() { public While() {
this.whileRepititions = 0; this.whileRepetitions = 1;
this.doWhileRepititions = 0; this.doWhileRepetitions = 1;
} }
public void whileLoop() { public void whileLoop() {
this.whileRepititions = 0; this.whileRepetitions = 0;
while (i < 5) { while (this.whileRepetitions < 5) {
this.whileRepititions = this.whileRepititions + 1; this.whileRepetitions = this.whileRepetitions + 1;
} }
} }
public void doWhileLoop() { public void doWhileLoop() {
this.doWhileRepititions = 0; this.doWhileRepetitions = 5;
do { do {
this.doWhileRepititions = this.doWhileRepititions + 1; this.doWhileRepetitions = this.doWhileRepetitions + 1;
} while (i < 5); } while (this.doWhileRepetitions < 5);
} }
} }