Finished first functioning and successful unit test (tests noise handling)

This commit is contained in:
Alexander Sazonov 2022-04-28 13:25:03 +02:00
parent f37597a5cd
commit 93b6bab957
3 changed files with 56 additions and 4 deletions

View File

@ -15,15 +15,13 @@ public class NoiseHandler {
* predator infecting a victim, so if there are already multiple ghosts in the game, the method
* should be called for each of them individually.
* @param passengers passengers of the train the game is played in
* @param predator ghost that has infected a human player during this night (called upon as
* passenger for convenience reasons)
* @param victim human player who has been turned into a ghost this night
* @param noiseAmount array containing information about how many times each passenger heard a noise this night
* @param game current game instance
* @return updated array with info on who heard how many noises
*/
public int[] noiseNotifier(Passenger[] passengers, Passenger predator, Passenger victim, int[] noiseAmount, Game game) {
public int[] noiseNotifier(Passenger predator, Passenger victim, int[] noiseAmount) {
if (predator.getPosition() - victim.getPosition()
> 0) { // if predator is to the right of victim
for (int i = predator.getPosition() - 1; i > victim.getPosition(); i--) {

View File

@ -97,7 +97,7 @@ public class VoteHandler {
for (int i = 0; i < passengers.length; i++) {
if (passengers[i].getIsGhost() && i != ghostPosition) {
NoiseHandler n = new NoiseHandler();
noiseAmount = n.noiseNotifier(passengers, passengers[i], g, noiseAmount, game);
noiseAmount = n.noiseNotifier(passengers[i], g, noiseAmount);
}
}
for (int i = 0; i < passengers.length; i++) {

View File

@ -0,0 +1,54 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
class NoiseHandlerTest {
Passenger[] passengers = new Passenger[6]; // create new collection of passengers
Passenger predator = new Passenger();
Passenger victim = new Passenger(); // player who will be turned into a ghost this night
int[] noiseAmount = new int[6]; // collect amount of times a ghost walked by the humans (= noises heard)
int[] noiseAmountGuideline = new int[6]; // array that represents the right people having heard the right noise amount
@BeforeEach
void preparation() {
for (int i = 0; i < passengers.length; i++) {
passengers[i] = new Passenger();
}
predator.setGhost();
}
@Test
@DisplayName("The right players hear the right amount of noises")
void noiseTest1() {
/* test 1: people heard the right noise amount for following case: ghost is at position 5 and ghostifies
player at position 2 --> noises at positions 3 and 4
*/
predator.setPosition(5);
victim.setPosition(2);
passengers[predator.getPosition()] = predator;
passengers[victim.getPosition()] = victim;
for (int i = 3; i < 5; i++) {
noiseAmountGuideline[i]++;
}
/* call main method of NoiseHandler so that all passengers are notified about all noises (notifications are
updated in noiseAmount array) */
for (int i = 0; i < passengers.length; i++) {
if (passengers[i].getIsGhost() && i != victim.getPosition()) {
NoiseHandler n = new NoiseHandler();
noiseAmount = n.noiseNotifier(passengers[i], victim, noiseAmount);
}
}
assertArrayEquals(noiseAmountGuideline, noiseAmount);
}
}