Split up methods in controllers, added tests for GameService, refactored GameGroup name to Game and directory Game to GameStuff

This commit is contained in:
Martin Berg Alstad
2023-07-18 17:09:27 +02:00
parent fab3e3d13f
commit c7bc473572
25 changed files with 243 additions and 164 deletions

View File

@ -1,8 +1,7 @@
using System.Net.WebSockets;
using BackendTests.TestUtils;
using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.Game;
using pacMan.Interfaces;
using pacMan.Services;
using pacMan.Utils;
@ -10,25 +9,12 @@ namespace BackendTests.Services;
public class WebSocketServiceTests
{
private readonly DirectionalPosition _spawn3By3Up = new()
{ At = new Position { X = 3, Y = 3 }, Direction = Direction.Up };
private GameService _service = null!;
private Queue<DirectionalPosition> _spawns = null!;
private IWebSocketService _service = null!;
[SetUp]
public void SetUp()
{
_service = new GameService(Substitute.For<ILogger<GameService>>());
_spawns = new Queue<DirectionalPosition>(new[]
{
_spawn3By3Up,
new DirectionalPosition { At = new Position { X = 7, Y = 7 }, Direction = Direction.Down },
new DirectionalPosition { At = new Position { X = 7, Y = 7 }, Direction = Direction.Down },
new DirectionalPosition { At = new Position { X = 7, Y = 7 }, Direction = Direction.Down }
});
_service = new WebSocketService(Substitute.For<ILogger<WebSocketService>>());
}
#region Send(Websocket, ArraySegment<byte>)
@ -140,44 +126,4 @@ public class WebSocketServiceTests
}
#endregion
#region AddPlayer(IPlayer)
[Test]
public void AddPlayer_ToEmptyGroup()
{
var player = Players.Create("white");
var group = _service.AddPlayer(player, _spawns);
Assert.Multiple(() =>
{
Assert.That(group.Players, Has.Count.EqualTo(1));
Assert.That(group.NextPlayer, Is.EqualTo(player));
Assert.That(_service.Games, Has.Count.EqualTo(1));
});
}
[Test]
public void AddPlayer_ToFullGroup()
{
for (var i = 0; i < 4; i++)
{
var player = Players.Create(i.ToString());
_service.AddPlayer(player, _spawns);
}
var player5 = Players.Create("white");
var group = _service.AddPlayer(player5, new Queue<DirectionalPosition>(new[] { _spawn3By3Up }));
Assert.Multiple(() =>
{
Assert.That(group.Players, Has.Count.EqualTo(1));
Assert.That(group.NextPlayer, Is.EqualTo(player5));
Assert.That(_service.Games, Has.Count.EqualTo(2));
Assert.That(_service.Games.First(), Has.Count.EqualTo(Rules.MaxPlayers));
});
}
#endregion
}