From 31196886fdb96ba7bb3ab20be38604f5a2dc1001 Mon Sep 17 00:00:00 2001 From: martin Date: Sun, 9 Jul 2023 19:58:32 +0200 Subject: [PATCH] Tests, finished for extensions --- BackendTests/BackendTests.csproj | 23 ++++++ .../Controllers/GameControllerTests.cs | 6 ++ BackendTests/Game/Items/DiceCupTests.cs | 6 ++ BackendTests/Game/Items/DiceTests.cs | 6 ++ BackendTests/Services/ActionServiceTests.cs | 74 +++++++++++++++++++ BackendTests/Services/GameGroupTests.cs | 3 + .../Services/WebSocketServiceTests.cs | 3 + BackendTests/Usings.cs | 1 + BackendTests/Utils/ExtensionsTests.cs | 64 ++++++++++++++++ pac-man-board-game.sln | 6 ++ pac-man-board-game.sln.DotSettings.user | 7 ++ pac-man-board-game/Services/ActionService.cs | 2 +- 12 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 BackendTests/BackendTests.csproj create mode 100644 BackendTests/Controllers/GameControllerTests.cs create mode 100644 BackendTests/Game/Items/DiceCupTests.cs create mode 100644 BackendTests/Game/Items/DiceTests.cs create mode 100644 BackendTests/Services/ActionServiceTests.cs create mode 100644 BackendTests/Services/GameGroupTests.cs create mode 100644 BackendTests/Services/WebSocketServiceTests.cs create mode 100644 BackendTests/Usings.cs create mode 100644 BackendTests/Utils/ExtensionsTests.cs create mode 100644 pac-man-board-game.sln.DotSettings.user diff --git a/BackendTests/BackendTests.csproj b/BackendTests/BackendTests.csproj new file mode 100644 index 0000000..9a53779 --- /dev/null +++ b/BackendTests/BackendTests.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/BackendTests/Controllers/GameControllerTests.cs b/BackendTests/Controllers/GameControllerTests.cs new file mode 100644 index 0000000..167c3f0 --- /dev/null +++ b/BackendTests/Controllers/GameControllerTests.cs @@ -0,0 +1,6 @@ +namespace BackendTests.Controllers; + +public class GameControllerTests +{ + +} diff --git a/BackendTests/Game/Items/DiceCupTests.cs b/BackendTests/Game/Items/DiceCupTests.cs new file mode 100644 index 0000000..9f6bb2b --- /dev/null +++ b/BackendTests/Game/Items/DiceCupTests.cs @@ -0,0 +1,6 @@ +namespace BackendTests.Game.Items; + +public class DiceCupTests +{ + +} diff --git a/BackendTests/Game/Items/DiceTests.cs b/BackendTests/Game/Items/DiceTests.cs new file mode 100644 index 0000000..9aafb1b --- /dev/null +++ b/BackendTests/Game/Items/DiceTests.cs @@ -0,0 +1,6 @@ +namespace BackendTests.Game.Items; + +public class DiceTests +{ + +} diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs new file mode 100644 index 0000000..fe97d3b --- /dev/null +++ b/BackendTests/Services/ActionServiceTests.cs @@ -0,0 +1,74 @@ +namespace BackendTests.Services; + +public class ActionServiceTests +{ + #region RollDice() + + [Test] + public void RollDice_ReturnsListOfIntegers() + { + Assert.Fail(); + } + + #endregion + + #region PlayerInfo(ActionMessage message) + + [Test] + public void PlayerInfo_DataIsNull() + { + Assert.Fail(); + } + + [Test] + public void PlayerInfo_DataIsNotPlayer() + { + Assert.Fail(); + } + + [Test] + public void PlayerInfo_DataIsPlayer() + { + Assert.Fail(); + } + + #endregion + + #region DoAction(ActionMessage message) + + [Test] + public void DoAction_NegativeAction() + { + Assert.Fail(); + } + + [Test] + public void DoAction_OutOfBoundsAction() + { + Assert.Fail(); + } + + #endregion + + #region Ready() + + [Test] + public void Ready_PlayerIsNull() + { + Assert.Fail(); + } + + [Test] + public void Ready_SomeReady() + { + Assert.Fail(); + } + + [Test] + public void Ready_AllReady() + { + Assert.Fail(); + } + + #endregion +} diff --git a/BackendTests/Services/GameGroupTests.cs b/BackendTests/Services/GameGroupTests.cs new file mode 100644 index 0000000..3c911f5 --- /dev/null +++ b/BackendTests/Services/GameGroupTests.cs @@ -0,0 +1,3 @@ +namespace BackendTests.Services; + +public class GameGroupTests { } diff --git a/BackendTests/Services/WebSocketServiceTests.cs b/BackendTests/Services/WebSocketServiceTests.cs new file mode 100644 index 0000000..59b9e4c --- /dev/null +++ b/BackendTests/Services/WebSocketServiceTests.cs @@ -0,0 +1,3 @@ +namespace BackendTests.Services; + +public class WebSocketServiceTests { } diff --git a/BackendTests/Usings.cs b/BackendTests/Usings.cs new file mode 100644 index 0000000..3244567 --- /dev/null +++ b/BackendTests/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; diff --git a/BackendTests/Utils/ExtensionsTests.cs b/BackendTests/Utils/ExtensionsTests.cs new file mode 100644 index 0000000..86c5723 --- /dev/null +++ b/BackendTests/Utils/ExtensionsTests.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using pacMan.Utils; + +namespace BackendTests.Utils; + +public class ExtensionsTests +{ + #region ToArraySegment(this object obj) + + [Test] + public void ToArraySegmentValidObject() + { + var obj = new { Test = "test" }; + var segment = obj.ToArraySegment(); + + Assert.That(JsonSerializer.Serialize(obj), Has.Length.EqualTo(segment.Count)); + } + + [Test] + public void ToArraySegmentNullableObject() + { + string? s = null; + var segment = s!.ToArraySegment(); // Segment contains: null + Assert.That(segment, Has.Count.EqualTo(4)); + } + + #endregion + + #region GetString(this byte[] bytes, int length) + + private byte[] _bytes = null!; + + [SetUp] + public void Setup() + { + _bytes = "Hello World!"u8.ToArray(); + } + + [Test] + public void GetString_ValidByteArray() + { + Assert.That(_bytes.GetString(_bytes.Length), Is.EqualTo("Hello World!")); + } + + [Test] + public void GetString_EmptyByteArray() + { + Assert.That(new byte[] { }.GetString(0), Is.EqualTo("")); + } + + [Test] + public void GetString_NegativeLength() + { + Assert.Throws(typeof(ArgumentOutOfRangeException), () => _bytes.GetString(-1)); + } + + [Test] + public void GetString_LengthGreaterThanByteArrayLength() + { + Assert.Throws(typeof(ArgumentOutOfRangeException), () => _bytes.GetString(_bytes.Length + 1)); + } + + #endregion +} diff --git a/pac-man-board-game.sln b/pac-man-board-game.sln index daec71c..98deafa 100644 --- a/pac-man-board-game.sln +++ b/pac-man-board-game.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pac-man-board-game", "pac-man-board-game\pac-man-board-game.csproj", "{60072632-A16F-4007-8A97-AC74B7E6703B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BackendTests", "BackendTests\BackendTests.csproj", "{35336347-32EB-4764-A28E-3F8FF6CA54C4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {60072632-A16F-4007-8A97-AC74B7E6703B}.Debug|Any CPU.Build.0 = Debug|Any CPU {60072632-A16F-4007-8A97-AC74B7E6703B}.Release|Any CPU.ActiveCfg = Release|Any CPU {60072632-A16F-4007-8A97-AC74B7E6703B}.Release|Any CPU.Build.0 = Release|Any CPU + {35336347-32EB-4764-A28E-3F8FF6CA54C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35336347-32EB-4764-A28E-3F8FF6CA54C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35336347-32EB-4764-A28E-3F8FF6CA54C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35336347-32EB-4764-A28E-3F8FF6CA54C4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/pac-man-board-game.sln.DotSettings.user b/pac-man-board-game.sln.DotSettings.user new file mode 100644 index 0000000..dcbe518 --- /dev/null +++ b/pac-man-board-game.sln.DotSettings.user @@ -0,0 +1,7 @@ + + <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> +</SessionState> \ No newline at end of file diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index f1661d4..b557c99 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -52,7 +52,7 @@ public class ActionService : IActionService // TODO tests public List PlayerInfo(ActionMessage message) { _player = JsonSerializer.Deserialize(message.Data); - _group = _wsService.AddPlayer(_player); // TODO missing some data? + _group = _wsService.AddPlayer(_player); return _group.Players; }