2022-12-09 01:38:11 +01:00
|
|
|
package domain;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2023-01-06 22:55:33 +01:00
|
|
|
import utility.robot_exceptions.RobotException;
|
2022-12-22 12:29:04 +01:00
|
|
|
|
2022-12-09 01:38:11 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
class C3POTest {
|
|
|
|
|
2022-12-20 12:03:41 +01:00
|
|
|
C3PO Herbert;
|
2022-12-09 01:38:11 +01:00
|
|
|
int id = 0;
|
|
|
|
String name = "Herbert";
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
void setup(){
|
|
|
|
Herbert = new C3PO(id, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Tests for basic functions
|
|
|
|
@Test
|
|
|
|
void getId() {
|
|
|
|
|
|
|
|
assertEquals(id, Herbert.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void getName() {
|
|
|
|
assertEquals(name, Herbert.getName());
|
|
|
|
assertEquals(name,
|
|
|
|
Herbert.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void triggerPowerSwitch() {
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
assertTrue(Herbert.isPowerOn());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void isPowerOn() {
|
|
|
|
assertFalse(Herbert.isPowerOn());
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
assertTrue(Herbert.isPowerOn());
|
|
|
|
}
|
|
|
|
|
2022-12-22 12:29:04 +01:00
|
|
|
@Test
|
|
|
|
void speak(){
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
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 = Herbert.speak(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
System.out.println(re);
|
|
|
|
}
|
|
|
|
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 = Herbert.think(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
System.out.println(re);
|
|
|
|
}
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
try{
|
|
|
|
value = Herbert.think(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
System.out.println(re);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < input.length; i++){
|
|
|
|
assertEquals(solution[i], value[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void magicValueException(){
|
|
|
|
int[] input = {3,2,42};
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
String expectedMessage = "Herbert has an unknown error. Code 42.";
|
|
|
|
try{
|
|
|
|
int[] solution = Herbert.think(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
String test = Herbert.speak(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void illegalStateException(){
|
|
|
|
int[] input = {3,2,42};
|
|
|
|
String expectedMessage = "Herbert is turned off.";
|
|
|
|
try{
|
|
|
|
int[] solution = Herbert.think(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
String test = Herbert.speak(input);
|
|
|
|
}catch(RobotException re){
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void arrayEmptyException(){
|
|
|
|
String expectedMessage = "Herbert got an empty array.";
|
|
|
|
Herbert.triggerPowerSwitch();
|
|
|
|
try{
|
|
|
|
int[] solution = Herbert.think(new int[0]);
|
|
|
|
}catch(RobotException re){
|
|
|
|
System.out.println(re);
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
String test = Herbert.speak(new int[0]);
|
|
|
|
}catch(RobotException re){
|
|
|
|
System.out.println(re);
|
|
|
|
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-09 01:38:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|