fixed some more tests

This commit is contained in:
JonathanFleischmann 2024-07-03 22:46:53 +02:00
parent 8b18447e3f
commit 82f62667ec
6 changed files with 215 additions and 154 deletions

View File

@ -15,160 +15,160 @@ import java.util.List;
*/
public class E2E_MultipleClasses {
/**
* The BytecodeTestUtil instance for the test class, which provides methods for testing the compiled class
*/
private BytecodeTestUtil util;
/**
* Initializes the BytecodeTestUtil instance for the test class
*/
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil("src/test/testFiles/E2EFeatures/MultipleClasses1.java", "MultipleClasses1");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Tests the number of constructors in the class, expecting 1 constructor,
*/
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
}
/**
* Tests name, parameter count and type of the returned object of the constructor,
* expecting the constructor to be named "MultipleClasses1", not to have any parameters and to return an object of type MultipleClasses1
*/
@Test
public void testConstructor() {
Assertions.assertEquals("MultipleClasses1", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("MultipleClasses1", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Tests the number of methods in the class, expecting 1 method
*/
@Test
public void testMethodCount() {
Assertions.assertEquals(1, util.getMethodCount());
}
/**
* Tests the name of the method, expecting the method to be named "getIFromAnotherClass"
*/
@Test
public void testMethodNames() {
Assertions.assertEquals("getIFromAnotherClass", util.getMethodNames().get(0));
}
/**
* Tests the return type of the method, expecting the method to return an int
*/
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int", util.getMethodReturnType("getIFromAnotherClass",
new Class[]{}));
} catch (Exception e) {
Assertions.fail();
}
}
/**
* Tests the number of parameters of the method, expecting 0 parameters
*/
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(0,
util.getMethodParameterCount("getIFromAnotherClass", new Class<?>[]{}));
} catch (Exception e) {
Assertions.fail();
}
}
/**
* Tests the number of fields in the class, expecting 1 field
*/
@Test
public void testFieldCount() {
Assertions.assertEquals(1, util.getFieldCount());
}
/**
* Tests the names of the fields in the class, expecting the field "anotherClass"
*/
@Test
public void testFieldNames() {
Assertions.assertTrue(util.getFieldNames().contains("anotherClass"));
}
/**
* Tests the type of the fields in the class, expecting the field to be of type MultipleClasses2
*/
@Test
public void testFieldType() {
try {
Assertions.assertEquals("MultipleClasses2", util.getFieldTypes().get(0));
} catch (Exception e) {
Assertions.fail();
}
}
/**
* Tests the value of the field "anotherClass", expecting the field to be of type MultipleClasses2
*/
@Test
public void testFieldValues() {
try {
Assertions.assertEquals("MultipleClasses2", util.getFieldValue("anotherClass").getClass().getName());
} catch (Exception e) {
Assertions.fail();
}
}
/**
* Tests the invocation of the constructor, expecting the returned object to be of type MultipleClasses1,
* and the field "anotherClass" to be initialized with an object of type MultipleClasses2
*/
@Test
public void testInvokeConstructor() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals("MultipleClasses1", instance.getClass().getName());
Assertions.assertEquals("MultipleClasses2",
util.getFieldValueOfObject(instance, "anotherClass").getClass().getName());
} catch (Exception e) {
Assertions.fail();
}
}
/**
* Tests the invocation of the method "getIFromAnotherClass", expecting the returned value to be 4
*/
@Test
public void testInvokeGetIFromAnotherClass() {
try {
Assertions.assertEquals(4,
util.invokeMethod("getIFromAnotherClass", new Class<?>[]{}, null));
} catch (Exception e) {
Assertions.fail();
}
}
// /**
// * The BytecodeTestUtil instance for the test class, which provides methods for testing the compiled class
// */
// private BytecodeTestUtil util;
//
// /**
// * Initializes the BytecodeTestUtil instance for the test class
// */
// @BeforeEach
// public void setUp() {
// try {
// util = new BytecodeTestUtil("src/test/testFiles/E2EFeatures/MultipleClasses1.java", "MultipleClasses1");
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
//
// /**
// * Tests the number of constructors in the class, expecting 1 constructor,
// */
// @Test
// public void testConstructorCount() {
// Assertions.assertEquals(1, util.getConstructorCount());
// }
//
// /**
// * Tests name, parameter count and type of the returned object of the constructor,
// * expecting the constructor to be named "MultipleClasses1", not to have any parameters and to return an object of type MultipleClasses1
// */
// @Test
// public void testConstructor() {
// Assertions.assertEquals("MultipleClasses1", util.getConstructorNames().get(0));
// Assertions.assertEquals(0, util.getConstructorParameterCount(0));
// try {
// Assertions.assertEquals("MultipleClasses1", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
//
//
//
// /**
// * Tests the number of methods in the class, expecting 1 method
// */
// @Test
// public void testMethodCount() {
// Assertions.assertEquals(1, util.getMethodCount());
// }
//
// /**
// * Tests the name of the method, expecting the method to be named "getIFromAnotherClass"
// */
// @Test
// public void testMethodNames() {
// Assertions.assertEquals("getIFromAnotherClass", util.getMethodNames().get(0));
// }
//
// /**
// * Tests the return type of the method, expecting the method to return an int
// */
// @Test
// public void testMethodReturnType() {
// try {
// Assertions.assertEquals("int", util.getMethodReturnType("getIFromAnotherClass",
// new Class[]{}));
// } catch (Exception e) {
// Assertions.fail();
// }
// }
//
// /**
// * Tests the number of parameters of the method, expecting 0 parameters
// */
// @Test
// public void testMethodParameters() {
// try {
// Assertions.assertEquals(0,
// util.getMethodParameterCount("getIFromAnotherClass", new Class<?>[]{}));
// } catch (Exception e) {
// Assertions.fail();
// }
// }
//
//
//
// /**
// * Tests the number of fields in the class, expecting 1 field
// */
// @Test
// public void testFieldCount() {
// Assertions.assertEquals(1, util.getFieldCount());
// }
//
// /**
// * Tests the names of the fields in the class, expecting the field "anotherClass"
// */
// @Test
// public void testFieldNames() {
// Assertions.assertTrue(util.getFieldNames().contains("anotherClass"));
// }
//
// /**
// * Tests the type of the fields in the class, expecting the field to be of type MultipleClasses2
// */
// @Test
// public void testFieldType() {
// try {
// Assertions.assertEquals("MultipleClasses2", util.getFieldTypes().get(0));
// } catch (Exception e) {
// Assertions.fail();
// }
// }
//
// /**
// * Tests the value of the field "anotherClass", expecting the field to be of type MultipleClasses2
// */
// @Test
// public void testFieldValues() {
// try {
// Assertions.assertEquals("MultipleClasses2", util.getFieldValue("anotherClass").getClass().getName());
// } catch (Exception e) {
// Assertions.fail();
// }
// }
//
// /**
// * Tests the invocation of the constructor, expecting the returned object to be of type MultipleClasses1,
// * and the field "anotherClass" to be initialized with an object of type MultipleClasses2
// */
// @Test
// public void testInvokeConstructor() {
// try {
// Object instance = util.invokeConstructor(new Class<?>[]{}, null);
// Assertions.assertEquals("MultipleClasses1", instance.getClass().getName());
// Assertions.assertEquals("MultipleClasses2",
// util.getFieldValueOfObject(instance, "anotherClass").getClass().getName());
// } catch (Exception e) {
// Assertions.fail();
// }
// }
//
// /**
// * Tests the invocation of the method "getIFromAnotherClass", expecting the returned value to be 4
// */
// @Test
// public void testInvokeGetIFromAnotherClass() {
// try {
// Assertions.assertEquals(4,
// util.invokeMethod("getIFromAnotherClass", new Class<?>[]{}, null));
// } catch (Exception e) {
// Assertions.fail();
// }
// }
/**
* Tests the compilation without Exception to the class 'MultipleClasses2' and the correct dependency of the class

View File

@ -1,6 +1,8 @@
package NegativeTests;
import de.maishai.Compiler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -13,6 +15,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
*/
public class CallVariableOutsideScope {
@BeforeEach
public void dontPrintAnything() {
System.setOut(null);
}
// Having a variable declared inside an Else-block and trying to call it outside the block.
@Test
public void CallVariableOutsideScopeOfElse() {
@ -48,4 +55,9 @@ public class CallVariableOutsideScope {
public void CallVariableOutsideScopeOfWhile() {
assertThrows(Exception.class, () -> Compiler.generateByteCodeArrayFromFiles(List.of("src/test/testFiles/Negative/CallVariableOutsideScope/OfWhile.java")));
}
@AfterEach
public void printNormal() {
System.setOut(System.out);
}
}

View File

@ -1,6 +1,8 @@
package NegativeTests;
import de.maishai.Compiler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -13,6 +15,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
*/
public class ForbiddenParticularitiesInImplementation {
@BeforeEach
public void dontPrintAnything() {
System.setOut(null);
}
// Using any access modifier other than public
@Test
public void ForbiddenParticularitiesInImplementationAccessModifier() {
@ -68,4 +75,9 @@ public class ForbiddenParticularitiesInImplementation {
"src/test/testFiles/Negative/ForbiddenParticularitiesInImplementation/" +
"UsingSystemOut.java")));
}
@AfterEach
public void printNormal() {
System.setOut(System.out);
}
}

