Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotnet8 and csharp12 #115

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions BackendTests/BackendTests.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NUnit" Version="4.0.1"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\pac-man-board-game\pac-man-board-game.csproj" />
<ProjectReference Include="..\pac-man-board-game\pac-man-board-game.csproj"/>
</ItemGroup>

</Project>
28 changes: 24 additions & 4 deletions BackendTests/Controllers/GameControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

namespace BackendTests.Controllers;

[TestFixture]
[TestOf(nameof(GameController))]
public class GameControllerTests
{
private IActionService _actionService = null!;
private GameController _controller = null!;
private GameService _gameService = null!;

[SetUp]
public void Setup()
{
Expand All @@ -27,6 +25,10 @@ public void Setup()
_controller = new GameController(Substitute.For<ILogger<GameController>>(), _gameService, _actionService);
}

private IActionService _actionService = null!;
private GameController _controller = null!;
private GameService _gameService = null!;

[Test]
public void Run_ReturnsSame()
{
Expand All @@ -51,4 +53,22 @@ public void Run_ReturnsSame()
else
Assert.Fail("Result is not an ArraySegment<byte>");
}

[Test]
public void DoAction_NegativeAction()
{
const string data = "Nothing happens";
var message = new ActionMessage { Action = (GameAction)(-1), Data = data };
_controller.DoAction(message);
Assert.That(message.Data, Is.EqualTo(data));
}

[Test]
public void DoAction_OutOfBoundsAction()
{
const string data = "Nothing happens";
var message = new ActionMessage { Action = (GameAction)100, Data = data };
_controller.DoAction(message);
Assert.That(message.Data, Is.EqualTo(data));
}
}
4 changes: 4 additions & 0 deletions BackendTests/Controllers/PlayerControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using pacMan.Controllers;

namespace BackendTests.Controllers;

[TestFixture]
[TestOf(nameof(PlayerController))]
public class PlayerControllerTests
{
// TODO
Expand Down
2 changes: 2 additions & 0 deletions BackendTests/Game/Items/DiceCupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BackendTests.Game.Items;

[TestFixture]
[TestOf(nameof(DiceCup))]
public class DiceCupTests
{
[Test]
Expand Down
2 changes: 2 additions & 0 deletions BackendTests/Game/Items/DiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BackendTests.Game.Items;

[TestFixture]
[TestOf(nameof(Dice))]
public class DiceTests
{
[Test]
Expand Down
67 changes: 15 additions & 52 deletions BackendTests/Services/ActionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,17 @@
using BackendTests.TestUtils;
using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.DTOs;
using pacMan.Exceptions;
using pacMan.GameStuff;
using pacMan.GameStuff.Items;
using pacMan.Services;

namespace BackendTests.Services;

[TestFixture, TestOf(nameof(ActionService))]
public class ActionServiceTests
{
private readonly Player _blackPlayer = Players.Create("black");
private readonly Player _redPlayer = Players.Create("red");
private readonly Player _whitePlayer = Players.Create("white");
private ActionMessage _blackMessage = null!;
private pacMan.Services.Game _game = null!;
private GameService _gameService = null!;
private ActionMessage _redMessage = null!;
private IActionService _service = null!;

private Queue<DirectionalPosition> _spawns = null!;
private ActionMessage _whiteMessage = null!;


[SetUp]
public void Setup()
{
Expand All @@ -36,6 +25,18 @@ public void Setup()
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _gameService);
}

private readonly Player _blackPlayer = Players.Create("black");
private readonly Player _redPlayer = Players.Create("red");
private readonly Player _whitePlayer = Players.Create("white");
private ActionMessage _blackMessage = null!;
private pacMan.Services.Game _game = null!;
private GameService _gameService = null!;
private ActionMessage _redMessage = null!;
private IActionService _service = null!;

private Queue<DirectionalPosition> _spawns = null!;
private ActionMessage _whiteMessage = null!;

private JsonElement SerializeData(string username) =>
JsonDocument.Parse(JsonSerializer.Serialize(
new JoinGameData { Username = username, GameId = _game.Id }
Expand All @@ -50,8 +51,6 @@ private static Queue<DirectionalPosition> CreateQueue() =>
new() { At = new Position { X = 9, Y = 9 }, Direction = Direction.Right }
});

#region RollDice()

[Test]
public void RollDice_ReturnsListOfIntegers()
{
Expand All @@ -64,10 +63,6 @@ public void RollDice_ReturnsListOfIntegers()
});
}

#endregion

#region PlayerInfo(ActionMessage message)

[Test]
public void PlayerInfo_DataIsNull()
{
Expand Down Expand Up @@ -97,32 +92,6 @@ public void PlayerInfo_DataIsUsernameAndGameId()
Assert.That(new List<Player> { _whitePlayer }, Is.EqualTo(players));
}

#endregion

#region DoAction(ActionMessage message)

[Test]
public void DoAction_NegativeAction()
{
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()
{
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

#region Ready()

[Test]
public void Ready_PlayerIsNull()
{
Expand Down Expand Up @@ -183,15 +152,11 @@ public void Ready_TwoReady()
Is.EqualTo(_blackPlayer.Username).Or.EqualTo(_whitePlayer.Username));
}

#endregion

#region FindNextPlayer()

[Test]
public void FindNextPlayer_NoPlayers()
{
_service.Game = new pacMan.Services.Game(new Queue<DirectionalPosition>());
Assert.Throws<InvalidOperationException>(() => _service.FindNextPlayer());
Assert.Throws<PlayerNotFoundException>(() => _service.FindNextPlayer());
}

[Test]
Expand Down Expand Up @@ -221,6 +186,4 @@ public void FindNextPlayer_TwoPlayers()
var second = _service.FindNextPlayer();
Assert.That(second, Is.EqualTo(_whitePlayer.Username));
}

#endregion
}
25 changes: 9 additions & 16 deletions BackendTests/Services/GameServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
using NSubstitute;
using pacMan.Exceptions;
using pacMan.GameStuff;
using pacMan.GameStuff.Items;
using pacMan.Services;

