Refactored some tests, added IClonable to Player

This commit is contained in:
martin 2023-10-21 12:28:09 +02:00
parent 719d37c6ee
commit a8fe82dd50
4 changed files with 25 additions and 45 deletions

View File

@ -29,30 +29,17 @@ public class ActionServiceTests
{ {
_spawns = CreateQueue(); _spawns = CreateQueue();
_game = new pacMan.Services.Game(_spawns); _game = new pacMan.Services.Game(_spawns);
_whiteMessage = new ActionMessage _whiteMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_whitePlayer.Username) };
{ _blackMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_blackPlayer.Username) };
Action = GameAction.JoinGame, _redMessage = new ActionMessage { Action = GameAction.JoinGame, Data = SerializeData(_redPlayer.Username) };
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<GameService>(Substitute.For<ILogger<GameService>>()); _gameService = Substitute.For<GameService>(Substitute.For<ILogger<GameService>>());
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _gameService); _service = new ActionService(Substitute.For<ILogger<ActionService>>(), _gameService);
} }
private JsonElement SerializeData(string username) => private JsonElement SerializeData(string username) =>
JsonDocument.Parse(JsonSerializer.Serialize( JsonDocument.Parse(JsonSerializer.Serialize(
new JoinGameData { Username = username, GameId = _game.Id } new JoinGameData { Username = username, GameId = _game.Id }
) )).RootElement;
).RootElement;
private static Queue<DirectionalPosition> CreateQueue() => private static Queue<DirectionalPosition> CreateQueue() =>
new(new[] new(new[]
@ -94,13 +81,8 @@ public class ActionServiceTests
[Test] [Test]
public void PlayerInfo_DataIsNotJoinGameData() public void PlayerInfo_DataIsNotJoinGameData()
{ {
var serialized = var serialized = JsonDocument.Parse(JsonSerializer.Serialize(new Box { Colour = "white" }));
JsonDocument.Parse(JsonSerializer.Serialize(new Box { Colour = "white" })); var message = new ActionMessage { Action = GameAction.JoinGame, Data = serialized.RootElement };
var message = new ActionMessage
{
Action = GameAction.JoinGame,
Data = serialized.RootElement
};
Assert.Throws<JsonException>(() => _service.FindGame(message.Data)); Assert.Throws<JsonException>(() => _service.FindGame(message.Data));
} }
@ -176,28 +158,29 @@ public class ActionServiceTests
var result = _service.Ready(); var result = _service.Ready();
// If selected the state is changed to InGame // If selected the state is changed to InGame
_whitePlayer.State = State.InGame; _whitePlayer.State = State.InGame;
var players = result.GetType().GetProperty("Players")?.GetValue(result) as IEnumerable<Player>; Assert.That(result.Players.First().Username, Is.EqualTo(_whitePlayer.Username));
Assert.That(players?.First().Username, Is.EqualTo(_whitePlayer.Username));
} }
[Test] [Test]
public void Ready_TwoReady() public void Ready_TwoReady()
{ {
var group = new pacMan.Services.Game(new Queue<DirectionalPosition>()) var group = new pacMan.Services.Game(new Queue<DirectionalPosition>())
{ Players = { _blackPlayer, _whitePlayer } }; {
Players = { _blackPlayer, _whitePlayer }
};
_service.Game = group; _service.Game = group;
_service.Player = _blackPlayer; _service.Player = _blackPlayer;
var result = _service.Ready(); 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; _service.Player = _whitePlayer;
result = _service.Ready(); result = _service.Ready();
var players = result.GetType().GetProperty("Players")?.GetValue(result) as IEnumerable<Player>; Assert.That(result.Players.First().Username,
Assert.That(players?.First().Username, Is.EqualTo(_blackPlayer.Username).Or.EqualTo(_whitePlayer.Username)); Is.EqualTo(_blackPlayer.Username).Or.EqualTo(_whitePlayer.Username));
} }
#endregion #endregion
@ -216,8 +199,8 @@ public class ActionServiceTests
{ {
_service.Game = _service.Game =
new pacMan.Services.Game(new Queue<DirectionalPosition>( new pacMan.Services.Game(new Queue<DirectionalPosition>(
new[] { new DirectionalPosition { At = new Position { X = 3, Y = 3 }, Direction = Direction.Up } })) new[] { new DirectionalPosition { At = new Position { X = 3, Y = 3 }, Direction = Direction.Up } }
{ Players = { _whitePlayer } }; )) { Players = { _whitePlayer } };
var name = _service.FindNextPlayer(); var name = _service.FindNextPlayer();
Assert.That(name, Is.EqualTo(_whitePlayer.Username)); Assert.That(name, Is.EqualTo(_whitePlayer.Username));

View File

@ -123,9 +123,9 @@ public class GameTests
[Test] [Test]
public void AddPlayer_WhenNameExists() public void AddPlayer_WhenNameExists()
{ {
var redClone = _redPlayer.Clone(); var redClone = _redPlayer.Clone() as Player;
_game.AddPlayer(_redPlayer); _game.AddPlayer(_redPlayer);
Assert.DoesNotThrow(() => _game.AddPlayer(redClone)); Assert.DoesNotThrow(() => _game.AddPlayer(redClone!));
} }
[Test] [Test]

View File

@ -26,13 +26,4 @@ internal static class Players
{ {
Colour = colour Colour = colour
}; };
internal static Player Clone(this Player player) =>
new()
{
Box = player.Box,
Colour = player.Colour,
Username = player.Username,
PacMan = player.PacMan
};
} }

View File

@ -11,7 +11,7 @@ public enum State
Disconnected Disconnected
} }
public class Player : IEquatable<Player> public class Player : IEquatable<Player>, ICloneable
{ {
[JsonPropertyName("username")] public required string Username { get; init; } [JsonPropertyName("username")] public required string Username { get; init; }
@ -23,6 +23,12 @@ public class Player : IEquatable<Player>
[JsonPropertyName("state")] public State State { get; set; } = State.WaitingForPlayers; [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) public bool Equals(Player? other)
{ {
if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(null, other)) return false;