New tests and corrected tests in both classes

main
CedricNew 2023-01-10 00:28:22 +01:00
parent b5475b051c
commit f5f33c7a86
2 changed files with 159 additions and 54 deletions

View File

@ -1,57 +1,50 @@
package tests; package tests;
import src.domain.C3PO; import domain.C3PO;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import src.utility.robot_exceptions.RobotException; import utility.robot_exceptions.RobotException;
import src.utility.robot_exceptions.RobotMagicValueException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class C3POTest { class C3POTest {
C3PO herbert; C3PO c3po;
int id = 0; int id = 0;
String name = "Herbert"; String name = "Herbert";
@BeforeEach @BeforeEach
void setup(){ void setup(){
herbert = new C3PO(id, name); c3po = new C3PO(id, name);
c3po.triggerPowerSwitch();
} }
//Tests for basic functions //Tests for basic functions
@Test @Test
void getId() { void getId() {
assertEquals(id, herbert.getId()); assertEquals(id, c3po.getId());
} }
@Test @Test
void getName() { void getName() {
assertEquals(name, herbert.getName()); assertEquals(name, c3po.getName());
assertEquals(name, assertEquals(name,
herbert.getName()); c3po.getName());
} }
@Test @Test
void triggerPowerSwitch() { void triggerPowerSwitch() {
herbert.triggerPowerSwitch(); assertTrue(c3po.isPowerOn());
assertTrue(herbert.isPowerOn()); c3po.triggerPowerSwitch();
} assertFalse(c3po.isPowerOn());
@Test
void isPowerOn() {
assertFalse(herbert.isPowerOn());
herbert.triggerPowerSwitch();
assertTrue(herbert.isPowerOn());
} }
@Test @Test
void speak(){ void speak(){
herbert.triggerPowerSwitch();
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23"; String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23";
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23}; int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
String array = ""; String array = "";
try{ try{
array = herbert.speak(input); array = c3po.speak(input);
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re); System.out.println(re);
} }
@ -59,19 +52,57 @@ class C3POTest {
} }
@Test @Test
void think(){ void C3POSpeakTestStandard() throws RobotException {
assertEquals("1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12", c3po.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
}
@Test
void C3POSpeakTestEmpty() {
try{
String solution = c3po.speak(new int[]{});
}catch(RobotException re){
assertEquals(0, "r2d2 leer".compareTo(re.getMessage()));
}
}
@Test
void C3POSpeakTestZeros() throws RobotException {
assertEquals("0; 0; 0; 0", c3po.speak(new int[4]));
}
@Test
void C3POSpeakTestOneElement() throws RobotException {
assertEquals("1", c3po.speak(new int[]{1}));
}
@Test
void C3POSpeakTestUnitElements() throws RobotException {
assertEquals("-1; 0; 1", c3po.speak(new int[]{-1, 0, 1}));
}
@Test
void C3POSpeakTestMagicNumberException() {
try{
String solution = c3po.speak(new int[]{42});
}catch(RobotException re){
assertEquals(0, "r2d2 Magic value".compareTo(re.getMessage()));
}
}
@Test
void think(){
int[] solution = { 2, 4, 4, 5, 7, 12, 23, 56, 433}; int[] solution = { 2, 4, 4, 5, 7, 12, 23, 56, 433};
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23}; int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
int[] value = new int[9]; int[] value = new int[9];
try{ try{
value = herbert.think(input); value = c3po.think(input);
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re); System.out.println(re);
} }
herbert.triggerPowerSwitch(); c3po.triggerPowerSwitch();
try{ try{
value = herbert.think(input); value = c3po.think(input);
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re); System.out.println(re);
} }
@ -83,20 +114,19 @@ class C3POTest {
} }
@Test @Test
void thinkTestMagicNumberException() { void thinkTestMagicNumberException() {
assertThrows(RobotMagicValueException.class, () -> herbert.think(new int[]{42})); //assertEquals(new RobotException(robotExceptions.MAGICVALUE), () -> herbert.think(new int[]{42}));
} }
@Test @Test
void magicValueException(){ void magicValueException(){
int[] input = {3,2,42}; int[] input = {3,2,42};
herbert.triggerPowerSwitch(); String expectedMessage = "Herbert Magic value";
String expectedMessage = "Herbert has an unknown error. Code 42.";
try{ try{
int[] solution = herbert.think(input); int[] solution = c3po.think(input);
}catch(RobotException re){ }catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
try{ try{
String test = herbert.speak(input); String test = c3po.speak(input);
}catch(RobotException re){ }catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
@ -105,40 +135,44 @@ class C3POTest {
@Test @Test
void illegalStateException(){ void illegalStateException(){
int[] input = {3,2,42}; int[] input = {3,2,42};
String expectedMessage = "Herbert is turned off."; c3po.triggerPowerSwitch();
String expectedMessage = "Herbert ist in einem illegalen Zustand";
try{ try{
int[] solution = herbert.think(input); int[] solution = c3po.think(input);
}catch(RobotException re){ }catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
try{ try{
String test = herbert.speak(input); String test = c3po.speak(input);
}catch(RobotException re){ }catch(RobotException re){
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
} }
@Test @Test
void arrayEmptyException(){ void arrayEmptyException(){
String expectedMessage = "Herbert got an empty array."; String expectedMessage = "Herbert leer";
herbert.triggerPowerSwitch();
try{ try{
int[] solution = herbert.think(new int[0]); int[] solution = c3po.think(new int[0]);
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re);
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
try{ try{
String test = herbert.speak(new int[0]); String test = c3po.speak(new int[0]);
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re);
assertEquals(0, expectedMessage.compareTo(re.getMessage())); assertEquals(0, expectedMessage.compareTo(re.getMessage()));
} }
} }
@Test
void testExceptionHistory() {
String expectedMessage = "Herbert leer";
try{
int[] solution = c3po.think(new int[0]);
}catch(RobotException re){
assertEquals(re.getMessage(), c3po.getLastException().toString());
}
}
} }

View File

@ -1,43 +1,114 @@
package tests; package tests;
import src.domain.R2D2; import domain.R2D2;
import domain.RobotType;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.RobotMagicValueException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class R2D2Test { class R2D2Test {
R2D2 Herbert; R2D2 r2d2;
int id = 0; int id = 0;
String name = "Herbert"; String name = "r2d2";
@BeforeEach @BeforeEach
void setup(){ void setup(){
Herbert = new R2D2(id, name); r2d2 = new R2D2(id, name);
r2d2.triggerPowerSwitch();
} }
//Tests for basic functions
@Test @Test
void getId() { void getId() {
assertEquals(id, Herbert.getId()); assertEquals(id, r2d2.getId());
} }
@Test @Test
void getName() { void getName() {
assertEquals(name, Herbert.getName()); assertEquals(name, r2d2.getName());
} }
@Test @Test
void triggerPowerSwitch() { void triggerPowerSwitch() {
Herbert.triggerPowerSwitch(); assertTrue(r2d2.isPowerOn());
assertTrue(Herbert.isPowerOn()); r2d2.triggerPowerSwitch();
assertFalse(r2d2.isPowerOn());
} }
@Test @Test
void isPowerOn() { void R2D2SpeakTestStandard() throws RobotException {
assertFalse(Herbert.isPowerOn()); assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
Herbert.triggerPowerSwitch();
assertTrue(Herbert.isPowerOn());
} }
@Test
void R2D2SpeakTestEmpty() {
try{
String solution = r2d2.speak(new int[]{});
}catch(RobotException re){
assertEquals(0, "r2d2 leer".compareTo(re.getMessage()));
}
}
@Test
void R2D2SpeakTestZeros() throws RobotException {
assertEquals("0, 0, 0, 0", r2d2.speak(new int[4]));
}
@Test
void R2D2SpeakTestOneElement() throws RobotException {
assertEquals("1", r2d2.speak(new int[]{1}));
}
@Test
void R2D2SpeakTestUnitElements() throws RobotException {
assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1}));
}
@Test
void R2D2SpeakTestMagicNumberException() {
try{
String solution = r2d2.speak(new int[]{42});
}catch(RobotException re){
assertEquals(0, "r2d2 Magic value".compareTo(re.getMessage()));
}
}
@Test
void speak(){
String solution = "12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23";
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
String array = "";
try{
array = r2d2.speak(input);
}catch(RobotException ignored){
}
assertEquals(0, array.compareTo(solution));
}
@Test
void think(){
int[] solution = { 2, 4, 4, 5, 7, 12, 23, 56, 433};
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
int[] value = new int[9];
try{
value = r2d2.think(input);
}catch(RobotException ignored){
}
r2d2.triggerPowerSwitch();
try{
value = r2d2.think(input);
}catch(RobotException re){
System.out.println(re);
}
for(int i = 0; i < input.length; i++){
assertEquals(solution[i], value[i]);
}
}
} }