Fixed actionservice tests

This commit is contained in:
Martin Berg Alstad 2023-07-12 21:21:51 +02:00
parent ea2f31b905
commit ab52179b5a
2 changed files with 81 additions and 33 deletions

View File

@ -1,6 +1,5 @@
using System.Text.Json; using System.Text.Json;
using BackendTests.TestUtils; using BackendTests.TestUtils;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NSubstitute; using NSubstitute;
using pacMan.Game; using pacMan.Game;
@ -13,12 +12,23 @@ namespace BackendTests.Services;
public class ActionServiceTests 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 IActionService _service = null!;
private ActionMessage _whiteMessage = null!;
private IWebSocketService _wssSub = null!; private IWebSocketService _wssSub = null!;
[SetUp] [SetUp]
public void 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<WebSocketService>(Substitute.For<ILogger<WebSocketService>>()); _wssSub = Substitute.For<WebSocketService>(Substitute.For<ILogger<WebSocketService>>());
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _wssSub); _service = new ActionService(Substitute.For<ILogger<ActionService>>(), _wssSub);
} }
@ -44,9 +54,9 @@ public class ActionServiceTests
public void PlayerInfo_DataIsNull() public void PlayerInfo_DataIsNull()
{ {
var message = new ActionMessage { Action = GameAction.PlayerInfo, Data = "null" }; var message = new ActionMessage { Action = GameAction.PlayerInfo, Data = "null" };
Assert.Throws<NullReferenceException>(() => _service.PlayerInfo(message)); Assert.Throws<NullReferenceException>(() => _service.SetPlayerInfo(message));
message.Data = null; message.Data = null;
Assert.Throws<NullReferenceException>(() => _service.PlayerInfo(message)); Assert.Throws<NullReferenceException>(() => _service.SetPlayerInfo(message));
} }
[Test] [Test]
@ -55,23 +65,17 @@ public class ActionServiceTests
var message = new ActionMessage var message = new ActionMessage
{ {
Action = GameAction.PlayerInfo, Action = GameAction.PlayerInfo,
Data = new Box { Colour = "white", Pellets = new List<Pellet>() } Data = JsonSerializer.Serialize(new Box { Colour = "white", Pellets = new List<Pellet>() })
}; };
Assert.Throws<RuntimeBinderException>(() => _service.PlayerInfo(message)); Assert.Throws<JsonException>(() => _service.SetPlayerInfo(message));
} }
[Test] [Test]
public void PlayerInfo_DataIsPlayer() public void PlayerInfo_DataIsPlayer()
{ {
var player = Players.Create("white"); var players = _service.SetPlayerInfo(_whiteMessage);
var message = new ActionMessage
{
Action = GameAction.PlayerInfo,
Data = JsonSerializer.Serialize(player)
};
var players = _service.PlayerInfo(message);
Assert.That(new List<IPlayer> { player }, Is.EqualTo(players)); Assert.That(new List<IPlayer> { _whitePlayer }, Is.EqualTo(players));
} }
#endregion #endregion
@ -81,13 +85,19 @@ public class ActionServiceTests
[Test] [Test]
public void DoAction_NegativeAction() 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] [Test]
public void DoAction_OutOfBoundsAction() 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 #endregion
@ -97,19 +107,54 @@ public class ActionServiceTests
[Test] [Test]
public void Ready_PlayerIsNull() public void Ready_PlayerIsNull()
{ {
Assert.Fail(); var result = _service.Ready();
Assert.That(result, Is.InstanceOf<string>());
} }
[Test] [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] [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 #endregion

View File

@ -9,9 +9,11 @@ namespace pacMan.Services;
public interface IActionService public interface IActionService
{ {
IPlayer Player { get; set; }
GameGroup Group { get; set; }
void DoAction(ActionMessage message); void DoAction(ActionMessage message);
List<int> RollDice(); List<int> RollDice();
List<IPlayer> PlayerInfo(ActionMessage message); List<IPlayer> SetPlayerInfo(ActionMessage message);
object Ready(); object Ready();
} }
@ -21,9 +23,6 @@ public class ActionService : IActionService // TODO tests
private readonly ILogger<ActionService> _logger; private readonly ILogger<ActionService> _logger;
private readonly IWebSocketService _wsService; private readonly IWebSocketService _wsService;
private GameGroup _group = new();
private IPlayer? _player;
public ActionService(ILogger<ActionService> logger, IWebSocketService wsService) public ActionService(ILogger<ActionService> logger, IWebSocketService wsService)
{ {
_logger = logger; _logger = logger;
@ -31,12 +30,16 @@ public class ActionService : IActionService // TODO tests
_wsService = wsService; _wsService = wsService;
} }
public GameGroup Group { get; set; } = new();
public IPlayer? Player { get; set; }
public void DoAction(ActionMessage message) public void DoAction(ActionMessage message)
{ {
message.Data = message.Action switch message.Data = message.Action switch
{ {
GameAction.RollDice => RollDice(), GameAction.RollDice => RollDice(),
GameAction.PlayerInfo => PlayerInfo(message), GameAction.PlayerInfo => SetPlayerInfo(message),
GameAction.Ready => Ready(), GameAction.Ready => Ready(),
_ => message.Data _ => message.Data
}; };
@ -50,13 +53,13 @@ public class ActionService : IActionService // TODO tests
return rolls; return rolls;
} }
public List<IPlayer> PlayerInfo(ActionMessage message) public List<IPlayer> SetPlayerInfo(ActionMessage message)
{ {
try try
{ {
// Receieved JsonElement from frontend // Receieved JsonElement from frontend
_player = JsonSerializer.Deserialize<Player>(message.Data); Player = JsonSerializer.Deserialize<Player>(message.Data);
_group = _wsService.AddPlayer(_player); Group = _wsService.AddPlayer(Player);
} }
catch (RuntimeBinderException e) catch (RuntimeBinderException e)
{ {
@ -66,20 +69,20 @@ public class ActionService : IActionService // TODO tests
throw; throw;
} }
return _group.Players; return Group.Players;
} }
public object Ready() public object Ready()
{ {
object data; 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)) if (players.All(p => p.State == State.Ready))
{ {
// TODO roll to start // TODO roll to start
_group.SetAllInGame(); Group.SetAllInGame();
data = new { AllReady = true, Players = players, Starter = _group.RandomPlayer }; data = new { AllReady = true, Players = players, Starter = Group.RandomPlayer };
} }
else else
{ {