namespace BackendTests.Services;

[TestFixture]
[TestOf(nameof(GameService))]
public class GameServiceTests
{
private readonly DirectionalPosition _spawn3By3Up = new()
{ At = new Position { X = 3, Y = 3 }, Direction = Direction.Up };

private GameService _service = null!;
private Queue<DirectionalPosition> _spawns = null!;

[SetUp]
public void SetUp()
{
Expand All @@ -29,7 +24,11 @@ public void SetUp()
});
}

#region CreateAndJoin(IPlayer player, Queue<DirectionalPosition> spawns)
private readonly DirectionalPosition _spawn3By3Up = new()
{ At = new Position { X = 3, Y = 3 }, Direction = Direction.Up };

private GameService _service = null!;
private Queue<DirectionalPosition> _spawns = null!;

[Test]
public void CreateAndJoin_WhenEmpty()
Expand All @@ -53,15 +52,11 @@ public void CreateAndJoin_SpawnsAreNotEqualToMaxPlayers()
Assert.Throws<ArgumentException>(() => _service.CreateAndJoin(player, _spawns));
}

#endregion

#region JoinbyId(Guid id)

[Test]
public void JoinById_WhenIdNotExists()
{
var player = Players.Create("white");
_service.Games.Add(new pacMan.Services.Game(_spawns) { Players = new List<Player> { player } });
_service.Games.Add(new pacMan.Services.Game(_spawns) { Players = [player] });

Assert.Throws<GameNotFoundException>(() => _service.JoinById(Guid.NewGuid(), player));
}
Expand All @@ -70,7 +65,7 @@ public void JoinById_WhenIdNotExists()
public void JoinById_WhenIdExists()
{
var player = Players.Create("white");
var game = new pacMan.Services.Game(_spawns) { Players = new List<Player> { player } };
var game = new pacMan.Services.Game(_spawns) { Players = [player] };
_service.Games.Add(game);


Expand All @@ -84,6 +79,4 @@ public void JoinById_WhenIdExists()
Assert.That(_service.Games, Has.Count.EqualTo(1));
});
}

#endregion
}
Loading