Skip to content

Commit

Permalink
PickBan tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ipax77 committed Jun 9, 2024
1 parent 827d3a6 commit 0fff0f2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/dsstats.api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dsstats.authshared", "dssta
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "pax.dsstats.parser", "dsstats.maui\pax.dsstats.parser\pax.dsstats.parser.csproj", "{8E62721C-E4E9-4AA9-9EEF-9D4B3562607F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dsstats.pickban", "dsstats.pickban\dsstats.pickban.csproj", "{F72AB22C-9A9C-402F-AA56-B5C712C35427}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dsstats.pickban", "dsstats.pickban\dsstats.pickban.csproj", "{F72AB22C-9A9C-402F-AA56-B5C712C35427}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dsstats.pickban.tests", "tests\dsstats.pickban.tests\dsstats.pickban.tests.csproj", "{2428AFF9-03C3-463B-9F41-4B93460EDB63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -93,6 +95,10 @@ Global
{F72AB22C-9A9C-402F-AA56-B5C712C35427}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F72AB22C-9A9C-402F-AA56-B5C712C35427}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F72AB22C-9A9C-402F-AA56-B5C712C35427}.Release|Any CPU.Build.0 = Release|Any CPU
{2428AFF9-03C3-463B-9F41-4B93460EDB63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2428AFF9-03C3-463B-9F41-4B93460EDB63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2428AFF9-03C3-463B-9F41-4B93460EDB63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2428AFF9-03C3-463B-9F41-4B93460EDB63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions src/dsstats.pickban/PickBanState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ public PickBanState(Guid guid, PickBanMode mode, GameMode gameMode, int totalBan

public PickBanStateDto? SetPick(PickBan pick)
{
if (!BansPublic || PicksPublic)
{
return null;
}
lock (lockobject)
{
var statePick = Picks.FirstOrDefault(f => f.Slot == pick.Slot);
if (statePick == null)
{
pick.Locked = true;
Picks.Add(pick);
return GetDto();
}
Expand All @@ -41,11 +46,16 @@ public PickBanState(Guid guid, PickBanMode mode, GameMode gameMode, int totalBan

public PickBanStateDto? SetBan(PickBan ban)
{
if (BansPublic)
{
return null;
}
lock (lockobject)
{
var stateBan = Bans.FirstOrDefault(f => f.Slot == ban.Slot);
if (stateBan == null)
{
ban.Locked = true;
Bans.Add(ban);
return GetDto();
}
Expand Down
2 changes: 1 addition & 1 deletion src/dsstats.razorlib/PickBan/PickBanComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</div>
}
@if (pbSetting.ClientState.Picks.Count > 0
&& pbSetting.ClientState.Bans.Count == pbSetting.ClientState.TotalBans)
&& pbSetting.BansReady)
{
<h4 class="mt-2">Picks</h4>
<div class="d-flex justify-content-evenly">
Expand Down
8 changes: 8 additions & 0 deletions src/dsstats.shared/PickBan/PickBanStateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public PickBanSetting(Guid guid, PickBanMode pickBanMode, PickBanStateDto server

public void ClientPick(PickBan pickBan)
{
if (!BansReady || PicksReady)
{
return;
}
lock (lockobject)
{
pickBan.Locked = true;
Expand All @@ -98,6 +102,10 @@ public void ClientPick(PickBan pickBan)
}
public void ClientBan(PickBan pickBan)
{
if (BansReady)
{
return;
}
lock (lockobject)
{
pickBan.Locked = true;
Expand Down
59 changes: 59 additions & 0 deletions src/tests/dsstats.pickban.tests/PickBanTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using dsstats.shared;

namespace dsstats.pickban.tests
{
[TestClass]
public class PickBanTests
{
[TestMethod]
public void InitializeTest()
{
var pickBanState = new PickBanState(Guid.NewGuid(), PickBanMode.Commanders, GameMode.Commanders, 2, 6);

Assert.AreEqual(0, pickBanState.Visitors);
Assert.AreEqual(0, pickBanState.Bans.Count);
Assert.AreEqual(0, pickBanState.Picks.Count);
Assert.IsFalse(pickBanState.BansPublic);
Assert.IsFalse(pickBanState.PicksPublic);
}

[TestMethod]
public void SetBan_AddsBanCorrectly()
{
// Arrange
var pickBanState = new PickBanState(Guid.NewGuid(), PickBanMode.Commanders, GameMode.Commanders, 2, 6);
var ban = new PickBan { Slot = 1, Commander = Commander.Abathur, Locked = false };

// Act
var dto = pickBanState.SetBan(ban);

// Assert
Assert.IsNotNull(dto);
Assert.AreEqual(1, pickBanState.Bans.Count);
Assert.AreEqual(ban.Slot, pickBanState.Bans[0].Slot);
Assert.AreEqual(ban.Commander, pickBanState.Bans[0].Commander);
Assert.AreEqual(ban.Locked, pickBanState.Bans[0].Locked);
}

[TestMethod]
public void SetPick_AddsPickCorrectly()
{
// Arrange
var pickBanState = new PickBanState(Guid.NewGuid(), PickBanMode.Commanders, GameMode.Commanders, 2, 6);
pickBanState.SetBan(new PickBan { Slot = 1, Commander = Commander.Abathur, Locked = true });
pickBanState.SetBan(new PickBan { Slot = 2, Commander = Commander.Alarak, Locked = true });
pickBanState.SetBan(new PickBan { Slot = 3, Commander = Commander.Artanis, Locked = true });
var pick = new PickBan { Slot = 1, Commander = Commander.Stukov, Locked = true };

// Act
var dto = pickBanState.SetPick(pick);

// Assert
Assert.IsNotNull(dto);
Assert.AreEqual(1, pickBanState.Picks.Count);
Assert.AreEqual(pick.Slot, pickBanState.Picks[0].Slot);
Assert.AreEqual(pick.Commander, pickBanState.Picks[0].Commander);
Assert.AreEqual(pick.Locked, pickBanState.Picks[0].Locked);
}
}
}
27 changes: 27 additions & 0 deletions src/tests/dsstats.pickban.tests/dsstats.pickban.tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\dsstats.pickban\dsstats.pickban.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>

0 comments on commit 0fff0f2

Please sign in to comment.