From a8fe82dd50d8fd5c75c1c707427e96c47d3fcd92 Mon Sep 17 00:00:00 2001 From: martin Date: Sat, 21 Oct 2023 12:28:09 +0200 Subject: [PATCH] Refactored some tests, added IClonable to Player --- BackendTests/Services/ActionServiceTests.cs | 49 +++++++------------- BackendTests/Services/GameTests.cs | 4 +- BackendTests/TestUtils/Players.cs | 9 ---- pac-man-board-game/GameStuff/Items/Player.cs | 8 +++- 4 files changed, 25 insertions(+), 45 deletions(-) diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs index 734f282..503fbd5 100644 --- a/BackendTests/Services/ActionServiceTests.cs +++ b/BackendTests/Services/ActionServiceTests.cs @@ -29,30 +29,17 @@ public class ActionServiceTests { _spawns = CreateQueue(); _game = new pacMan.Services.Game(_spawns); - _whiteMessage = new ActionMessage - { - Action = GameAction.JoinGame, - Data = SerializeData(_whitePlayer.Username) - }; - _blackMessage = new ActionMessage - { - Action = GameAction.JoinGame, - Data = SerializeData(_blackPlayer.Username) - }; - _redMessage = new ActionMessage - { - Action = GameAction.JoinGame, - Data = SerializeData(_redPlayer.Username) - }; + _whiteMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_whitePlayer.Username) }; + _blackMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_blackPlayer.Username) }; + _redMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_redPlayer.Username) }; _gameService = Substitute.For(Substitute.For>()); _service = new ActionService(Substitute.For>(), _gameService); } private JsonElement SerializeData(string username) => JsonDocument.Parse(JsonSerializer.Serialize( - new JoinGameData { Username = username, GameId = _game.Id } - ) - ).RootElement; + new JoinGameData { Username = username, GameId = _game.Id } + )).RootElement; private static Queue CreateQueue() => new(new[] @@ -94,13 +81,8 @@ public class ActionServiceTests [Test] public void PlayerInfo_DataIsNotJoinGameData() { - var serialized = - JsonDocument.Parse(JsonSerializer.Serialize(new Box { Colour = "white" })); - var message = new ActionMessage - { - Action = GameAction.JoinGame, - Data = serialized.RootElement - }; + var serialized = JsonDocument.Parse(JsonSerializer.Serialize(new Box { Colour = "white" })); + var message = new ActionMessage { Action = GameAction.JoinGame, Data = serialized.RootElement }; Assert.Throws(() => _service.FindGame(message.Data)); } @@ -176,28 +158,29 @@ public class ActionServiceTests var result = _service.Ready(); // If selected the state is changed to InGame _whitePlayer.State = State.InGame; - var players = result.GetType().GetProperty("Players")?.GetValue(result) as IEnumerable; - Assert.That(players?.First().Username, Is.EqualTo(_whitePlayer.Username)); + Assert.That(result.Players.First().Username, Is.EqualTo(_whitePlayer.Username)); } [Test] public void Ready_TwoReady() { var group = new pacMan.Services.Game(new Queue()) - { Players = { _blackPlayer, _whitePlayer } }; + { + Players = { _blackPlayer, _whitePlayer } + }; _service.Game = group; _service.Player = _blackPlayer; var result = _service.Ready(); - Assert.That(result.GetType().GetProperty("AllReady")?.GetValue(result), Is.EqualTo(false)); + Assert.That(result.AllReady, Is.EqualTo(false)); _service.Player = _whitePlayer; result = _service.Ready(); - var players = result.GetType().GetProperty("Players")?.GetValue(result) as IEnumerable; - Assert.That(players?.First().Username, Is.EqualTo(_blackPlayer.Username).Or.EqualTo(_whitePlayer.Username)); + Assert.That(result.Players.First().Username, + Is.EqualTo(_blackPlayer.Username).Or.EqualTo(_whitePlayer.Username)); } #endregion @@ -216,8 +199,8 @@ public class ActionServiceTests { _service.Game = new pacMan.Services.Game(new Queue( - new[] { new DirectionalPosition { At = new Position { X = 3, Y = 3 }, Direction = Direction.Up } })) - { Players = { _whitePlayer } }; + new[] { new DirectionalPosition { At = new Position { X = 3, Y = 3 }, Direction = Direction.Up } } + )) { Players = { _whitePlayer } }; var name = _service.FindNextPlayer(); Assert.That(name, Is.EqualTo(_whitePlayer.Username)); diff --git a/BackendTests/Services/GameTests.cs b/BackendTests/Services/GameTests.cs index 5ceab4b..fb0822f 100644 --- a/BackendTests/Services/GameTests.cs +++ b/BackendTests/Services/GameTests.cs @@ -123,9 +123,9 @@ public class GameTests [Test] public void AddPlayer_WhenNameExists() { - var redClone = _redPlayer.Clone(); + var redClone = _redPlayer.Clone() as Player; _game.AddPlayer(_redPlayer); - Assert.DoesNotThrow(() => _game.AddPlayer(redClone)); + Assert.DoesNotThrow(() => _game.AddPlayer(redClone!)); } [Test] diff --git a/BackendTests/TestUtils/Players.cs b/BackendTests/TestUtils/Players.cs index 2083995..c7cbff4 100644 --- a/BackendTests/TestUtils/Players.cs +++ b/BackendTests/TestUtils/Players.cs @@ -26,13 +26,4 @@ internal static class Players { Colour = colour }; - - internal static Player Clone(this Player player) => - new() - { - Box = player.Box, - Colour = player.Colour, - Username = player.Username, - PacMan = player.PacMan - }; } diff --git a/pac-man-board-game/GameStuff/Items/Player.cs b/pac-man-board-game/GameStuff/Items/Player.cs index 640be81..bbfaaa1 100644 --- a/pac-man-board-game/GameStuff/Items/Player.cs +++ b/pac-man-board-game/GameStuff/Items/Player.cs @@ -11,7 +11,7 @@ public enum State Disconnected } -public class Player : IEquatable +public class Player : IEquatable, ICloneable { [JsonPropertyName("username")] public required string Username { get; init; } @@ -23,6 +23,12 @@ public class Player : IEquatable [JsonPropertyName("state")] public State State { get; set; } = State.WaitingForPlayers; + public object Clone() => + new Player + { + Username = Username, Colour = Colour, PacMan = PacMan, Box = Box, State = State + }; + public bool Equals(Player? other) { if (ReferenceEquals(null, other)) return false;