From ab52179b5a0b07fe72400e09be22cd99f3042670 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Wed, 12 Jul 2023 21:21:51 +0200 Subject: [PATCH] Fixed actionservice tests --- BackendTests/Services/ActionServiceTests.cs | 85 +++++++++++++++----- pac-man-board-game/Services/ActionService.cs | 29 ++++--- 2 files changed, 81 insertions(+), 33 deletions(-) diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs index 660c54d..88f2367 100644 --- a/BackendTests/Services/ActionServiceTests.cs +++ b/BackendTests/Services/ActionServiceTests.cs @@ -1,6 +1,5 @@ using System.Text.Json; using BackendTests.TestUtils; -using Microsoft.CSharp.RuntimeBinder; using Microsoft.Extensions.Logging; using NSubstitute; using pacMan.Game; @@ -13,12 +12,23 @@ namespace BackendTests.Services; public class ActionServiceTests { + private readonly IPlayer _blackPlayer = Players.Create("black"); + private readonly IPlayer _redPlayer = Players.Create("red"); + private readonly IPlayer _whitePlayer = Players.Create("white"); + private ActionMessage _blackMessage = null!; + private ActionMessage _redMessage = null!; private IActionService _service = null!; + private ActionMessage _whiteMessage = null!; private IWebSocketService _wssSub = null!; [SetUp] public void Setup() { + _whiteMessage = new ActionMessage + { Action = GameAction.PlayerInfo, Data = JsonSerializer.Serialize(_whitePlayer) }; + _blackMessage = new ActionMessage + { Action = GameAction.PlayerInfo, Data = JsonSerializer.Serialize(_blackPlayer) }; + _redMessage = new ActionMessage { Action = GameAction.PlayerInfo, Data = JsonSerializer.Serialize(_redPlayer) }; _wssSub = Substitute.For(Substitute.For>()); _service = new ActionService(Substitute.For>(), _wssSub); } @@ -44,9 +54,9 @@ public class ActionServiceTests public void PlayerInfo_DataIsNull() { var message = new ActionMessage { Action = GameAction.PlayerInfo, Data = "null" }; - Assert.Throws(() => _service.PlayerInfo(message)); + Assert.Throws(() => _service.SetPlayerInfo(message)); message.Data = null; - Assert.Throws(() => _service.PlayerInfo(message)); + Assert.Throws(() => _service.SetPlayerInfo(message)); } [Test] @@ -55,23 +65,17 @@ public class ActionServiceTests var message = new ActionMessage { Action = GameAction.PlayerInfo, - Data = new Box { Colour = "white", Pellets = new List() } + Data = JsonSerializer.Serialize(new Box { Colour = "white", Pellets = new List() }) }; - Assert.Throws(() => _service.PlayerInfo(message)); + Assert.Throws(() => _service.SetPlayerInfo(message)); } [Test] public void PlayerInfo_DataIsPlayer() { - var player = Players.Create("white"); - var message = new ActionMessage - { - Action = GameAction.PlayerInfo, - Data = JsonSerializer.Serialize(player) - }; - var players = _service.PlayerInfo(message); + var players = _service.SetPlayerInfo(_whiteMessage); - Assert.That(new List { player }, Is.EqualTo(players)); + Assert.That(new List { _whitePlayer }, Is.EqualTo(players)); } #endregion @@ -81,13 +85,19 @@ public class ActionServiceTests [Test] public void DoAction_NegativeAction() { - Assert.Fail(); + const string data = "Nothing happens"; + var message = new ActionMessage { Action = (GameAction)(-1), Data = data }; + _service.DoAction(message); + Assert.That(message.Data, Is.EqualTo(data)); } [Test] public void DoAction_OutOfBoundsAction() { - Assert.Fail(); + const string data = "Nothing happens"; + var message = new ActionMessage { Action = (GameAction)100, Data = data }; + _service.DoAction(message); + Assert.That(message.Data, Is.EqualTo(data)); } #endregion @@ -97,19 +107,54 @@ public class ActionServiceTests [Test] public void Ready_PlayerIsNull() { - Assert.Fail(); + var result = _service.Ready(); + Assert.That(result, Is.InstanceOf()); } [Test] - public void Ready_SomeReady() + public void Ready_NotAllReady() { - Assert.Fail(); + _service.SetPlayerInfo(_whiteMessage); + _service.SetPlayerInfo(_blackMessage); + + var result = _service.Ready(); + + Assert.That(result.GetType().GetProperty("Starter"), Is.Null); + + _service.SetPlayerInfo(_redMessage); + + result = _service.Ready(); + + Assert.That(result.GetType().GetProperty("Starter"), Is.Null); } [Test] - public void Ready_AllReady() + public void Ready_OneReady() { - Assert.Fail(); + _service.SetPlayerInfo(_whiteMessage); + var result = _service.Ready(); + // If selected the state is changed to InGame + _whitePlayer.State = State.InGame; + Assert.That(result.GetType().GetProperty("Starter")?.GetValue(result), Is.EqualTo(_whitePlayer)); + } + + [Test] + public void Ready_TwoReady() + { + _service.Group.AddPlayer(_blackPlayer); + _service.Group.AddPlayer(_whitePlayer); + _service.Player = _blackPlayer; + + var result = _service.Ready(); + + Assert.That(result.GetType().GetProperty("Starter"), Is.Null); + + _service.Player = _whitePlayer; + + result = _service.Ready(); + + Assert.That(result.GetType().GetProperty("Starter")?.GetValue(result), + Is.EqualTo(_whitePlayer).Or.EqualTo(_blackPlayer)); } #endregion diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index 52fae4f..f9153fb 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -9,9 +9,11 @@ namespace pacMan.Services; public interface IActionService { + IPlayer Player { get; set; } + GameGroup Group { get; set; } void DoAction(ActionMessage message); List RollDice(); - List PlayerInfo(ActionMessage message); + List SetPlayerInfo(ActionMessage message); object Ready(); } @@ -21,9 +23,6 @@ public class ActionService : IActionService // TODO tests private readonly ILogger _logger; private readonly IWebSocketService _wsService; - private GameGroup _group = new(); - private IPlayer? _player; - public ActionService(ILogger logger, IWebSocketService wsService) { _logger = logger; @@ -31,12 +30,16 @@ public class ActionService : IActionService // TODO tests _wsService = wsService; } + public GameGroup Group { get; set; } = new(); + + public IPlayer? Player { get; set; } + public void DoAction(ActionMessage message) { message.Data = message.Action switch { GameAction.RollDice => RollDice(), - GameAction.PlayerInfo => PlayerInfo(message), + GameAction.PlayerInfo => SetPlayerInfo(message), GameAction.Ready => Ready(), _ => message.Data }; @@ -50,13 +53,13 @@ public class ActionService : IActionService // TODO tests return rolls; } - public List PlayerInfo(ActionMessage message) + public List SetPlayerInfo(ActionMessage message) { try { // Receieved JsonElement from frontend - _player = JsonSerializer.Deserialize(message.Data); - _group = _wsService.AddPlayer(_player); + Player = JsonSerializer.Deserialize(message.Data); + Group = _wsService.AddPlayer(Player); } catch (RuntimeBinderException e) { @@ -66,20 +69,20 @@ public class ActionService : IActionService // TODO tests throw; } - return _group.Players; + return Group.Players; } public object Ready() { object data; - if (_player != null) + if (Player != null) { - var players = _group.SetReady(_player).ToArray(); + var players = Group.SetReady(Player).ToArray(); if (players.All(p => p.State == State.Ready)) { // TODO roll to start - _group.SetAllInGame(); - data = new { AllReady = true, Players = players, Starter = _group.RandomPlayer }; + Group.SetAllInGame(); + data = new { AllReady = true, Players = players, Starter = Group.RandomPlayer }; } else {