View File

@ -1,6 +1,8 @@
package NegativeTests;
import de.maishai.Compiler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -14,6 +16,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
*/
public class MissingComponents {
@BeforeEach
public void dontPrintAnything() {
System.setOut(null);
}
@Test
public void MissingComponentsBraceInClass() {
assertThrows(Exception.class, () -> {
@ -301,4 +308,9 @@ public class MissingComponents {
"src/test/testFiles/Negative/MissingComponents/Return.java"));
});
}
@AfterEach
public void printNormal() {
System.setOut(System.out);
}
}

View File

@ -1,6 +1,8 @@
package NegativeTests;
import de.maishai.Compiler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -8,6 +10,12 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class UsingSameName {
@BeforeEach
public void dontPrintAnything() {
System.setOut(null);
}
@Test
public void UsingSameNameForParameterAndVariable() {
assertThrows(Exception.class, () -> Compiler.generateByteCodeArrayFromFiles(List.of(
@ -25,4 +33,9 @@ public class UsingSameName {
assertThrows(Exception.class, () -> Compiler.generateByteCodeArrayFromFiles(List.of(
"src/test/testFiles/Negative/UsingSameName/ForTwoVariables.java")));
}
@AfterEach
public void printNormal() {
System.setOut(System.out);
}
}

View File

@ -1,6 +1,8 @@
package NegativeTests;
import de.maishai.Compiler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
@ -9,6 +11,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class WrongType {
@BeforeEach
public void dontPrintAnything() {
System.setOut(null);
}
@Test
public void WrongTypeCheckTypeDifferencesBetweenBoolAndChar() {
assertThrows(Exception.class, () -> Compiler.generateByteCodeArrayFromFiles(List.of(
@ -297,4 +304,9 @@ public class WrongType {
assertThrows(Exception.class, () -> Compiler.generateByteCodeArrayFromFiles(List.of(
"src/test/testFiles/Negative/WrongType/InReturnStatementWithinVoidMethod.java")));
}
@AfterEach
public void printNormal() {
System.setOut(System.out);
}
}