diff --git a/dicegame/src/test/dicegame/logic/LogicTest.java b/dicegame/src/test/dicegame/logic/LogicTest.java new file mode 100644 index 00000000..efbdf33b --- /dev/null +++ b/dicegame/src/test/dicegame/logic/LogicTest.java @@ -0,0 +1,90 @@ +package test.dicegame.logic; + +import dicegame.logic.Logic; +import dicegame.logic.SelectionException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.Random; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; + +class LogicTest { + Logic logic; + Random random = new Random(1234L); + + /** + * K1 Passed index: + * - index: 0-5 + * - error: out of bounds + * + * K2 The number of the dice passed: + * - value: 1 | 5 + * - error: !(1 | 5) + * + * K3 Existing fixation of the passed dice: + * - fixable: true | false + * - error: dice was fixed previously + * + * K4 Number of other dice of the same number fixed in the current roll: + * - bonus triggered: (2 | 5) of the same number fixed + * - no bonus triggered: (1 | 3 | 4) of the same number fixed + * - first fix of the game: 0 of the same number fixed + * + * K5 Number of fixed dice of the same number in previous rolls: + * - bonus triggered: (2 | 5) of the same number fixed + * - no bonus triggered: (1 | 3 | 4) of the same number fixed + * - first fix of the game: 0 of the same number fixed + */ + + @AfterEach + void tearDown() { + logic = null; + } + + // Input a valid index: 0-5 + // The value for this index is not 1 or 5 + @Test + void changeDiceStateWithValidDiceNot1Or5() { + Random mockedRandom = Mockito.mock(Random.class); + Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumberNot0or4()); + logic = new Logic(mockedRandom); + logic.startGame(); + logic.rollDice(); + + Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0)); + String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!"; + String actualMessage = e.getMessage(); + assertEquals(expectedMessage, actualMessage); + } + + @Test + void changeDiceStateWhenFixedInPrevRoll() { + Random mockedRandom = Mockito.mock(Random.class); + Mockito.when(mockedRandom.nextInt(6)).thenAnswer(invocation -> randomNumber0or4()); + logic = new Logic(mockedRandom); + logic.startGame(); + logic.rollDice(); + assertDoesNotThrow(() -> logic.changeDiceState(0)); + logic.rollDice(); + assertThrows(Exception.class, () -> logic.changeDiceState(0)); + } + + private int randomNumber0or4() { + return randomNumber(n -> n != 0 && n != 4); + } + + private int randomNumberNot0or4() { + return randomNumber(n -> n == 0 || n == 4); + } + + private int randomNumber(Predicate condition) { + int randomNumber; + do { + randomNumber = random.nextInt(6); // 0 to 5 + } while (condition.test(randomNumber)); // Repeat until condition is satisfied + return randomNumber; + } +} \ No newline at end of file