GameGroup tests
This commit is contained in:
parent
b2f9573fd5
commit
5db0af5841
@ -1,3 +1,218 @@
|
||||
using pacMan.Exceptions;
|
||||
using pacMan.Game;
|
||||
using pacMan.Game.Interfaces;
|
||||
using pacMan.Game.Items;
|
||||
using pacMan.Services;
|
||||
using pacMan.Utils;
|
||||
|
||||
namespace BackendTests.Services;
|
||||
|
||||
public class GameGroupTests { }
|
||||
public class GameGroupTests
|
||||
{
|
||||
private IPlayer _bluePlayer = null!;
|
||||
private GameGroup _gameGroup = null!;
|
||||
private IPlayer _greenPlayer = null!;
|
||||
private IPlayer _purplePlayer = null!;
|
||||
private IPlayer _redPlayer = null!;
|
||||
private IPlayer _yellowPlayer = null!;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_gameGroup = new GameGroup();
|
||||
const string redColour = "red";
|
||||
_redPlayer = new Player
|
||||
{
|
||||
Name = "Red",
|
||||
Colour = redColour,
|
||||
PacMan = CreatePacMan(redColour),
|
||||
Box = CreateBox(redColour)
|
||||
};
|
||||
const string blueColour = "blue";
|
||||
_bluePlayer = new Player
|
||||
{
|
||||
Name = "Blue",
|
||||
Colour = blueColour,
|
||||
PacMan = CreatePacMan(blueColour),
|
||||
Box = CreateBox(blueColour)
|
||||
};
|
||||
const string yellowColour = "yellow";
|
||||
_yellowPlayer = new Player
|
||||
{
|
||||
Name = "Yellow",
|
||||
Colour = yellowColour,
|
||||
PacMan = CreatePacMan(yellowColour),
|
||||
Box = CreateBox(yellowColour)
|
||||
};
|
||||
const string greenColour = "green";
|
||||
_greenPlayer = new Player
|
||||
{
|
||||
Name = "Green",
|
||||
Colour = greenColour,
|
||||
PacMan = CreatePacMan(greenColour),
|
||||
Box = CreateBox(greenColour)
|
||||
};
|
||||
const string purpleColour = "purple";
|
||||
_purplePlayer = new Player
|
||||
{
|
||||
Name = "Purple",
|
||||
Colour = purpleColour,
|
||||
PacMan = CreatePacMan(purpleColour),
|
||||
Box = CreateBox(purpleColour)
|
||||
};
|
||||
}
|
||||
|
||||
private static Character CreatePacMan(string colour) =>
|
||||
new()
|
||||
{
|
||||
Colour = colour,
|
||||
IsEatable = true,
|
||||
Type = CharacterType.PacMan
|
||||
};
|
||||
|
||||
private static Box CreateBox(string colour) =>
|
||||
new()
|
||||
{
|
||||
Colour = colour,
|
||||
Pellets = new List<Pellet>()
|
||||
};
|
||||
|
||||
private static IPlayer Clone(IPlayer player) =>
|
||||
new Player
|
||||
{
|
||||
Box = player.Box,
|
||||
Colour = player.Colour,
|
||||
Name = player.Name,
|
||||
PacMan = player.PacMan
|
||||
};
|
||||
|
||||
private void AddFullParty()
|
||||
{
|
||||
_gameGroup.AddPlayer(_bluePlayer);
|
||||
_gameGroup.AddPlayer(_redPlayer);
|
||||
_gameGroup.AddPlayer(_yellowPlayer);
|
||||
_gameGroup.AddPlayer(_greenPlayer);
|
||||
}
|
||||
|
||||
#region AddPlayer(IPlayer player)
|
||||
|
||||
[Test]
|
||||
public void AddPlayer_WhenEmpty()
|
||||
{
|
||||
var added = _gameGroup.AddPlayer(_redPlayer);
|
||||
Assert.That(added, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddPlayer_WhenFull() =>
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
AddFullParty();
|
||||
Assert.That(_gameGroup.Players.Count, Is.EqualTo(Rules.MaxPlayers));
|
||||
Assert.That(_gameGroup.AddPlayer(_purplePlayer), Is.False);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void AddPlayer_WhenNameExists()
|
||||
{
|
||||
var redClone = Clone(_redPlayer);
|
||||
_gameGroup.AddPlayer(_redPlayer);
|
||||
var added = _gameGroup.AddPlayer(redClone);
|
||||
Assert.That(added, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddPlayer_WhenStateIsNotWaitingForPlayers()
|
||||
{
|
||||
_redPlayer.State = State.InGame;
|
||||
_gameGroup.AddPlayer(_redPlayer);
|
||||
Assert.That(_redPlayer.State, Is.EqualTo(State.WaitingForPlayers));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sendtoall(ArraySegment<byte> segment)
|
||||
|
||||
[Test]
|
||||
public void SendToAll_WhenConnectionsIsNull()
|
||||
{
|
||||
Assert.DoesNotThrow(() => _gameGroup.SendToAll(new { }.ToArraySegment()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendToAll_WhenConnectionsIsNotNull()
|
||||
{
|
||||
var counter = 0;
|
||||
async Task Send(ArraySegment<byte> segment) => await Task.Run(() => counter++);
|
||||
|
||||
_gameGroup.Connections += Send;
|
||||
_gameGroup.Connections += Send;
|
||||
|
||||
_gameGroup.SendToAll(new { }.ToArraySegment());
|
||||
|
||||
// TODO timeout after n amount of time
|
||||
while (counter < 2) { }
|
||||
|
||||
Assert.That(counter, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetReady(IPlayer player)
|
||||
|
||||
[Test]
|
||||
public void SetReady_ReturnsAllPlayers()
|
||||
{
|
||||
_gameGroup.AddPlayer(_redPlayer);
|
||||
_gameGroup.AddPlayer(_bluePlayer);
|
||||
|
||||
var players = _gameGroup.SetReady(_redPlayer).ToList();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(players, Has.Count.EqualTo(2));
|
||||
Assert.That(players, Does.Contain(_redPlayer));
|
||||
Assert.That(players, Does.Contain(_bluePlayer));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetReady_SetsStateToReady()
|
||||
{
|
||||
_gameGroup.AddPlayer(_redPlayer);
|
||||
|
||||
Assert.That(_redPlayer.State, Is.Not.EqualTo(State.Ready));
|
||||
|
||||
_gameGroup.SetReady(_redPlayer);
|
||||
|
||||
Assert.That(_redPlayer.State, Is.EqualTo(State.Ready));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetReady_WhenPlayerIsNotInPlayers()
|
||||
{
|
||||
Assert.Throws<PlayerNotFoundException>(() => _gameGroup.SetReady(_redPlayer));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetAllIngame()
|
||||
|
||||
[Test]
|
||||
public void SetAllInGame_SetsStateToInGame()
|
||||
{
|
||||
AddFullParty();
|
||||
Assert.That(_gameGroup.Players, Has.All.Property(nameof(IPlayer.State)).Not.EqualTo(State.Ready));
|
||||
_gameGroup.SetAllInGame();
|
||||
Assert.That(_gameGroup.Players, Has.All.Property(nameof(IPlayer.State)).EqualTo(State.InGame));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetAllInGame_WhenPlayersIsEmpty()
|
||||
{
|
||||
_gameGroup.SetAllInGame();
|
||||
Assert.That(_gameGroup.Players, Is.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c5faef1f_002D6857_002D480d_002D9cae_002Da308bdf09365/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="GetStringValidByteArray" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>NUnit3x::35336347-32EB-4764-A28E-3F8FF6CA54C4::net7.0::BackendTests.Utils.ExtensionsTests</TestId>
|
||||
<TestId>NUnit3x::35336347-32EB-4764-A28E-3F8FF6CA54C4::net7.0::BackendTests.Services.ActionServiceTests</TestId>
|
||||
</TestAncestor>
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c5faef1f_002D6857_002D480d_002D9cae_002Da308bdf09365/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="All backend tests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<Project Location="/home/martin/git/Csharp/pac-man-board-game/BackendTests" Presentation="&lt;BackendTests&gt;" />
|
||||
</SessionState></s:String></wpf:ResourceDictionary>
|
6
pac-man-board-game/Exceptions/PlayerNotFoundException.cs
Normal file
6
pac-man-board-game/Exceptions/PlayerNotFoundException.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace pacMan.Exceptions;
|
||||
|
||||
public class PlayerNotFoundException : Exception
|
||||
{
|
||||
public PlayerNotFoundException(string? message = "Player not found") : base(message) { }
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using pacMan.Exceptions;
|
||||
using pacMan.Game;
|
||||
using pacMan.Game.Interfaces;
|
||||
|
||||
@ -25,6 +26,8 @@ public class GameGroup // TODO tests
|
||||
|
||||
public IEnumerable<IPlayer> SetReady(IPlayer player)
|
||||
{
|
||||
if (!Players.Contains(player))
|
||||
throw new PlayerNotFoundException("The player was not found in the game group.");
|
||||
player.State = State.Ready;
|
||||
return Players;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user