package domain; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import utility.robot_exceptions.RobotException; import static org.junit.jupiter.api.Assertions.*; class C3POTest { C3PO Herbert; 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()); } @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())); } } }