Skip to content

Commit

Permalink
feat: add test of CanStartGame and CannotStartGame
Browse files Browse the repository at this point in the history
Co-authored-by: Rick Su <[email protected]>
Co-authored-by:  miku3920 <[email protected]>
  • Loading branch information
3 people committed Apr 2, 2024
1 parent 946f8f7 commit 41ba3e5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ChineseCheckers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChineseCheckers.Web", "src\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChineseCheckers.Tests", "tests\ChineseCheckers.Tests\ChineseCheckers.Tests.csproj", "{07C57EBD-0711-4F4A-A438-18F95CE7393D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChineseCheckers.Domain", "src\ChineseCheckers.Domain\ChineseCheckers.Domain.csproj", "{50647B55-A7DC-456F-941A-51957FEEB148}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -43,6 +45,10 @@ Global
{07C57EBD-0711-4F4A-A438-18F95CE7393D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07C57EBD-0711-4F4A-A438-18F95CE7393D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07C57EBD-0711-4F4A-A438-18F95CE7393D}.Release|Any CPU.Build.0 = Release|Any CPU
{50647B55-A7DC-456F-941A-51957FEEB148}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50647B55-A7DC-456F-941A-51957FEEB148}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50647B55-A7DC-456F-941A-51957FEEB148}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50647B55-A7DC-456F-941A-51957FEEB148}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -53,5 +59,9 @@ Global
{55A8D52B-432B-477F-84F7-3C2565B922EC} = {072D3C5D-A113-45B6-86D6-EF5C54ECA984}
{0D1D26B1-A598-4942-B009-847E26A42A67} = {072D3C5D-A113-45B6-86D6-EF5C54ECA984}
{07C57EBD-0711-4F4A-A438-18F95CE7393D} = {4370858E-DFB5-4590-AE4D-270098CCAB79}
{50647B55-A7DC-456F-941A-51957FEEB148} = {072D3C5D-A113-45B6-86D6-EF5C54ECA984}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {613C01CF-175B-4931-9732-C7B2185B03B2}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions src/ChineseCheckers.Domain/ChineseCheckers.Domain.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Compile Remove="Class1.cs" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/ChineseCheckers.Domain/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ChineseCheckers.Domain;

public class Game
{
public bool IsStarted { get; private set; }
private readonly List<Player> _players = [];

public Game()
{
}

public void AddPlayer(Player player)
{
_players.Add(player);
}

public void Start()
{
// 在這裡實作遊戲開始的邏輯
// 例如,初始化遊戲板、發放棋子等等
IsStarted = _players.Count is 2 or 3 or 4 or 6;

if (IsStarted == false)
{
// TODO: throw domain exception
}
}
}
6 changes: 6 additions & 0 deletions src/ChineseCheckers.Domain/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ChineseCheckers.Domain;

public class Player(string name)
{
public string Name { get; private set; } = name;
}
1 change: 1 addition & 0 deletions tests/ChineseCheckers.Tests/ChineseCheckers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\ChineseCheckers.ApiService\ChineseCheckers.ApiService.csproj" />
<ProjectReference Include="..\..\src\ChineseCheckers.Domain\ChineseCheckers.Domain.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
59 changes: 59 additions & 0 deletions tests/ChineseCheckers.Tests/StartGameTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using ChineseCheckers.Domain;

namespace ChineseCheckers.Tests;

[TestClass]
public class StartGameTest
{
[Description("""
Given: 2 players
When: Start game
Then: Game is started
""")]
[TestMethod]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
[DataRow(6)]
public void CanStartGame(int playerCount)
{
// Arrange
var game = new Game();
for (var i = 0; i < playerCount; i++)
{
game.AddPlayer(new Player($"Player {i}"));
}

// Act
game.Start();

// Assert
Assert.IsTrue(game.IsStarted);
}


[Description("""
Given: 1 player
When: Start game
Then: Game is not started
""")]
[TestMethod]
[DataRow(1)]
[DataRow(5)]
[DataRow(7)]
public void CannotStartGame(int playerCount)
{
// Arrange
var game = new Game();
for (var i = 0; i < playerCount; i++)
{
game.AddPlayer(new Player($"Player {i}"));
}

// Act
game.Start();

// Assert
Assert.IsFalse(game.IsStarted);
}
}

0 comments on commit 41ba3e5

Please sign in to comment.