From a0039979c22d0ec80294d5de6c572ab8f9095110 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 15 Jan 2022 19:23:37 +0100 Subject: [PATCH] player_management: selectPlayer adds new players to player-list --- .../tims/player_management/PlayerManager.java | 6 ++- .../player_management/PlayerManagerTest.java | 38 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index 8ab6b68..2e3b322 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -27,7 +27,11 @@ public class PlayerManager { } } - return new Player(playerName, 0); + Player newPlayer = new Player(playerName, 0); + + players.add(newPlayer); + + return newPlayer; } public void loadPlayers(String fileName) { diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index b3c9dcd..d1c32f1 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -5,9 +5,11 @@ import static org.assertj.core.api.Assertions.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.util.LinkedList; import java.util.List; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -25,11 +27,11 @@ class PlayerManagerTest { } private static Stream testCasesForSelectPlayer() { - return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", List.of(), + return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", new LinkedList(List.of()), "Tobias", new Player("Tobias", 0)), - Arguments.of("NoPlayerWithNameInListReturnNewPlayer", List.of(new Player("Steffen", 40), new Player("Lorenz", 60)), + Arguments.of("NoPlayerWithNameInListReturnNewPlayer", new LinkedList(List.of(new Player("Steffen", 40), new Player("Lorenz", 60))), "Tobias", new Player("Tobias", 0)), - Arguments.of("PlayerWithNameInListReturnPlayerInList", List.of(new Player("Steffen", 40), new Player("Tobias", 50)), + Arguments.of("PlayerWithNameInListReturnPlayerInList", new LinkedList(List.of(new Player("Steffen", 40), new Player("Tobias", 50))), "Tobias", new Player("Tobias", 50))); } @@ -74,5 +76,35 @@ class PlayerManagerTest { Arguments.of("WriteElementsOfListToFile", "src/test/java/resources/player_testdata2.csv", List.of(new Player("Tobias", 50), new Player("Steffen", 60)))); } + + @Test + void selectPlayerAddsNewPlayersToList() { + LinkedList players = new LinkedList(); + players.add(new Player("Steffen", 40)); + players.add(new Player("Max", 60)); + + manager.setPlayers(players); + manager.selectPlayer("Tobias"); + + List expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60), new Player("Tobias", 0)); + List calculatedResult = manager.getPlayers(); + + assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); + } + + @Test + void selectExistingPlayerDoesntChangeTheList() { + LinkedList players = new LinkedList(); + players.add(new Player("Steffen", 40)); + players.add(new Player("Max", 60)); + + manager.setPlayers(players); + manager.selectPlayer("Max"); + + List expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60)); + List calculatedResult = manager.getPlayers(); + + assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); + } }