Compare commits

...

57 Commits

Author SHA1 Message Date
mzjeeawody 13f45645fe maj_local
continuous-integration/drone/push Build is failing Details
2 years ago
Ismail TAHA JANAN d95ee1ad2e Merge pull request 'doxygen' (#202) from doxygen into main
continuous-integration/drone/push Build is passing Details
2 years ago
mzjeeawody b73d88f983 change branch master yml
2 years ago
mzjeeawody c19f7398f6 output directory
continuous-integration/drone/push Build is passing Details
2 years ago
mzjeeawody 142f902ed9 correct yml
continuous-integration/drone/push Build is passing Details
2 years ago
mzjeeawody 4cf65f7bbd test local doxygen
continuous-integration/drone/push Build is passing Details
2 years ago
mzjeeawody 2e7974f29d doxygen
continuous-integration/drone/push Build is passing Details
2 years ago
Mohammad Zafir JEEAWODY 4b240195e9 Mise à jour de '.drone.yml'
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 89c98fef2c Merge pull request '🐛 Replace Equals with SequenceEqual for a collection' (#200) from apply-fixes-following-interview into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai c61ffeb783 🐛 Replace Equals with SequenceEqual
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 72ff04f845 Merge pull request '🚨 Encapsulate collections...' (#199) from apply-fixes-following-interview into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai d259884ba8 🚨 Encapsulate collections...
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 7d1f973153 Merge pull request ' Fix #176' (#195) from ut-turnextensions into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 9c0edea60b Finish unit testing TurnExtensions
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai a2d832a9f2 🚧 WIP
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 308ef1a5b1 ✏️
2 years ago
Alexis Drai c4c2a993c6 🔥 ♻️
2 years ago
Alexis Drai 42ab467bfb ✏️ Fix typo
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 7c6b3ee663 Merge pull request '🔊 Implement logger' (#192) from implement-logger into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 6de4e96803 🔧
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 932babdca6 🔥 📈 Get rid of ToString() methods
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 5c11e4b253 🔊 Use NLog in App.Program and Data.Players.PlayerDbManager
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai ef1a6966ea Merge pull request '🗃️ Add TurnEntity (many to many)' (#191) from add-turnentities into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 865947fa9c 📈
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 5694baf4cd Unit test Utils.Enumerables
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 2ef6b304dc 📈 Slither over the 80% line
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai cce587437e Test TurnEntity
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 1bf9d8394f 🚧 Redefine Equals and Hash in dice and faces
2 years ago
Alexis Drai 27e90bfbd5 🚚 Move Point to Tests root
2 years ago
Alexis Drai 32a58dd414 🐛 Initialize empty collections, debug die creation
2 years ago
Alexis Drai 0a01b402c0 🗃️ Add TurnEntity and implement many-to-many
2 years ago
Alexis Drai bf22c44740 ♻️ Rename Player to PlayerEntity in EF project for clarity
2 years ago
Alexis Drai 7fd6078c25 ♻️ Use inheritance, use properties for FK
2 years ago
Alexis Drai c7aac52f48 🔀 main --> this
2 years ago
Alexis Drai c3f7194a34 Merge pull request '🚑 Fix EF issue by using entities' (#190) from ef-hotfix into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai ca7c4a796f 🚑
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai e6ba5f150e 🚧 WIP
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 3a49fe0e88 Merge pull request '🐛 ♻️ Fix up dice and faces (EF)' (#189) from fixup-dice-entities into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 2c44df5e16 ♻️ Use extension features
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai b760b6f905 🐛
2 years ago
Alexis Drai f20adbad17 Merge pull request 'EF_Dice_Faces' (#188) from EF_Dice_Faces into main
continuous-integration/drone/push Build is passing Details
2 years ago
Ismail TAHA JANAN 6128f57dcc Merge branch 'main' into EF_Dice_Faces
continuous-integration/drone/push Build is passing Details
2 years ago
Ismail TAHA JANAN 48c130faf5 add entities and extentions to dice and faces
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai c623776e4e ✏️ Fix typo
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 84f52c3dce Merge pull request 'use-async-in-playerdb' (#186) from use-async-in-playerdb into main
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai e5fc62499f 📈 🔥 Remove unused and untestable code
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai cd2df0ba86 Update and correct UTs
2 years ago
Alexis Drai a319d7deb2 🚨 fix code smells
2 years ago
Alexis Drai a46d4b73a4 ⚗️ Play around with async-await
2 years ago
Alexis Drai 9ea198b9fa 📝 Update README
continuous-integration/drone/push Build is passing Details
2 years ago
Ismail TAHA JANAN 423965361b no sence code
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 4574e48203 Merge pull request ' dice and faces tests implementd' (#185) from TU_Dice_Faces into main
continuous-integration/drone/push Build is passing Details
2 years ago
Ismail TAHA JANAN 963ac99001 dice and faces tests implementd
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis DRAI 6c460efa63 ✏️ Fix typo in class name
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai ac5ebd7b87 ✏️ Fix comment
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai d4a90f917a 📝 Update README
continuous-integration/drone/push Build is passing Details
2 years ago
Alexis Drai 0050836358 📝 Update README
continuous-integration/drone/push Build is passing Details
2 years ago

@ -9,6 +9,9 @@ trigger:
steps:
- name: build
image: mcr.microsoft.com/dotnet/sdk:6.0
volumes:
- name: docs
path: /docs
commands:
- cd Sources/
- dotnet restore DiceApp.sln
@ -41,4 +44,25 @@ steps:
# accessible en ligne de commande par $${PLUGIN_SONAR_TOKEN}
sonar_token:
from_secret: SECRET_SONAR_LOGIN
depends_on: [tests]
depends_on: [tests]
# La documentation Doxygen doit être dans le répertoire
# Documentation/doxygen
# avec le ficher
# Documentation/doxygen/Doxyfile contenant
# OUTPUT_DIRECTORY = /docs/doxygen
- name: generate-and-deploy-docs
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-docdeployer
volumes:
- name: docs
path: /docs
commands:
- /entrypoint.sh
when:
branch:
- master
depends_on: [ build ]
volumes:
- name: docs
temp: {}

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

File diff suppressed because it is too large Load Diff

@ -0,0 +1,8 @@
<html><body>
<p>
<hr size="1"/><address style="text-align: right;"><small>Generated on $datetime with &nbsp;
<img src="CodeFirst.png" alt="Code#0" align="middle" border="0" height="40px"/>
by Doxygen version $doxygenversion</small></address>
</p>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

@ -34,11 +34,13 @@ However, you do need to create the migrations and DB (and you probably should de
First, in Visual Studio's terminal ("Developer PowerShell"), go to *DiceApp/Sources/Data*, and make sure Entity Framework is installed and / or updated.
```
cd Data
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef
```
Now the migrations and DB. Since we have a `DiceAppDbContext` *and* and `DiceAppDbContextWithStub`, you will need to specify which one to use. Make sure you are in *DiceApp/Sources/Data*.
Now the migrations and DB. Since we have a `DiceAppDbContext` *and* and `DiceAppDbContextWithStub`, you will need to specify which one to use.
```
cd Data
dotnet ef migrations add dice_app_db --context DiceAppDbContextWithStub
dotnet ef database update --context DiceAppDbContextWithStub --startup-project ../App
```
@ -50,6 +52,10 @@ You may not want to read tables in the debug window -- in which case, just downl
Ta-da.
#### Troubleshooting (VS vs .NET EF)
**If Visual Studio's embedded terminal refuses to recognize `dotnet ef`, try to fully close and reopen Visual Studio**
## To contribute (workflow)
We are using the feature branch workflow ([details here](https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow), or see the summary below)

@ -6,11 +6,19 @@
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<Content Include="NLog.config">
<BuildAction>Content</BuildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
<ItemGroup>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true">
<targets async="true">
<target name="logfile" xsi:type="File" fileName="log.txt" />
<target name="logdebug" xsi:type="Debug" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logdebug"/>
<logger name="*" minlevel="Warn" writeTo="logfile"/>
</rules>
</nlog>

@ -7,28 +7,32 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App
{
internal static class Program
{
static void Main(string[] args)
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static async Task Main(string[] args)
{
// MODEL stuff
ILoader loader = new Stub();
MasterOfCeremonies masterOfCeremonies;
try
{
masterOfCeremonies = loader.LoadApp();
masterOfCeremonies = await loader.LoadApp();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
masterOfCeremonies = new(new PlayerManager(), new DiceGroupManager(), null);
logger.Warn(ex);
masterOfCeremonies = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
}
try
@ -39,23 +43,21 @@ namespace App
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB
IEnumerable<PlayerEntity> entities = db.Players;
Debug.WriteLine("Loading players");
PlayerDbManager playerDbManager = new(db);
IEnumerable<PlayerEntity> entities = await playerDbManager.GetAll();
foreach (PlayerEntity entity in entities)
{
try
{
// persist them as models !
masterOfCeremonies.GlobalPlayerManager.Add(entity.ToModel());
Debug.WriteLine($"{entity.ID} -- {entity.Name}");
await masterOfCeremonies.GlobalPlayerManager.Add(entity.ToModel());
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
string menuChoice = "nothing";
@ -81,38 +83,38 @@ namespace App
break;
case "l":
string loadName = ChooseGame(masterOfCeremonies);
string loadName = await ChooseGame(masterOfCeremonies);
if (masterOfCeremonies.GameManager.GetOneByName(loadName) != null)
{
Play(masterOfCeremonies, loadName);
await Play(masterOfCeremonies, loadName);
}
break;
case "n":
if (!masterOfCeremonies.DiceGroupManager.GetAll().Any())
if (!(await masterOfCeremonies.DiceGroupManager.GetAll()).Any())
{
Console.WriteLine("make at least one dice group first, then try again");
break;
}
Console.WriteLine("add dice to the game");
IEnumerable<Die> newGameDice = PrepareDice(masterOfCeremonies);
IEnumerable<Die> newGameDice = await PrepareDice(masterOfCeremonies);
string newGameName;
Console.WriteLine("give this new game a name\n>");
newGameName = Console.ReadLine();
Console.WriteLine("add players to the game");
PlayerManager playerManager = PreparePlayers(masterOfCeremonies);
PlayerManager playerManager = await PreparePlayers(masterOfCeremonies);
masterOfCeremonies.StartNewGame(newGameName, playerManager, newGameDice);
Play(masterOfCeremonies, newGameName);
await masterOfCeremonies.StartNewGame(newGameName, playerManager, newGameDice);
await Play(masterOfCeremonies, newGameName);
break;
case "d":
string deleteName = ChooseGame(masterOfCeremonies);
masterOfCeremonies.GameManager.Remove(masterOfCeremonies.GameManager.GetOneByName(deleteName));
string deleteName = await ChooseGame(masterOfCeremonies);
masterOfCeremonies.GameManager.Remove(await masterOfCeremonies.GameManager.GetOneByName(deleteName));
break;
case "c":
@ -151,19 +153,19 @@ namespace App
newGroupDice.Add(die);
}
}
masterOfCeremonies.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(newGroupName, newGroupDice));
await masterOfCeremonies.DiceGroupManager.Add(new DiceGroup(newGroupName, newGroupDice));
break;
case "p":
ShowPlayers(masterOfCeremonies);
await ShowPlayers(masterOfCeremonies);
break;
case "i":
ShowDice(masterOfCeremonies);
await ShowDice(masterOfCeremonies);
break;
case "y":
PreparePlayers(masterOfCeremonies);
await PreparePlayers(masterOfCeremonies);
break;
default:
@ -178,36 +180,35 @@ namespace App
using (DiceAppDbContext db = new())
{
// get all the players from the app's memory
IEnumerable<Player> models = masterOfCeremonies.GlobalPlayerManager.GetAll();
IEnumerable<Player> models = await masterOfCeremonies.GlobalPlayerManager.GetAll();
// create a PlayerDbManager (and inject it with the DB)
PlayerDbManager playerDbManager = new(db);
Debug.WriteLine("Saving players");
foreach (Player model in models)
{
try // to persist them
{ // as entities !
PlayerEntity entity = model.ToEntity();
playerDbManager.Add(entity);
Debug.WriteLine($"{entity.ID} -- {entity.Name}");
await playerDbManager.Add(entity);
}
// what if there's already a player with that name? Exception (see PlayerEntity's annotations)
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
// flushing and closing NLog before quitting completely
NLog.LogManager.Shutdown();
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
}
private static void Play(MasterOfCeremonies masterOfCeremonies, string name)
private static async Task Play(MasterOfCeremonies masterOfCeremonies, string name)
{
string menuChoicePlay = "";
while (menuChoicePlay != "q")
{
Game game = masterOfCeremonies.GameManager.GetOneByName(name);
Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" +
Game game = await masterOfCeremonies.GameManager.GetOneByName(name);
Console.WriteLine($"{PlayerToString(await game.GetWhoPlaysNow())}'s turn\n" +
"q... quit\n" +
"h... show history\n" +
"s... save\n" +
@ -218,48 +219,45 @@ namespace App
case "q":
break;
case "h":
foreach (Turn turn in game.GetHistory())
{
Console.WriteLine(turn);
}
foreach (Turn turn in game.GetHistory()) { Console.WriteLine(TurnToString(turn)); }
break;
case "s":
masterOfCeremonies.GameManager.Add(game);
await masterOfCeremonies.GameManager.Add(game);
break;
default:
MasterOfCeremonies.PlayGame(game);
Console.WriteLine(game.GetHistory().Last());
await MasterOfCeremonies.PlayGame(game);
Console.WriteLine(TurnToString(game.GetHistory().Last()));
break;
}
}
}
private static string ChooseGame(MasterOfCeremonies masterOfCeremonies)
private static async Task<string> ChooseGame(MasterOfCeremonies masterOfCeremonies)
{
string name;
Console.WriteLine("which of these games?\n(choose by name)\n>");
foreach (Game game in masterOfCeremonies.GameManager.GetAll())
foreach (Game game in await masterOfCeremonies.GameManager.GetAll())
{
Console.WriteLine(game);
Console.WriteLine(GameToString(game));
}
name = Console.ReadLine();
return name;
}
private static void ShowPlayers(MasterOfCeremonies masterOfCeremonies)
private static async Task ShowPlayers(MasterOfCeremonies masterOfCeremonies)
{
Console.WriteLine("Look at all them players!");
foreach (Player player in masterOfCeremonies.GlobalPlayerManager.GetAll())
foreach (Player player in await masterOfCeremonies.GlobalPlayerManager.GetAll())
{
Console.WriteLine(player);
Console.WriteLine(PlayerToString(player));
}
}
private static void ShowDice(MasterOfCeremonies masterOfCeremonies)
private static async Task ShowDice(MasterOfCeremonies masterOfCeremonies)
{
foreach ((string name, IEnumerable<Die> dice) in masterOfCeremonies.DiceGroupManager.GetAll())
foreach ((string name, ReadOnlyCollection<Die> dice) in await masterOfCeremonies.DiceGroupManager.GetAll())
{
Console.WriteLine($"{name} -- {dice}");
Console.WriteLine($"{name} -- {dice}"); // maybe code a quick and dirty DieToString()
}
}
@ -332,11 +330,13 @@ namespace App
catch (ArgumentNullException ex)
{
Console.WriteLine(ex.Message);
logger.Warn(ex);
}
catch (UriFormatException ex)
{
Console.WriteLine("that URI was not valid");
Console.WriteLine(ex.Message);
logger.Warn(ex);
}
}
}
@ -353,15 +353,15 @@ namespace App
if (menuChoice.Equals("ok") && count == 0)
{
Console.WriteLine("create at least one valid face");
menuChoice = ""; // persiste en dehors du scope de cette fonction
menuChoice = ""; // persists outside the scope of this function
}
}
private static IEnumerable<Die> PrepareDice(MasterOfCeremonies masterOfCeremonies)
private async static Task<IEnumerable<Die>> PrepareDice(MasterOfCeremonies masterOfCeremonies)
{
List<Die> result = new();
Console.WriteLine("all known dice or groups of dice:");
ShowDice(masterOfCeremonies);
await ShowDice(masterOfCeremonies);
string menuChoiceDice = "";
while (!(menuChoiceDice.Equals("ok") && result.Any()))
{
@ -369,7 +369,7 @@ namespace App
menuChoiceDice = Console.ReadLine();
if (!menuChoiceDice.Equals("ok"))
{
IEnumerable<Die> chosenDice = masterOfCeremonies.DiceGroupManager.GetOneByName(menuChoiceDice).Value;
IEnumerable<Die> chosenDice = (await masterOfCeremonies.DiceGroupManager.GetOneByName(menuChoiceDice)).Dice;
foreach (Die die in chosenDice)
{
result.Add(die);
@ -378,35 +378,80 @@ namespace App
}
return result.AsEnumerable();
}
private static PlayerManager PreparePlayers(MasterOfCeremonies masterOfCeremonies)
private async static Task<PlayerManager> PreparePlayers(MasterOfCeremonies masterOfCeremonies)
{
PlayerManager result = new();
Console.WriteLine("all known players:");
ShowPlayers(masterOfCeremonies);
await ShowPlayers(masterOfCeremonies);
string menuChoicePlayers = "";
while (!(menuChoicePlayers.Equals("ok") && result.GetAll().Any()))
while (!(menuChoicePlayers.Equals("ok") && (await result.GetAll()).Any()))
{
Console.WriteLine("write the name of a player you want to add (at least one), or 'ok' if you're finished");
menuChoicePlayers = Console.ReadLine();
if (!menuChoicePlayers.Equals("ok"))
{
Player player = new(menuChoicePlayers);
if (!masterOfCeremonies.GlobalPlayerManager.GetAll().Contains(player))
if (!(await masterOfCeremonies.GlobalPlayerManager.GetAll()).Contains(player))
{
// if the player didn't exist, now it does... this is temporary
masterOfCeremonies.GlobalPlayerManager.Add(player);
// if the player didn't exist, now it does...
await masterOfCeremonies.GlobalPlayerManager.Add(player);
}
// almost no checks, this is temporary
try
{
result.Add(player);
await result.Add(player);
}
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
catch (ArgumentException ex) { Console.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
return result;
}
private static string TurnToString(Turn turn)
{
string[] datetime = turn.When.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
string date = datetime[0];
string time = datetime[1];
StringBuilder sb = new();
sb.AppendFormat("{0} {1} -- {2} rolled:",
date,
time,
PlayerToString(turn.Player));
foreach (KeyValuePair<Die, Face> kvp in turn.DiceNFaces)
{
sb.Append(" " + kvp.Value.StringValue);
}
return sb.ToString();
}
private async static Task<string> GameToString(Game game)
{
StringBuilder sb = new();
sb.Append($"Game: {game.Name}");
sb.Append("\nPlayers:");
foreach (Player player in game.PlayerManager.GetAll()?.Result)
{
sb.Append($" {PlayerToString(player)}");
}
sb.Append($"\nNext: {PlayerToString(await game.GetWhoPlaysNow())}");
sb.Append("\nLog:\n");
foreach (Turn turn in game.GetHistory())
{
sb.Append($"\t{TurnToString(turn)}\n");
}
return sb.ToString();
}
private static string PlayerToString(Player player)
{
return player.Name;
}
}
}

@ -13,10 +13,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Utils\Utils\Utils.csproj" />
</ItemGroup>
</Project>

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
namespace Data.EF.Dice
{
public class ColorDieEntity
public class ColorDieEntity : DieEntity
{
public new ICollection<ColorFaceEntity> Faces { get; set; } = new List<ColorFaceEntity>();
}
}

@ -1,12 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
using Model.Dice;
using Model.Dice.Faces;
namespace Data.EF.Dice
{
public static class ColorDieExtensions
{
public static ColorDie ToModel(this ColorDieEntity dieEntity)
{
/*
* creating an array of faces model
*/
ColorFace[] faces = dieEntity.Faces.ToModels().ToArray();
/*
* creating the die
*/
ColorDie die = new(faces[0], faces[1..]);
return die;
}
public static IEnumerable<ColorDie> ToModels(this IEnumerable<ColorDieEntity> entities) => entities.Select(entity => entity.ToModel());
public static ColorDieEntity ToEntity(this ColorDie model)
{
var entity = new ColorDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((ColorFace)face).ToEntity()); }
return entity;
}
public static IEnumerable<ColorDieEntity> ToEntities(this IEnumerable<ColorDie> models) => models.Select(model => model.ToEntity());
}
}

@ -0,0 +1,32 @@
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using System.Diagnostics.CodeAnalysis;
namespace Data.EF.Dice
{
/// <summary>
/// not designed to be instantiated, but not abstract in order to allow extensions
/// </summary>
///
public class DieEntity : IEqualityComparer<DieEntity>
{
public Guid ID { get; set; }
public ICollection<FaceEntity> Faces { get; set; } = new List<FaceEntity>(); // one to many
public ICollection<TurnEntity> Turns { get; set; } = new List<TurnEntity>(); // many to many
public List<DieTurn> DieTurns { get; set; } = new();
public bool Equals(DieEntity x, DieEntity y)
{
return x is not null
&& y is not null
&& x.ID.Equals(y.ID)
&& x.Faces.Equals(y.Faces);
}
public int GetHashCode([DisallowNull] DieEntity obj)
{
return HashCode.Combine(ID, Faces);
}
}
}

@ -1,12 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Data.EF.Dice.Faces
{
public class ColorFaceEntity
public class ColorFaceEntity : FaceEntity
{
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public Guid ColorDieEntityID { get; set; }
public ColorDieEntity ColorDieEntity { get; set; }
public void SetValue(Color c)
{
A = c.A;
R = c.R;
G = c.G;
B = c.B;
}
}
}

@ -3,10 +3,19 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Dice.Faces;
using System.Drawing;
namespace Data.EF.Dice.Faces
{
public static class ColorFaceExtensions
{
public static ColorFace ToModel(this ColorFaceEntity clrFaceEntity) => new(Color.FromArgb(clrFaceEntity.A, clrFaceEntity.R, clrFaceEntity.G, clrFaceEntity.B));
public static IEnumerable<ColorFace> ToModels(this IEnumerable<ColorFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static ColorFaceEntity ToEntity(this ColorFace model) => new() { A = model.Value.A, R = model.Value.R, G = model.Value.G, B = model.Value.B };
public static IEnumerable<ColorFaceEntity> ToEntities(this IEnumerable<ColorFace> models) => models.Select(model => model.ToEntity());
}
}

@ -0,0 +1,28 @@
using Data.EF.Games;
using Data.EF.Joins;
using System.Diagnostics.CodeAnalysis;
namespace Data.EF.Dice.Faces
{
/// <summary>
/// not designed to be instantiated, but not abstract in order to allow extensions
/// </summary>
public class FaceEntity : IEqualityComparer<FaceEntity>
{
public Guid ID { get; set; }
public ICollection<TurnEntity> Turns { get; set; } // many to many
public List<FaceTurn> FaceTurns { get; set; }
public bool Equals(FaceEntity x, FaceEntity y)
{
return x is not null
&& y is not null
&& x.ID.Equals(y.ID);
}
public int GetHashCode([DisallowNull] FaceEntity obj)
{
return ID.GetHashCode();
}
}
}

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice.Faces
namespace Data.EF.Dice.Faces
{
public class ImageFaceEntity
public class ImageFaceEntity : FaceEntity
{
public string Value { get; set; }
public Guid ImageDieEntityID { get; set; }
public ImageDieEntity ImageDieEntity { get; set; }
}
}

@ -1,4 +1,7 @@
using System;
using Data.EF.Players;
using Model.Dice.Faces;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +11,12 @@ namespace Data.EF.Dice.Faces
{
public static class ImageFaceExtensions
{
public static ImageFace ToModel(this ImageFaceEntity entity) => new(new Uri(entity.Value));
public static IEnumerable<ImageFace> ToModels(this IEnumerable<ImageFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static ImageFaceEntity ToEntity(this ImageFace model) => new() { Value = model.StringValue };
public static IEnumerable<ImageFaceEntity> ToEntities(this IEnumerable<ImageFace> models) => models.Select(model => model.ToEntity());
}
}

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice.Faces
namespace Data.EF.Dice.Faces
{
public class NumberFaceEntity
public class NumberFaceEntity : FaceEntity
{
public int Value { get; set; }
public Guid NumberDieEntityID { get; set; }
public NumberDieEntity NumberDieEntity { get; set; }
}
}

@ -1,4 +1,5 @@
using System;
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +9,12 @@ namespace Data.EF.Dice.Faces
{
public static class NumberFaceExtensions
{
public static NumberFace ToModel(this NumberFaceEntity entity) => new(entity.Value);
public static IEnumerable<NumberFace> ToModels(this IEnumerable<NumberFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static NumberFaceEntity ToEntity(this NumberFace model) => new() { Value = model.Value };
public static IEnumerable<NumberFaceEntity> ToEntities(this IEnumerable<NumberFace> models) => models.Select(model => model.ToEntity());
}
}

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
namespace Data.EF.Dice
{
public class ImageDieEntity
public class ImageDieEntity : DieEntity
{
public new ICollection<ImageFaceEntity> Faces { get; set; } = new List<ImageFaceEntity>();
}
}

@ -1,12 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
using Model.Dice;
using Model.Dice.Faces;
namespace Data.EF.Dice
{
public static class ImageDieExtensions
{
public static ImageDie ToModel(this ImageDieEntity dieEntity)
{
/*
* creating an array of faces model
*/
ImageFace[] faces = dieEntity.Faces.ToModels().ToArray();
/*
* creating the die
*/
ImageDie die = new(faces[0], faces[1..]);
return die;
}
public static IEnumerable<ImageDie> ToModels(this IEnumerable<ImageDieEntity> entities) => entities.Select(entity => entity.ToModel());
public static ImageDieEntity ToEntity(this ImageDie model)
{
var entity = new ImageDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((ImageFace)face).ToEntity()); }
return entity;
}
public static IEnumerable<ImageDieEntity> ToEntities(this IEnumerable<ImageDie> models) => models.Select(model => model.ToEntity());
}
}

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
namespace Data.EF.Dice
{
public class NumberDieEntity
public class NumberDieEntity : DieEntity
{
public new ICollection<NumberFaceEntity> Faces { get; set; } = new List<NumberFaceEntity>();
}
}

@ -1,12 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice.Faces;
using Model.Dice;
using Model.Dice.Faces;
namespace Data.EF.Dice
{
public static class NumberDieExtensions
{
public static NumberDie ToModel(this NumberDieEntity dieEntity)
{
/*
* creating an array of faces model
*/
NumberFace[] faces = dieEntity.Faces.ToModels().ToArray();
/*
* creating the die
*/
NumberDie die = new(faces[0], faces[1..]);
return die;
}
public static IEnumerable<NumberDie> ToModels(this IEnumerable<NumberDieEntity> entities) => entities.Select(entity => ToModel(entity));
public static NumberDieEntity ToEntity(this NumberDie model)
{
var entity = new NumberDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((NumberFace)face).ToEntity()); }
return entity;
}
public static IEnumerable<NumberDieEntity> ToEntities(this IEnumerable<NumberDie> models) => models.Select(model => model.ToEntity());
}
}

@ -1,4 +1,8 @@
using Data.EF.Players;
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using Microsoft.EntityFrameworkCore;
using Model.Games;
@ -6,9 +10,17 @@ namespace Data.EF
{
public class DiceAppDbContext : DbContext, ILoader
{
public virtual MasterOfCeremonies LoadApp() { throw new NotImplementedException(); }
// will be async!
public virtual Task<MasterOfCeremonies> LoadApp() { throw new NotImplementedException(); }
public DbSet<PlayerEntity> Players { get; set; }
public DbSet<PlayerEntity> PlayerEntity { get; set; }
public DbSet<TurnEntity> TurnEntity { get; set; }
public DbSet<NumberDieEntity> NumberDieEntity { get; set; }
public DbSet<NumberFaceEntity> NumberFaceEntity { get; set; }
public DbSet<ImageDieEntity> ImageDieEntity { get; set; }
public DbSet<ImageFaceEntity> ImageFaceEntity { get; set; }
public DbSet<ColorDieEntity> ColorDieEntity { get; set; }
public DbSet<ColorFaceEntity> ColorFaceEntity { get; set; }
public DiceAppDbContext() { }
@ -19,5 +31,52 @@ namespace Data.EF
{
if (!optionsBuilder.IsConfigured) optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db").EnableSensitiveDataLogging();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// thanks to https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#join-entity-type-configuration
// many to many TurnEntity <-> FaceEntity
modelBuilder.Entity<FaceEntity>()
.HasMany(face => face.Turns)
.WithMany(turn => turn.Faces)
.UsingEntity<FaceTurn>(
join => join
// FaceTurn --> TurnEntity
.HasOne(faceturn => faceturn.TurnEntity)
.WithMany(turn => turn.FaceTurns)
.HasForeignKey(faceturn => faceturn.TurnEntityID),
join => join
// FaceTurn --> FaceEntity
.HasOne(faceturn => faceturn.FaceEntity)
.WithMany(face => face.FaceTurns)
.HasForeignKey(faceturn => faceturn.FaceEntityID),
// FaceTurn.PK = (ID1, ID2)
join => join.HasKey(faceturn => new { faceturn.FaceEntityID, faceturn.TurnEntityID })
);
// many to many TurnEntity <-> DieEntity
modelBuilder.Entity<DieEntity>()
.HasMany(die => die.Turns)
.WithMany(turn => turn.Dice)
.UsingEntity<DieTurn>(
join => join
// DieTurn --> TurnEntity
.HasOne(dieturn => dieturn.TurnEntity)
.WithMany(turn => turn.DieTurns)
.HasForeignKey(dieturn => dieturn.TurnEntityID),
join => join
// DieTurn --> DieEntity
.HasOne(dieturn => dieturn.DieEntity)
.WithMany(die => die.DieTurns)
.HasForeignKey(dieturn => dieturn.DieEntityID),
// DieTurn.PK = (ID1, ID2)
join => join.HasKey(dieturn => new { dieturn.DieEntityID, dieturn.TurnEntityID })
);
}
}
}

@ -1,13 +1,20 @@
using Data.EF.Players;
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using Microsoft.EntityFrameworkCore;
using Model.Games;
using System.Drawing;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
namespace Data.EF
{
public class DiceAppDbContextWithStub : DiceAppDbContext
{
public override MasterOfCeremonies LoadApp() { throw new NotImplementedException(); }
// will be async
public override Task<MasterOfCeremonies> LoadApp() { throw new NotImplementedException(); }
public DiceAppDbContextWithStub() { }
@ -18,12 +25,88 @@ namespace Data.EF
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PlayerEntity>().HasData(
new PlayerEntity { ID = Guid.NewGuid(), Name = "Alice" }, // some tests depend on this name
new PlayerEntity { ID = Guid.NewGuid(), Name = "Bob" }, // some tests depend on this name
new PlayerEntity { ID = Guid.NewGuid(), Name = "Clyde" }, // some tests depend on this name
new PlayerEntity { ID = Guid.NewGuid(), Name = "Dahlia" } // some tests depend on this name
);
Guid playerID_1 = Guid.NewGuid();
Guid playerID_2 = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1");
Guid playerID_3 = Guid.NewGuid();
Guid playerID_4 = Guid.NewGuid();
PlayerEntity player_1 = new() { ID = playerID_1, Name = "Alice" };
PlayerEntity player_2 = new() { ID = playerID_2, Name = "Bob" };
PlayerEntity player_3 = new() { ID = playerID_3, Name = "Clyde" };
PlayerEntity player_4 = new() { ID = playerID_4, Name = "Dahlia" };
Guid turnID_1 = Guid.NewGuid();
Guid turnID_2 = Guid.NewGuid();
DateTime datetime_1 = new(2017, 1, 6, 17, 30, 0, DateTimeKind.Utc);
TurnEntity turn_1 = new() { ID = turnID_1, When = datetime_1, PlayerEntityID = playerID_1 };
TurnEntity turn_2 = new() { ID = turnID_2, When = DateTime.UtcNow, PlayerEntityID = playerID_2 };
Guid dieID_1 = Guid.NewGuid();
Guid dieID_2 = Guid.NewGuid();
Guid dieID_3 = Guid.NewGuid();
NumberDieEntity die_1 = new() { ID = dieID_1 };
ImageDieEntity die_2 = new() { ID = dieID_2 };
ColorDieEntity die_3 = new() { ID = dieID_3 };
Guid faceID_1 = Guid.NewGuid();
Guid faceID_2 = Guid.NewGuid();
Guid faceID_3 = Guid.NewGuid();
Guid faceID_4 = Guid.NewGuid();
Guid faceID_5 = Guid.NewGuid();
Guid faceID_6 = Guid.NewGuid();
NumberFaceEntity face_1 = new() { ID = faceID_1, Value = 1, NumberDieEntityID = dieID_1 };
NumberFaceEntity face_2 = new() { ID = faceID_2, Value = 2, NumberDieEntityID = dieID_1 };
ImageFaceEntity face_3 = new() { ID = faceID_3, Value = "https://1", ImageDieEntityID = dieID_2 };
ImageFaceEntity face_4 = new() { ID = faceID_4, Value = "https://2", ImageDieEntityID = dieID_2 };
ColorFaceEntity face_5 = new() { ID = faceID_5, ColorDieEntityID = dieID_3 };
face_5.SetValue(Color.FromArgb(255, 255, 0, 0));
ColorFaceEntity face_6 = new() { ID = faceID_6, ColorDieEntityID = dieID_3 };
face_6.SetValue(Color.FromName("green"));
modelBuilder.Entity<PlayerEntity>().HasData(player_1, player_2, player_3, player_4);
modelBuilder.Entity<TurnEntity>().HasData(turn_1, turn_2);
modelBuilder.Entity<NumberDieEntity>().HasData(die_1);
modelBuilder.Entity<NumberFaceEntity>().HasData(face_1, face_2);
modelBuilder.Entity<ImageDieEntity>().HasData(die_2);
modelBuilder.Entity<ImageFaceEntity>().HasData(face_3, face_4);
modelBuilder.Entity<ColorDieEntity>().HasData(die_3);
modelBuilder.Entity<ColorFaceEntity>().HasData(face_5, face_6);
// die 1 die 2 die 3
// turn 1 : num->2, img->https://2, clr->red
// turn 2 : num->1, clr->green
modelBuilder
.Entity<TurnEntity>()
.HasMany(turn => turn.Faces)
.WithMany(face => face.Turns)
.UsingEntity<FaceTurn>(join => join.HasData(
new { TurnEntityID = turnID_1, FaceEntityID = faceID_2 },
new { TurnEntityID = turnID_1, FaceEntityID = faceID_4 },
new { TurnEntityID = turnID_1, FaceEntityID = faceID_5 },
new { TurnEntityID = turnID_2, FaceEntityID = faceID_1 },
new { TurnEntityID = turnID_2, FaceEntityID = faceID_6 }));
modelBuilder
.Entity<TurnEntity>()
.HasMany(turn => turn.Dice)
.WithMany(die => die.Turns)
.UsingEntity<DieTurn>(join => join.HasData(
new { TurnEntityID = turnID_1, DieEntityID = dieID_1 },
new { TurnEntityID = turnID_1, DieEntityID = dieID_2 },
new { TurnEntityID = turnID_1, DieEntityID = dieID_3 },
new { TurnEntityID = turnID_2, DieEntityID = dieID_1 },
new { TurnEntityID = turnID_2, DieEntityID = dieID_3 }));
}
}
}

@ -1,12 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Joins;
using Data.EF.Players;
using Model.Players;
namespace Data.EF.Games
{
public class TurnEntity
public sealed class TurnEntity : IEquatable<TurnEntity>
{
public Guid ID { get; set; }
public DateTime When { get; set; }
public PlayerEntity PlayerEntity { get; set; }
public Guid PlayerEntityID { get; set; }
public ICollection<DieEntity> Dice { get; set; } = new List<DieEntity>(); // many to many
public List<DieTurn> DieTurns { get; set; } = new();
public ICollection<FaceEntity> Faces { get; set; } = new List<FaceEntity>(); // many to many
public List<FaceTurn> FaceTurns { get; set; } = new();
public override bool Equals(object obj)
{
if (obj is not TurnEntity)
{
return false;
}
return Equals(obj as TurnEntity);
}
public bool Equals(TurnEntity other)
{
if (other is null
||
!(PlayerEntity.Equals(other.PlayerEntity)
&& When.Equals(other.When)
&& ID.Equals(other.ID)
&& Dice.Count == other.Dice.Count
&& Faces.Count == other.Faces.Count))
{
return false;
}
for (int i = 0; i < Faces.Count; i++)
{
if (Dice.ElementAt(i).Faces.Count
!= other.Dice.ElementAt(i).Faces.Count)
{
return false;
}
if (!other.Faces.ElementAt(i).ID
.Equals(Faces.ElementAt(i).ID))
{
return false;
}
for (int j = 0; j < Dice.ElementAt(i).Faces.Count; j++)
{
if (!other.Dice.ElementAt(i).Faces.ElementAt(j).ID
.Equals(Dice.ElementAt(i).Faces.ElementAt(j).ID))
{
return false;
}
}
}
return true;
}
public override int GetHashCode()
{
int result = HashCode.Combine(
ID,
When,
PlayerEntity);
foreach (DieEntity die in Dice)
{
result += die.GetHashCode();
}
foreach (FaceEntity face in Faces)
{
result += face.GetHashCode();
}
return result;
}
}
}
}

@ -1,12 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Players;
using Model.Dice;
using Model.Dice.Faces;
using Model.Games;
namespace Data.EF.Games
{
public static class TurnExtensions
{
private static (List<Die>, List<Face>) ToModelsByTypes(ICollection<DieEntity> diceEntities, ICollection<FaceEntity> faceEntities)
{
List<Die> dice = new();
List<Face> faces = new();
foreach (DieEntity dieEntity in diceEntities)
{
if (dieEntity.GetType() == typeof(NumberDieEntity)) { dice.Add((dieEntity as NumberDieEntity).ToModel()); }
if (dieEntity.GetType() == typeof(ColorDieEntity)) { dice.Add((dieEntity as ColorDieEntity).ToModel()); }
if (dieEntity.GetType() == typeof(ImageDieEntity)) { dice.Add((dieEntity as ImageDieEntity).ToModel()); }
}
foreach (FaceEntity faceEntity in faceEntities)
{
if (faceEntity.GetType() == typeof(NumberFaceEntity)) { faces.Add((faceEntity as NumberFaceEntity).ToModel()); }
if (faceEntity.GetType() == typeof(ColorFaceEntity)) { faces.Add((faceEntity as ColorFaceEntity).ToModel()); }
if (faceEntity.GetType() == typeof(ImageFaceEntity)) { faces.Add((faceEntity as ImageFaceEntity).ToModel()); }
}
return (dice, faces);
}
public static Turn ToModel(this TurnEntity entity)
{
Dictionary<Die, Face> DiceNFaces = new();
List<Die> keysList;
List<Face> valuesList;
(keysList, valuesList) = ToModelsByTypes(entity.Dice, entity.Faces);
DiceNFaces = Utils.Enumerables.FeedListsToDict(DiceNFaces, keysList, valuesList);
return Turn.CreateWithSpecifiedTime(when: entity.When, player: entity.PlayerEntity.ToModel(), diceNFaces: DiceNFaces);
}
public static IEnumerable<Turn> ToModels(this IEnumerable<TurnEntity> entities) => entities.Select(entity => entity.ToModel());
public static TurnEntity ToEntity(this Turn model)
{
List<DieEntity> DiceEntities = new();
List<FaceEntity> FaceEntities = new();
foreach (KeyValuePair<Die, Face> kvp in model.DiceNFaces)
{
if (kvp.Key.GetType() == typeof(NumberDie)) { DiceEntities.Add((kvp.Key as NumberDie).ToEntity()); FaceEntities.Add((kvp.Value as NumberFace).ToEntity()); }
if (kvp.Key.GetType() == typeof(ImageDie)) { DiceEntities.Add((kvp.Key as ImageDie).ToEntity()); FaceEntities.Add((kvp.Value as ImageFace).ToEntity()); }
if (kvp.Key.GetType() == typeof(ColorDie)) { DiceEntities.Add((kvp.Key as ColorDie).ToEntity()); FaceEntities.Add((kvp.Value as ColorFace).ToEntity()); }
}
return new TurnEntity() { When = model.When, PlayerEntity = model.Player.ToEntity(), Dice = DiceEntities, Faces = FaceEntities };
}
public static IEnumerable<TurnEntity> ToEntities(this IEnumerable<Turn> models) => models.Select(model => model.ToEntity());
}
}
}

@ -0,0 +1,15 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
namespace Data.EF.Joins
{
public class DieTurn
{
public Guid DieEntityID { get; set; }
public DieEntity DieEntity { get; set; }
public Guid TurnEntityID { get; set; }
public TurnEntity TurnEntity { get; set; }
}
}

@ -0,0 +1,19 @@
using Data.EF.Dice.Faces;
using Data.EF.Games;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Joins
{
public class FaceTurn
{
public Guid FaceEntityID { get; set; }
public FaceEntity FaceEntity { get; set; }
public Guid TurnEntityID { get; set; }
public TurnEntity TurnEntity { get; set; }
}
}

@ -1,20 +1,23 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
using System.Collections.ObjectModel;
namespace Data.EF.Players
{
public sealed class PlayerDbManager : IManager<PlayerEntity>
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db), "param should not be null");
ArgumentNullException ex = new(nameof(db), "param should not be null");
logger.Error(ex, "attempted to construct PlayerDbManager with a null context");
throw ex;
}
this.db = db;
}
@ -29,11 +32,15 @@ namespace Data.EF.Players
{
if (entity is null)
{
throw new ArgumentNullException(nameof(entity), "param should not be null");
ArgumentNullException ex = new(nameof(entity), "param should not be null");
logger.Warn(ex);
throw ex;
}
if (string.IsNullOrWhiteSpace(entity.Name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
ArgumentException ex = new("Name property should not be null or whitespace", nameof(entity));
logger.Warn(ex);
throw ex;
}
entity.Name = entity.Name.Trim();
}
@ -45,23 +52,33 @@ namespace Data.EF.Players
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Add(PlayerEntity toAdd)
public Task<PlayerEntity> Add(PlayerEntity toAdd)
{
CleanPlayerEntity(toAdd);
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
if (db.PlayerEntity.Where(entity => entity.Name == toAdd.Name).Any())
{
throw new ArgumentException("this username is already taken", nameof(toAdd));
ArgumentException ex = new("this username is already taken", nameof(toAdd));
logger.Warn(ex);
throw ex;
}
EntityEntry ee = db.Players.Add(toAdd);
db.SaveChanges();
return InternalAdd(toAdd);
}
private async Task<PlayerEntity> InternalAdd(PlayerEntity toAdd)
{
EntityEntry ee = await db.PlayerEntity.AddAsync(toAdd);
await db.SaveChangesAsync();
logger.Info("Added {0}", ee.Entity.ToString());
return (PlayerEntity)ee.Entity;
}
public IEnumerable<PlayerEntity> GetAll()
public async Task<ReadOnlyCollection<PlayerEntity>> GetAll()
{
return db.Players.AsEnumerable();
List<PlayerEntity> players = new();
await Task.Run(() => players.AddRange(db.PlayerEntity));
return new ReadOnlyCollection<PlayerEntity>(players);
}
/// <summary>
@ -72,25 +89,30 @@ namespace Data.EF.Players
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public PlayerEntity GetOneByName(string name)
public Task<PlayerEntity> GetOneByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(name));
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).First();
return InternalGetOneByName(name);
}
public bool IsPresentByName(string name)
private async Task<PlayerEntity> InternalGetOneByName(string name)
{
return await db.PlayerEntity.Where(p => p.Name == name).FirstAsync();
}
public async Task<bool> IsPresentByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).Any();
return await db.PlayerEntity.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
}
/// <summary>
@ -102,10 +124,11 @@ namespace Data.EF.Players
public void Remove(PlayerEntity toRemove)
{
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID))
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.Players.Remove(toRemove);
db.PlayerEntity.Remove(toRemove);
logger.Info("Removed {0}", toRemove.ToString());
db.SaveChanges();
}
}
@ -118,7 +141,7 @@ namespace Data.EF.Players
/// <returns>the updated entity</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Update(PlayerEntity before, PlayerEntity after)
public Task<PlayerEntity> Update(PlayerEntity before, PlayerEntity after)
{
PlayerEntity[] args = { before, after };
@ -129,19 +152,18 @@ namespace Data.EF.Players
if (before.ID != after.ID)
{
throw new ArgumentException("ID cannot be updated", nameof(after));
ArgumentException ex = new("ID cannot be updated", nameof(after));
logger.Warn(ex);
throw ex;
}
string beforeName = before.Name;
before.Name = after.Name;
EntityEntry ee = db.Players.Update(before);
db.SaveChanges();
before.Name = beforeName;
return (PlayerEntity)ee.Entity;
return InternalUpdate(before, after);
}
private async Task<PlayerEntity> InternalUpdate(PlayerEntity before, PlayerEntity after)
{
Remove(before);
return await Add(after);
}
/// <summary>
@ -151,14 +173,14 @@ namespace Data.EF.Players
/// <param name="ID">the ID to look for</param>
/// <returns>PlayerEntity with that ID</returns>
/// <exception cref="InvalidOperationException"></exception>
public PlayerEntity GetOneByID(Guid ID)
public async Task<PlayerEntity> GetOneByID(Guid ID)
{
return db.Players.First(p => p.ID == ID);
return await db.PlayerEntity.FirstAsync(p => p.ID == ID);
}
public bool IsPresentByID(Guid ID)
public async Task<bool> IsPresentByID(Guid ID)
{
return db.Players.Where(p => p.ID == ID).Any();
return await db.PlayerEntity.FirstOrDefaultAsync(p => p.ID == ID) is not null;
}
}
}

@ -1,20 +1,23 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
using System.Collections.ObjectModel;
namespace Data.EF.Players
{
public sealed class PlayerDbManager : IManager<PlayerEntity>
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db), "param should not be null");
ArgumentNullException ex = new(nameof(db), "param should not be null");
logger.Error(ex, "attempted to construct PlayerDbManager with a null context");
throw ex;
}
this.db = db;
}
@ -29,11 +32,15 @@ namespace Data.EF.Players
{
if (entity is null)
{
throw new ArgumentNullException(nameof(entity), "param should not be null");
ArgumentNullException ex = new(nameof(entity), "param should not be null");
logger.Warn(ex);
throw ex;
}
if (string.IsNullOrWhiteSpace(entity.Name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
ArgumentException ex = new("Name property should not be null or whitespace", nameof(entity));
logger.Warn(ex);
throw ex;
}
entity.Name = entity.Name.Trim();
}
@ -45,23 +52,33 @@ namespace Data.EF.Players
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Add(PlayerEntity toAdd)
public Task<PlayerEntity> Add(PlayerEntity toAdd)
{
CleanPlayerEntity(toAdd);
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
if (db.PlayerEntity.Where(entity => entity.Name == toAdd.Name).Any())
{
throw new ArgumentException("this username is already taken", nameof(toAdd));
ArgumentException ex = new("this username is already taken", nameof(toAdd));
logger.Warn(ex);
throw ex;
}
EntityEntry ee = db.Players.Add(toAdd);
db.SaveChanges();
return InternalAdd(toAdd);
}
private async Task<PlayerEntity> InternalAdd(PlayerEntity toAdd)
{
EntityEntry ee = await db.PlayerEntity.AddAsync(toAdd);
await db.SaveChangesAsync();
logger.Info("Added {0}", ee.Entity.ToString());
return (PlayerEntity)ee.Entity;
}
public IEnumerable<PlayerEntity> GetAll()
public async Task<ReadOnlyCollection<PlayerEntity>> GetAll()
{
return db.Players.AsEnumerable();
List<PlayerEntity> players = new();
await Task.Run(() => players.AddRange(db.PlayerEntity));
return new ReadOnlyCollection<PlayerEntity>(players);
}
/// <summary>
@ -72,25 +89,30 @@ namespace Data.EF.Players
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public PlayerEntity GetOneByName(string name)
public Task<PlayerEntity> GetOneByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(name));
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).First();
return InternalGetOneByName(name);
}
public bool IsPresentByName(string name)
private async Task<PlayerEntity> InternalGetOneByName(string name)
{
return await db.PlayerEntity.Where(p => p.Name == name).FirstAsync();
}
public async Task<bool> IsPresentByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).Any();
return await db.PlayerEntity.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
}
/// <summary>
@ -102,10 +124,11 @@ namespace Data.EF.Players
public void Remove(PlayerEntity toRemove)
{
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID))
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.Players.Remove(toRemove);
db.PlayerEntity.Remove(toRemove);
logger.Info("Removed {0}", toRemove.ToString());
db.SaveChanges();
}
}
@ -118,7 +141,7 @@ namespace Data.EF.Players
/// <returns>the updated entity</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Update(PlayerEntity before, PlayerEntity after)
public Task<PlayerEntity> Update(PlayerEntity before, PlayerEntity after)
{
PlayerEntity[] args = { before, after };
@ -129,19 +152,18 @@ namespace Data.EF.Players
if (before.ID != after.ID)
{
throw new ArgumentException("ID cannot be updated", nameof(after));
ArgumentException ex = new("ID cannot be updated", nameof(after));
logger.Warn(ex);
throw ex;
}
string beforeName = before.Name;
before.Name = after.Name;
EntityEntry ee = db.Players.Update(before);
db.SaveChanges();
before.Name = beforeName;
return (PlayerEntity)ee.Entity;
return InternalUpdate(before, after);
}
private async Task<PlayerEntity> InternalUpdate(PlayerEntity before, PlayerEntity after)
{
Remove(before);
return await Add(after);
}
/// <summary>
@ -151,14 +173,14 @@ namespace Data.EF.Players
/// <param name="ID">the ID to look for</param>
/// <returns>PlayerEntity with that ID</returns>
/// <exception cref="InvalidOperationException"></exception>
public PlayerEntity GetOneByID(Guid ID)
public async Task<PlayerEntity> GetOneByID(Guid ID)
{
return db.Players.First(p => p.ID == ID);
return await db.PlayerEntity.FirstAsync(p => p.ID == ID);
}
public bool IsPresentByID(Guid ID)
public async Task<bool> IsPresentByID(Guid ID)
{
return db.Players.Where(p => p.ID == ID).Any();
return await db.PlayerEntity.FirstOrDefaultAsync(p => p.ID == ID) is not null;
}
}
}

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Data.EF.Games;
using Microsoft.EntityFrameworkCore;
namespace Data.EF.Players
{
@ -9,6 +10,8 @@ namespace Data.EF.Players
public string Name { get; set; }
public ICollection<TurnEntity> Turns { get; set; } = new List<TurnEntity>();
public override bool Equals(object obj)
{
if (obj is not PlayerEntity)
@ -20,7 +23,7 @@ namespace Data.EF.Players
public bool Equals(PlayerEntity other)
{
return other is not null && this.ID == other.ID && this.Name == other.Name;
return other is not null && this.ID.Equals(other.ID) && this.Name.Equals(other.Name);
}
public override int GetHashCode()

@ -4,24 +4,12 @@ namespace Data.EF.Players
{
public static class PlayerExtensions
{
public static Player ToModel(this PlayerEntity entity)
{
return new Player(name: entity.Name);
}
public static Player ToModel(this PlayerEntity entity) => new(name: entity.Name);
public static IEnumerable<Player> ToModels(this IEnumerable<PlayerEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static IEnumerable<Player> ToModels(this IEnumerable<PlayerEntity> entities) => entities.Select(entity => entity.ToModel());
public static PlayerEntity ToEntity(this Player model)
{
return new PlayerEntity() { Name = model.Name };
}
public static PlayerEntity ToEntity(this Player model) => new() { Name = model.Name };
public static IEnumerable<PlayerEntity> ToEntities(this IEnumerable<Player> models)
{
return models.Select(model => model.ToEntity());
}
public static IEnumerable<PlayerEntity> ToEntities(this IEnumerable<Player> models) => models.Select(model => model.ToEntity());
}
}

@ -4,6 +4,6 @@ namespace Data
{
public interface ILoader
{
public MasterOfCeremonies LoadApp();
public Task<MasterOfCeremonies> LoadApp();
}
}

@ -8,15 +8,15 @@ namespace Data
{
public class Stub : ILoader
{
public MasterOfCeremonies LoadApp()
public async Task<MasterOfCeremonies> LoadApp()
{
MasterOfCeremonies gr = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
MasterOfCeremonies mc = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
Player player1 = new("Alice(Old Stub)"), player2 = new("Bob(Old Stub)"), player3 = new("Clyde(Old Stub)");
gr.GlobalPlayerManager.Add(player1);
gr.GlobalPlayerManager.Add(player2);
gr.GlobalPlayerManager.Add(player3);
await mc.GlobalPlayerManager.Add(player1);
await mc.GlobalPlayerManager.Add(player2);
await mc.GlobalPlayerManager.Add(player3);
List<Die> monopolyDice = new();
@ -63,36 +63,36 @@ namespace Data
dndDice.Add(new NumberDie(d20Faces[0], d20Faces[1..]));
gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable()));
gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable()));
await mc.DiceGroupManager.Add(new DiceGroup(dndName, dndDice));
await mc.DiceGroupManager.Add(new DiceGroup(monopolyName, monopolyDice));
string game1 = "Forgotten Realms", game2 = "4e", game3 = "The Coopers";
gr.GameManager.Add(new(game1, new PlayerManager(), dndDice.AsEnumerable()));
gr.GameManager.Add(new(game2, new PlayerManager(), dndDice.AsEnumerable()));
gr.GameManager.Add(new(game3, new PlayerManager(), monopolyDice.AsEnumerable()));
await mc.GameManager.Add(new(game1, new PlayerManager(), dndDice.AsEnumerable()));
await mc.GameManager.Add(new(game2, new PlayerManager(), dndDice.AsEnumerable()));
await mc.GameManager.Add(new(game3, new PlayerManager(), monopolyDice.AsEnumerable()));
gr.GameManager.GetOneByName(game1).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game1).PlayerManager.Add(player2);
await (await mc.GameManager.GetOneByName(game1)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game1)).PlayerManager.Add(player2);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player2);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player3);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player2);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player3);
gr.GameManager.GetOneByName(game3).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game3).PlayerManager.Add(player3);
await (await mc.GameManager.GetOneByName(game3)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game3)).PlayerManager.Add(player3);
foreach (Game game in gr.GameManager.GetAll())
foreach (Game game in mc.GameManager.GetAll()?.Result)
{
for (int i = 0; i < 10; i++)
{
Player currentPlayer = game.GetWhoPlaysNow();
Player currentPlayer = await game.GetWhoPlaysNow();
game.PerformTurn(currentPlayer);
game.PrepareNextPlayer(currentPlayer);
await game.PrepareNextPlayer(currentPlayer);
}
}
return gr;
return mc;
}
}
}

@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{953D2D67-BCE7-412C-B7BB-7C63B5592359}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils\Utils.csproj", "{9300910D-9D32-4C79-8868-67D0ED56E2F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -29,6 +31,10 @@ Global
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.Build.0 = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -14,7 +14,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{953D2D67-BCE7-412C-B7BB-7C63B5592359}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils\Utils.csproj", "{9300910D-9D32-4C79-8868-67D0ED56E2F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -38,6 +40,10 @@ Global
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.Build.0 = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Model.Dice
{
public sealed class DiceGroup : IEquatable<DiceGroup>
{
public string Name { get; private set; }
public ReadOnlyCollection<Die> Dice => new(dice);
private readonly List<Die> dice = new();
public DiceGroup(string name, IEnumerable<Die> dice)
{
Name = name;
this.dice.AddRange(dice);
}
public bool Equals(DiceGroup other)
{
return Name == other.Name && Dice.SequenceEqual(other.Dice);
}
public override bool Equals(object obj)
{
if (obj is null) return false; // is null
if (ReferenceEquals(obj, this)) return true; // is me
if (!obj.GetType().Equals(GetType())) return false; // is different type
return Equals(obj as DiceGroup); // is not me, is not null, is same type : send up
}
public override int GetHashCode()
{
return HashCode.Combine(Name, dice);
}
public void Deconstruct(out string name, out ReadOnlyCollection<Die> dice)
{
dice = Dice;
name = Name;
}
}
}

@ -1,39 +1,41 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Model.Dice
{
public class DiceGroupManager : IManager<KeyValuePair<string, IEnumerable<Die>>>
public class DiceGroupManager : IManager<DiceGroup>
{
private readonly Dictionary<string, IEnumerable<Die>> diceGroups = new();
private readonly List<DiceGroup> diceGroups = new();
public KeyValuePair<string, IEnumerable<Die>> Add(KeyValuePair<string, IEnumerable<Die>> toAdd)
public Task<DiceGroup> Add(DiceGroup toAdd)
{
if (string.IsNullOrWhiteSpace(toAdd.Key))
if (string.IsNullOrWhiteSpace(toAdd.Name))
{
throw new ArgumentNullException(nameof(toAdd), "param should not be null or empty");
}
if (diceGroups.Contains(toAdd))
{
throw new ArgumentException("this username is already taken", nameof(toAdd));
throw new ArgumentException("this dice group already exists", nameof(toAdd));
}
diceGroups.Add(toAdd.Key.Trim(), toAdd.Value);
return toAdd;
diceGroups.Add(new(toAdd.Name.Trim(), toAdd.Dice));
return Task.FromResult(toAdd);
}
public IEnumerable<KeyValuePair<string, IEnumerable<Die>>> GetAll()
public Task<ReadOnlyCollection<DiceGroup>> GetAll()
{
return diceGroups.AsEnumerable();
return Task.FromResult(new ReadOnlyCollection<DiceGroup>(diceGroups));
}
public KeyValuePair<string, IEnumerable<Die>> GetOneByID(Guid ID)
public Task<DiceGroup> GetOneByID(Guid ID)
{
throw new NotImplementedException();
}
public KeyValuePair<string, IEnumerable<Die>> GetOneByName(string name)
public Task<DiceGroup> GetOneByName(string name)
{
// les groupes de dés nommés :
// ils sont case-sensistive, mais "mon jeu" == "mon jeu " == " mon jeu"
@ -41,46 +43,43 @@ namespace Model.Dice
{
throw new ArgumentNullException(nameof(name), "param should not be null or empty");
}
else
{
return new KeyValuePair<string, IEnumerable<Die>>(name, diceGroups[name]);
}
return Task.FromResult(diceGroups.First(diceGroup => diceGroup.Name.Equals(name.Trim())));
}
public void Remove(KeyValuePair<string, IEnumerable<Die>> toRemove)
public void Remove(DiceGroup toRemove)
{
if (toRemove.Key is null)
if (toRemove.Name is null)
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
}
else
{
diceGroups.Remove(toRemove.Key);
diceGroups.Remove(toRemove);
}
}
/// <summary>
/// updates a (string, IEnumerable-of-Die) couple. only the name can be updated
/// updates a (string, ReadOnlyCollection-of-Die) couple. only the name can be updated
/// </summary>
/// <param name="before">original couple</param>
/// <param name="after">new couple</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public KeyValuePair<string, IEnumerable<Die>> Update(KeyValuePair<string, IEnumerable<Die>> before, KeyValuePair<string, IEnumerable<Die>> after)
public Task<DiceGroup> Update(DiceGroup before, DiceGroup after)
{
if (!before.Value.ToList().Equals(after.Value.ToList()))
// pas autorisé de changer les dés, juste le nom
if (!before.Dice.SequenceEqual(after.Dice))
{
throw new ArgumentException("the group of dice cannot be updated, only the name", nameof(before));
}
if (string.IsNullOrWhiteSpace(before.Key) || string.IsNullOrWhiteSpace(after.Key))
if (string.IsNullOrWhiteSpace(before.Name) || string.IsNullOrWhiteSpace(after.Name))
{
throw new ArgumentNullException(nameof(before), "dice group name should not be null or empty");
}
diceGroups.Remove(before.Key);
diceGroups.Add(after.Key, after.Value);
return after;
Remove(before);
Add(after);
return Task.FromResult(after);
}
}

@ -1,13 +1,14 @@
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Model.Dice
{
public abstract class Die
{
public IEnumerable<Face> Faces => faces;
public ReadOnlyCollection<Face> Faces => new(faces);
protected static readonly Random rnd = new();
@ -20,7 +21,7 @@ namespace Model.Dice
public virtual Face GetRandomFace()
{
int faceIndex = rnd.Next(0, Faces.Count());
int faceIndex = rnd.Next(0, Faces.Count);
return Faces.ElementAt(faceIndex);
}
}

@ -3,12 +3,8 @@
public abstract class Face
{
public string StringValue { get; protected set; }
public override string ToString()
{
return StringValue;
}
}
public abstract class Face<T> : Face
{
public T Value { get; protected set; }
@ -18,11 +14,5 @@
Value = value;
StringValue = value.ToString();
}
protected Face(T value, string stringValue)
{
Value = value;
StringValue = stringValue;
}
}
}

@ -3,8 +3,9 @@ using Model.Dice.Faces;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.Games
{
@ -34,18 +35,18 @@ namespace Model.Games
/// <summary>
/// references the position in list of the current player, for a given game.
/// </summary>
private int nextIndex;
private int nextIndex = 0;
/// <summary>
/// the turns that have been done so far
/// </summary>
private readonly List<Turn> turns;
private readonly List<Turn> turns = new();
/// </summary>
/// get a READ ONLY enumerable of all turns belonging to this game
/// </summary>
/// <returns>a readonly enumerable of all this game's turns</returns>
public IEnumerable<Turn> GetHistory() => turns.AsEnumerable();
public ReadOnlyCollection<Turn> GetHistory() => new(turns);
/// <summary>
/// the game's player manager, doing CRUD on players and switching whose turn it is
@ -55,8 +56,8 @@ namespace Model.Games
/// <summary>
/// the group of dice used for this game
/// </summary>
public IEnumerable<Die> Dice => dice;
private readonly IEnumerable<Die> dice;
public ReadOnlyCollection<Die> Dice => new(dice);
private readonly List<Die> dice = new();
/// <summary>
/// constructs a Game with its own history of Turns.
@ -70,19 +71,19 @@ namespace Model.Games
{
Name = name;
PlayerManager = playerManager;
this.turns = turns is null ? new List<Turn>() : turns.ToList();
this.dice = dice;
this.nextIndex = 0;
this.dice.AddRange(dice);
this.turns.AddRange(turns);
}
/// <summary>
/// constructs a Game with no history of turns.
///
/// </summary>
/// <param name="name">the name of the game 😎</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, IManager<Player> playerManager, IEnumerable<Die> dice)
: this(name, playerManager, dice, null)
: this(name, playerManager, dice, new List<Turn>())
{ }
/// <summary>
@ -112,13 +113,13 @@ namespace Model.Games
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public Player GetWhoPlaysNow()
public async Task<Player> GetWhoPlaysNow()
{
if (!PlayerManager.GetAll().Any())
if (!(await PlayerManager.GetAll()).Any())
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
}
return PlayerManager.GetAll().ElementAt(nextIndex);
return (await PlayerManager.GetAll()).ElementAt(nextIndex);
}
/// <summary>
@ -128,9 +129,9 @@ namespace Model.Games
/// <exception cref="MemberAccessException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void PrepareNextPlayer(Player current)
public async Task PrepareNextPlayer(Player current)
{
IEnumerable<Player> players = PlayerManager.GetAll();
IEnumerable<Player> players = await PlayerManager.GetAll();
if (!players.Any())
{
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
@ -143,16 +144,8 @@ namespace Model.Games
{
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
}
if (players.Last() == current)
{
// if we've reached the last player, we need the index to loop back around
nextIndex = 0;
}
else
{
// else we can just move up by one from the current index
nextIndex++;
}
nextIndex = (nextIndex + 1) % players.Count();
}
/// <summary>
@ -168,31 +161,5 @@ namespace Model.Games
}
return faces;
}
/// <summary>
/// represents a Game in string format
/// </summary>
/// <returns>a Game in string format</returns>
public override string ToString()
{
StringBuilder sb = new();
sb.Append($"Game: {Name}");
sb.Append("\nPlayers:");
foreach (Player player in PlayerManager.GetAll())
{
sb.Append($" {player.ToString()}");
}
sb.Append($"\nNext: {GetWhoPlaysNow()}");
sb.Append("\nLog:\n");
foreach (Turn turn in this.turns)
{
sb.Append($"\t{turn.ToString()}\n");
}
return sb.ToString();
}
}
}

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Model.Games
{
@ -9,18 +11,14 @@ namespace Model.Games
/// <summary>
/// the games managed by this instance
/// </summary>
private readonly List<Game> games;
private readonly List<Game> games = new();
public GameManager()
{
games = new();
}
/// <summary>
/// gets an unmodifiable collection of the games
/// </summary>
/// <returns>unmodifiable collection of the games</returns>
public IEnumerable<Game> GetAll() => games.AsEnumerable();
public Task<ReadOnlyCollection<Game>> GetAll() => Task.FromResult(new ReadOnlyCollection<Game>(games));
/// <summary>
/// finds the game with that name and returns it
@ -29,12 +27,11 @@ namespace Model.Games
/// </summary>
/// <param name="name">a games's name</param>
/// <returns>game with said name, <em>or null</em> if no such game was found</returns>
public Game GetOneByName(string name)
public Task<Game> GetOneByName(string name)
{
if (!string.IsNullOrWhiteSpace(name))
{
Game result = games.FirstOrDefault(g => g.Name == name);
return result; // may return null
return Task.FromResult(games.FirstOrDefault(g => g.Name == name)); // may return null
}
throw new ArgumentException("param should not be null or blank", nameof(name));
}
@ -45,7 +42,7 @@ namespace Model.Games
/// <param name="ID"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Game GetOneByID(Guid ID)
public Task<Game> GetOneByID(Guid ID)
{
throw new NotImplementedException();
}
@ -54,7 +51,7 @@ namespace Model.Games
/// saves a given game -- does not allow copies yet: if a game with the same name exists, it is overwritten
/// </summary>
/// <param name="toAdd">a game to save</param>
public Game Add(Game toAdd)
public Task<Game> Add(Game toAdd)
{
if (toAdd is null)
{
@ -64,7 +61,7 @@ namespace Model.Games
games.Remove(games.FirstOrDefault(g => g.Name == toAdd.Name));
// will often be an update: if game with that name exists, it is removed, else, nothing happens above
games.Add(toAdd);
return toAdd;
return Task.FromResult(toAdd);
}
/// <summary>
@ -88,7 +85,7 @@ namespace Model.Games
/// <param name="after">new game</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public Game Update(Game before, Game after)
public Task<Game> Update(Game before, Game after)
{
Game[] args = { before, after };
@ -103,7 +100,7 @@ namespace Model.Games
}
}
Remove(before);
return Add(after);
return (Add(after));
}
}
}

@ -1,16 +1,17 @@
using Model.Dice;
using Model.Players;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Model.Games
{
public class MasterOfCeremonies
{
public IManager<Player> GlobalPlayerManager { get; private set; }
public IManager<KeyValuePair<string, IEnumerable<Die>>> DiceGroupManager { get; private set; }
public IManager<DiceGroup> DiceGroupManager { get; private set; }
public IManager<Game> GameManager { get; private set; }
public MasterOfCeremonies(IManager<Player> globalPlayerManager, IManager<KeyValuePair<string, IEnumerable<Die>>> globalDiceGroupManager, IManager<Game> gameManager)
public MasterOfCeremonies(IManager<Player> globalPlayerManager, IManager<DiceGroup> globalDiceGroupManager, IManager<Game> gameManager)
{
GlobalPlayerManager = globalPlayerManager;
DiceGroupManager = globalDiceGroupManager;
@ -24,21 +25,21 @@ namespace Model.Games
/// <param name="playerManager"></param>
/// <param name="dice"></param>
/// <returns></returns>
public Game StartNewGame(string name, IManager<Player> playerManager, IEnumerable<Die> dice)
public async Task<Game> StartNewGame(string name, IManager<Player> playerManager, IEnumerable<Die> dice)
{
Game game = new(name, playerManager, dice);
return GameManager.Add(game);
return await GameManager.Add(game);
}
/// <summary>
/// plays one turn of the game
/// </summary>
/// <param name="game">the game from which a turn will be played</param>
public static void PlayGame(Game game)
public static async Task PlayGame(Game game)
{
Player current = game.GetWhoPlaysNow();
Player current = await game.GetWhoPlaysNow();
game.PerformTurn(current);
game.PrepareNextPlayer(current);
await game.PrepareNextPlayer(current);
}
}

@ -3,17 +3,14 @@ using Model.Dice.Faces;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Model.Games
{
/// <summary>
/// a Turn consists of a Player, a DateTime, and a IEnumerable of AbstractDieFace
/// Like a turn in some game.
/// <br/>
/// Two turns are equal if they are litterally the same instance in RAM
/// (default behaviors Equals() and GetHashCode())
/// </summary>
public sealed class Turn : IEquatable<Turn>
{
@ -31,7 +28,7 @@ namespace Model.Games
/// <summary>
/// the collection of Face that were rolled
/// </summary>
public IEnumerable<KeyValuePair<Die, Face>> DiceNFaces => diceNFaces.AsEnumerable();
public ReadOnlyDictionary<Die, Face> DiceNFaces => new(diceNFaces);
private readonly Dictionary<Die, Face> diceNFaces;
/// <summary>
@ -40,24 +37,7 @@ namespace Model.Games
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
private Turn(DateTime when, Player player, Dictionary<Die, Face> diceNFaces)
{
When = when;
Player = player;
this.diceNFaces = diceNFaces;
}
/// <summary>
/// creates a Turn with a specified time, passed as a parameter.
/// <br/>
/// whatever the DateTimeKind of <paramref name="when"/> might be,
/// it will become UTC during construction
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithSpecifiedTime(DateTime when, Player player, Dictionary<Die, Face> diceNFaces)
private Turn(DateTime when, Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
{
if (player is null)
{
@ -67,7 +47,7 @@ namespace Model.Games
{
throw new ArgumentNullException(nameof(diceNFaces), "param should not be null");
}
if (diceNFaces.Count == 0)
if (!diceNFaces.Any())
{
throw new ArgumentException("param should not be null", nameof(diceNFaces));
}
@ -76,50 +56,73 @@ namespace Model.Games
when = when.ToUniversalTime();
}
return new Turn(when, player, diceNFaces);
When = when;
Player = player;
this.diceNFaces = diceNFaces.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
/// <summary>
/// creates a Turn with a default time, which is "now" in UTC.
/// creates a Turn with a specified time, passed as a parameter.
/// <br/>
/// whatever the DateTimeKind of <paramref name="when"/> might be,
/// it will become UTC during construction
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithDefaultTime(Player player, Dictionary<Die, Face> diceNFaces)
public static Turn CreateWithSpecifiedTime(DateTime when, Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
{
return CreateWithSpecifiedTime(DateTime.UtcNow, player, diceNFaces);
return new Turn(when, player, diceNFaces);
}
/// <summary>
/// represents a turn in string format
/// creates a Turn with a default time, which is "now" in UTC.
/// </summary>
/// <returns>a turn in string format</returns>
public override string ToString()
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithDefaultTime(Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
{
string[] datetime = When.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
string date = datetime[0];
string time = datetime[1];
StringBuilder sb = new();
sb.AppendFormat("{0} {1} -- {2} rolled:",
date,
time,
Player.ToString());
foreach (Face face in this.diceNFaces.Values)
{
sb.Append(" " + face.ToString());
}
return sb.ToString();
return CreateWithSpecifiedTime(DateTime.UtcNow, player, diceNFaces);
}
public bool Equals(Turn other)
{
return Player.Equals(other.Player)
if (other is null
||
!(Player.Equals(other.Player)
&& When.Equals(other.When)
&& DiceNFaces.SequenceEqual(other.DiceNFaces);
&& DiceNFaces.Count == other.DiceNFaces.Count))
{
return false;
}
// 🤮
for (int i = 0; i < DiceNFaces.Count; i++)
{
if (DiceNFaces.ElementAt(i).Key.Faces.Count
!= other.DiceNFaces.ElementAt(i).Key.Faces.Count)
{
return false;
}
if (!other.DiceNFaces.ElementAt(i).Value.StringValue
.Equals(DiceNFaces.ElementAt(i).Value.StringValue))
{
return false;
}
for (int j = 0; j < DiceNFaces.ElementAt(i).Key.Faces.Count; j++)
{
if (!other.DiceNFaces.ElementAt(i).Key.Faces.ElementAt(j).StringValue
.Equals(DiceNFaces.ElementAt(i).Key.Faces.ElementAt(j).StringValue))
{
return false;
}
}
}
return true;
}
public override bool Equals(object obj)
@ -133,7 +136,7 @@ namespace Model.Games
public override int GetHashCode()
{
return HashCode.Combine(Player, When, DiceNFaces);
return When.GetHashCode();
}
}
}

@ -1,14 +1,21 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
namespace Model
{
public interface IManager<T>
{
public T Add(T toAdd);
public T GetOneByName(string name);
public T GetOneByID(Guid ID);
public IEnumerable<T> GetAll();
public T Update(T before, T after);
public Task<T> Add(T toAdd);
public Task<T> GetOneByName(string name);
public Task<T> GetOneByID(Guid ID);
public Task<ReadOnlyCollection<T>> GetAll();
public Task<T> Update(T before, T after);
public void Remove(T toRemove);
}
}

@ -4,4 +4,8 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
</Project>

@ -30,11 +30,6 @@ namespace Model.Players
: this(player?.Name) // passes the player's name if player exists, else null
{ }
public override string ToString()
{
return Name;
}
public bool Equals(Player other)
{
return other is not null && Name.ToUpper() == other.Name.ToUpper(); // equality is case insensitive

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Model.Players
{
@ -9,18 +11,14 @@ namespace Model.Players
/// <summary>
/// a collection of the players that this manager is in charge of
/// </summary>
private readonly List<Player> players;
private readonly List<Player> players = new();
public PlayerManager()
{
players = new();
}
/// <summary>
/// add a new player
/// </summary>
/// <param name="toAdd">player to be added</param>
/// <returns>added player</returns>
public Player Add(Player toAdd)
public Task<Player> Add(Player toAdd)
{
if (toAdd is null)
{
@ -31,25 +29,27 @@ namespace Model.Players
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
players.Add(toAdd);
return toAdd;
return Task.FromResult(toAdd);
}
/// <summary>
/// finds the player with that name and returns A COPY OF IT
/// <br/>
/// that copy does not belong to this manager's players, so it should not be modified
/// finds the player with that name and returns it
/// </summary>
/// <param name="name">a player's unique name</param>
/// <returns>player with said name, <em>or null</em> if no such player was found</returns>
public Player GetOneByName(string name)
public Task<Player> GetOneByName(string name)
{
if (!string.IsNullOrWhiteSpace(name))
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("param should not be null or blank", nameof(name));
}
Player result = players.FirstOrDefault(p => p.Name.ToUpper().Equals(name.ToUpper().Trim()));
if (result == null)
{
Player wanted = new(name);
Player result = players.FirstOrDefault(p => p.Equals(wanted));
return result is null ? null : new Player(result); // THIS IS A COPY (using a copy constructor)
return Task.FromResult<Player>(null);
}
throw new ArgumentException("param should not be null or blank", nameof(name));
return Task.FromResult(result);
}
/// </summary>
@ -57,7 +57,7 @@ namespace Model.Players
/// so that the only way to modify the collection of players is to use this class's methods
/// </summary>
/// <returns>a readonly enumerable of all this manager's players</returns>
public IEnumerable<Player> GetAll() => players.AsEnumerable();
public Task<ReadOnlyCollection<Player>> GetAll() => Task.FromResult(new ReadOnlyCollection<Player>(players));
/// <summary>
/// update a player from <paramref name="before"/> to <paramref name="after"/>
@ -65,7 +65,7 @@ namespace Model.Players
/// <param name="before">player to be updated</param>
/// <param name="after">player in the state that it needs to be in after the update</param>
/// <returns>updated player</returns>
public Player Update(Player before, Player after)
public Task<Player> Update(Player before, Player after)
{
Player[] args = { before, after };
@ -96,7 +96,7 @@ namespace Model.Players
players.Remove(toRemove);
}
public Player GetOneByID(Guid ID)
public Task<Player> GetOneByID(Guid ID)
{
throw new NotImplementedException();
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tests.Data_UTs
{
public class DiceAppDbContextTest
{
}
}

@ -0,0 +1,49 @@
using Data.EF;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Data_UTs
{
public class DiceAppDbContextWithStubTest
{
private readonly SqliteConnection connection = new("DataSource=:memory:");
private readonly DbContextOptions<DiceAppDbContext> options;
public DiceAppDbContextWithStubTest()
{
connection.Open();
options = new DbContextOptionsBuilder<DiceAppDbContext>()
.UseSqlite(connection)
.EnableSensitiveDataLogging()
.Options;
}
[Theory]
[InlineData("Alice")]
[InlineData("Bob")]
[InlineData("Clyde")]
[InlineData("Dahlia")]
public void TestDbStubContainsAll(string name)
{
// Arrange
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
// Assert
Assert.True(db.PlayerEntity.Where(p => p.Name.Equals(name)).Any());
}
}
}
}

@ -1,12 +1,407 @@
using System;
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Xunit;
namespace Tests.Data_UTs.Games
{
public class TurnEntityTest
{
private readonly DieEntity numDie;
private readonly FaceEntity numFace1 = new NumberFaceEntity() { ID = Guid.NewGuid(), Value = 7 };
private readonly FaceEntity numFace2 = new NumberFaceEntity() { ID = Guid.NewGuid(), Value = 8 };
private readonly DieEntity clrDie;
private readonly FaceEntity clrFace1 = new ColorFaceEntity() { ID = Guid.NewGuid(), A = 255, R = 255, G = 255, B = 255 };
private readonly FaceEntity clrFace2 = new ColorFaceEntity() { ID = Guid.NewGuid() };
private readonly DieEntity imgDie;
private readonly FaceEntity imgFace1 = new ImageFaceEntity() { ID = Guid.NewGuid(), Value = "https://a" };
private readonly FaceEntity imgFace2 = new ImageFaceEntity() { ID = Guid.NewGuid(), Value = "https://b" };
private readonly PlayerEntity player1 = new() { ID = Guid.NewGuid(), Name = "Marvin" };
private readonly PlayerEntity player2 = new() { ID = Guid.NewGuid(), Name = "Barbara" };
private readonly DateTime datetime1 = new(2020, 6, 15, 12, 15, 3, DateTimeKind.Utc);
private readonly DateTime datetime2 = new(2016, 12, 13, 14, 15, 16, DateTimeKind.Utc);
private readonly DieTurn dieTurn1;
private readonly DieTurn dieTurn2;
private readonly DieTurn dieTurn3;
private readonly DieTurn dieTurn4;
private readonly DieTurn dieTurn5;
private readonly FaceTurn faceTurn1;
private readonly FaceTurn faceTurn2;
private readonly FaceTurn faceTurn3;
private readonly FaceTurn faceTurn4;
private readonly FaceTurn faceTurn5;
private readonly TurnEntity turn1;
private readonly TurnEntity turn2;
private readonly TurnEntity turn3;
public TurnEntityTest()
{
numDie = new NumberDieEntity() { ID = Guid.NewGuid(), Faces = new List<NumberFaceEntity>() { numFace1 as NumberFaceEntity, numFace2 as NumberFaceEntity } };
(numFace1 as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDie;
(numFace2 as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDie;
(clrFace2 as ColorFaceEntity).SetValue(Color.FromName("blue"));
clrDie = new ColorDieEntity() { ID = Guid.NewGuid(), Faces = new List<ColorFaceEntity>() { clrFace1 as ColorFaceEntity, clrFace2 as ColorFaceEntity } };
(clrFace1 as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDie;
(clrFace2 as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDie;
imgDie = new ImageDieEntity() { ID = Guid.NewGuid(), Faces = new List<ImageFaceEntity>() { imgFace1 as ImageFaceEntity, imgFace2 as ImageFaceEntity } };
(imgFace1 as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDie;
(imgFace2 as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDie;
turn1 = new()
{
ID = Guid.NewGuid(),
When = datetime1,
PlayerEntity = player1,
PlayerEntityID = player1.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie,
imgDie
},
Faces = new List<FaceEntity>
{
numFace1,
clrFace2,
imgFace2
},
};
dieTurn1 = new() { DieEntityID = numDie.ID, DieEntity = numDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
dieTurn2 = new() { DieEntityID = clrDie.ID, DieEntity = clrDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
dieTurn3 = new() { DieEntityID = imgDie.ID, DieEntity = imgDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
turn1.DieTurns = new() { dieTurn1, dieTurn2, dieTurn3 };
numDie.DieTurns = new() { dieTurn1 };
clrDie.DieTurns = new() { dieTurn2 };
imgDie.DieTurns = new() { dieTurn3 };
faceTurn1 = new() { FaceEntityID = numFace1.ID, FaceEntity = numFace1, TurnEntityID = turn1.ID, TurnEntity = turn1 };
faceTurn2 = new() { FaceEntityID = clrFace2.ID, FaceEntity = clrFace2, TurnEntityID = turn1.ID, TurnEntity = turn1 };
faceTurn3 = new() { FaceEntityID = imgFace2.ID, FaceEntity = imgFace2, TurnEntityID = turn1.ID, TurnEntity = turn1 };
turn1.FaceTurns = new() { faceTurn1, faceTurn2, faceTurn3 };
numFace1.FaceTurns = new() { faceTurn1 };
clrFace2.FaceTurns = new() { faceTurn2 };
imgFace2.FaceTurns = new() { faceTurn3 };
Guid turn2ID = Guid.NewGuid();
turn2 = new()
{
ID = turn2ID,
When = datetime2,
PlayerEntity = player2,
PlayerEntityID = player2.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie
},
Faces = new List<FaceEntity>
{
numFace2,
clrFace2
},
};
dieTurn4 = new() { DieEntityID = numDie.ID, DieEntity = numDie, TurnEntityID = turn2.ID, TurnEntity = turn2 };
dieTurn5 = new() { DieEntityID = clrDie.ID, DieEntity = clrDie, TurnEntityID = turn2.ID, TurnEntity = turn2 };
turn2.DieTurns = new() { dieTurn4, dieTurn5 };
numDie.DieTurns = new() { dieTurn4 };
clrDie.DieTurns = new() { dieTurn5 };
faceTurn4 = new() { FaceEntityID = numFace2.ID, FaceEntity = numFace2, TurnEntityID = turn2.ID, TurnEntity = turn2 };
faceTurn5 = new() { FaceEntityID = clrFace2.ID, FaceEntity = clrFace2, TurnEntityID = turn2.ID, TurnEntity = turn2 };
turn2.FaceTurns = new() { faceTurn4, faceTurn5 };
numFace2.FaceTurns = new() { faceTurn4 };
clrFace2.FaceTurns = new() { faceTurn5 };
turn3 = new()
{
ID = turn2ID,
When = datetime2,
PlayerEntity = player2,
PlayerEntityID = player2.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie
},
Faces = new List<FaceEntity>
{
numFace2,
clrFace2
},
};
}
[Fact]
public void TestGetSetID()
{
// Arrange
TurnEntity turn = new();
Guid expected = Guid.NewGuid();
// Act
turn.ID = expected;
Guid actual = turn.ID;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetWhen()
{
// Arrange
TurnEntity turn = new();
DateTime expected = datetime1;
// Act
turn.When = expected;
DateTime actual = turn.When;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetPlayer()
{
// Arrange
TurnEntity turn = new();
PlayerEntity expected = player1;
// Act
turn.PlayerEntity = expected;
PlayerEntity actual = turn.PlayerEntity;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetPlayerID()
{
// Arrange
TurnEntity turn = new();
Guid expected = player1.ID;
// Act
turn.PlayerEntityID = expected;
Guid actual = turn.PlayerEntityID;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetDice()
{
// Arrange
TurnEntity turn = new();
ICollection<DieEntity> expected = new List<DieEntity>
{
numDie,
clrDie,
imgDie
};
// Act
turn.Dice = expected;
ICollection<DieEntity> actual = turn.Dice;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetDieTurns()
{
// Arrange
TurnEntity turn = new();
List<DieTurn> expected = new() { dieTurn1, dieTurn2, dieTurn3 };
// Act
turn.DieTurns = expected;
List<DieTurn> actual = turn.DieTurns;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetFaces()
{
// Arrange
TurnEntity turn = new();
ICollection<FaceEntity> expected = new List<FaceEntity>
{
numFace1,
clrFace1,
imgFace1
};
// Act
turn.Faces = expected;
ICollection<FaceEntity> actual = turn.Faces;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetFaceTurns()
{
// Arrange
TurnEntity turn = new();
List<FaceTurn> expected = new() { faceTurn1, faceTurn2 };
// Act
turn.FaceTurns = expected;
List<FaceTurn> actual = turn.FaceTurns;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestEqualsWhenNotTurnEntityThenFalse()
{
// Arrange
Point point;
TurnEntity entity;
// Act
point = new(1, 2);
entity = turn1;
// Assert
Assert.False(point.Equals(entity));
Assert.False(entity.Equals(point));
}
[Fact]
public void TestEqualsWhenNullThenFalse()
{
// Arrange
TurnEntity entity;
// Act
entity = turn2;
// Assert
Assert.False(entity.Equals(null));
}
[Fact]
public void TestGoesThruToSecondMethodIfObjIsTypeTurnEntity()
{
// Arrange
object t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.Equals(t2));
Assert.False(t2.Equals(t1));
}
[Fact]
public void TestEqualsFalseIfNotSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.Equals(t2));
Assert.False(t2.Equals(t1));
}
[Fact]
public void TestEqualsTrueIfSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn2;
t2 = turn3; // turns 2 and 3 should be same as far as Equals is concerned
// Assert
Assert.True(t1.Equals(t2));
Assert.True(t2.Equals(t1));
}
[Fact]
public void TestSameHashFalseIfNotSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.GetHashCode().Equals(t2.GetHashCode()));
Assert.False(t2.GetHashCode().Equals(t1.GetHashCode()));
}
[Fact]
public void TestSameHashTrueIfSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn2;
t2 = turn3;
// Assert
Assert.True(t1.GetHashCode().Equals(t2.GetHashCode()));
Assert.True(t2.GetHashCode().Equals(t1.GetHashCode()));
}
}
}

@ -1,12 +1,274 @@
using System;
using Data.EF.Dice.Faces;
using Data.EF.Dice;
using Data.EF.Players;
using Model.Dice;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Data.EF.Games;
using System.Drawing;
using Model.Games;
using Newtonsoft.Json.Linq;
using Model.Dice.Faces;
using System.Diagnostics;
namespace Tests.Data_UTs.Games
{
public class TurnExtensionsTest
{
private readonly DateTime datetime1 = new(2013, 2, 1, 1, 19, 4, DateTimeKind.Utc);
private readonly DateTime datetime2 = new(2014, 8, 1, 23, 12, 4, DateTimeKind.Utc);
private readonly PlayerEntity playerEntity = new() { Name = "Paula" };
private readonly DieEntity numDieEntity;
private readonly FaceEntity numFace1Entity = new NumberFaceEntity() { Value = 7 };
private readonly FaceEntity numFace2Entity = new NumberFaceEntity() { Value = 8 };
private readonly DieEntity clrDieEntity;
private readonly FaceEntity clrFace1Entity = new ColorFaceEntity() { A = 255, R = 255, G = 255, B = 255 };
private readonly FaceEntity clrFace2Entity = new ColorFaceEntity() { A = 255, R = 0, G = 0, B = 128 };
private readonly DieEntity imgDieEntity;
private readonly FaceEntity imgFace1Entity = new ImageFaceEntity() { Value = "https://a" };
private readonly FaceEntity imgFace2Entity = new ImageFaceEntity() { Value = "https://b" };
public TurnExtensionsTest()
{
numDieEntity = new NumberDieEntity() { Faces = new List<NumberFaceEntity>() { numFace1Entity as NumberFaceEntity, numFace2Entity as NumberFaceEntity } };
(numFace1Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity;
(numFace2Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity;
clrDieEntity = new ColorDieEntity() { Faces = new List<ColorFaceEntity>() { clrFace1Entity as ColorFaceEntity, clrFace2Entity as ColorFaceEntity } };
(clrFace1Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity;
(clrFace2Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity;
imgDieEntity = new ImageDieEntity() { Faces = new List<ImageFaceEntity>() { imgFace1Entity as ImageFaceEntity, imgFace2Entity as ImageFaceEntity } };
(imgFace1Entity as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDieEntity;
(imgFace2Entity as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDieEntity;
}
[Fact]
public void TestToModel()
{
// Arrange
TurnEntity entity = new()
{
When = datetime1,
PlayerEntity = playerEntity,
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
numFace1Entity,
clrFace2Entity,
imgFace2Entity
}
};
Turn expected = Turn.CreateWithSpecifiedTime(
datetime1,
playerEntity.ToModel(),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace1Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace2Entity as ImageFaceEntity).ToModel() }
});
// Act
Turn actual = entity.ToModel();
// Assert
Assert.True(expected.Equals(actual));
}
[Fact]
public void TestToModels()
{
// Arrange
TurnEntity[] entities = new TurnEntity[]
{
new TurnEntity()
{
When = datetime1,
PlayerEntity = new PlayerEntity() {Name = "Aardvark"},
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity
}
},
new TurnEntity()
{
When = datetime2,
PlayerEntity = new PlayerEntity() {Name = "Chloe"},
Dice = new List<DieEntity>
{
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
clrFace1Entity,
imgFace1Entity
}
}
};
IEnumerable<Turn> expected = new Turn[]
{
Turn.CreateWithSpecifiedTime(
datetime1,
new("Aardvark"),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
}),
Turn.CreateWithSpecifiedTime(
datetime2,
new("Chloe"),
new Dictionary<Die, Face>()
{
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace1Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
})
}.AsEnumerable();
// Act
IEnumerable<Turn> actual = entities.ToModels();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestToEntity()
{
// Arrange
Turn model = Turn.CreateWithSpecifiedTime(
datetime1,
playerEntity.ToModel(),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
});
TurnEntity expected = new()
{
When = datetime1,
PlayerEntity = playerEntity,
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity,
imgFace1Entity
}
};
// Act
TurnEntity actual = model.ToEntity();
// Assert
Assert.True(expected.Equals(actual));
}
[Fact]
public void TestToEntities()
{
// Arrange
Turn[] models = new Turn[]
{
Turn.CreateWithSpecifiedTime(
datetime2,
new("Mimi"),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
}),
Turn.CreateWithSpecifiedTime(
datetime1,
new("blaaargh"),
new Dictionary<Die, Face>()
{
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace1Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
})
};
IEnumerable<TurnEntity> expected = new TurnEntity[]
{
new TurnEntity()
{
When = datetime2,
PlayerEntity = new PlayerEntity() {Name = "Mimi"},
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity
}
},
new TurnEntity()
{
When = datetime1,
PlayerEntity = new PlayerEntity() {Name = "blaaargh"},
Dice = new List<DieEntity>
{
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
clrFace1Entity,
imgFace1Entity
}
}
}.AsEnumerable();
// Act
IEnumerable<TurnEntity> actual = models.ToEntities();
// Assert
Assert.Equal(expected, actual);
}
}
}

@ -2,17 +2,11 @@
using Data.EF.Players;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
namespace Tests.Data_UTs.Players
{
@ -33,32 +27,8 @@ namespace Tests.Data_UTs.Players
.Options;
}
[Theory]
[InlineData("Alice")]
[InlineData("Bob")]
[InlineData("Clyde")]
[InlineData("Dahlia")]
public void TestDbStubContainsAll(string name)
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
// Assert
Assert.True(mgr.IsPresentByName(name));
}
}
[Fact]
public void TestConstructorWhenGivenContextThenConstructs()
public async Task TestConstructorWhenGivenContextThenConstructs()
{
// Arrange
@ -72,7 +42,7 @@ namespace Tests.Data_UTs.Players
// Assert
Assert.Equal(new Collection<PlayerEntity>(), mgr.GetAll());
Assert.Equal(new Collection<PlayerEntity>(), await mgr.GetAll());
}
}
@ -93,7 +63,7 @@ namespace Tests.Data_UTs.Players
}
[Fact]
public void TestAddWhenValidThenValid()
public async Task TestAddWhenValidThenValid()
{
// Arrange
@ -107,8 +77,8 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new PlayerEntity() { Name = expectedName });
mgr.Add(new PlayerEntity() { Name = "whatever" });
await mgr.Add(new PlayerEntity() { Name = expectedName });
await mgr.Add(new PlayerEntity() { Name = "whatever" });
// mgr takes care of the SaveChange() calls internally
// we might use Units of Work later, to optimize our calls to DB
}
@ -119,14 +89,14 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.Equal(expectedName, mgr.GetOneByName(expectedName).Name);
Assert.Equal(expectedCount, mgr.GetAll().Count());
Assert.Equal(expectedName, (await mgr.GetOneByName(expectedName)).Name);
Assert.Equal(expectedCount, (await mgr.GetAll()).Count);
}
}
[Fact]
public void TestAddWhenPreExistentThenException()
public async Task TestAddWhenPreExistentThenException()
{
// Arrange
@ -140,12 +110,12 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new PlayerEntity() { Name = name });
void action() => mgr.Add(new PlayerEntity() { Name = name });
await mgr.Add(new PlayerEntity() { Name = name });
async Task actionAsync() => await mgr.Add(new PlayerEntity() { Name = name });
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
}
@ -164,11 +134,11 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
void action() => mgr.Add(null);
async Task actionAsync() => await mgr.Add(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
Assert.ThrowsAsync<ArgumentNullException>(actionAsync);
}
}
@ -190,11 +160,11 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
void action() => mgr.Add(new PlayerEntity() { Name = name });
async Task actionAsync() => await mgr.Add(new PlayerEntity() { Name = name });
// Assert
Assert.Throws<ArgumentException>(action);
Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
}
@ -202,7 +172,7 @@ namespace Tests.Data_UTs.Players
[InlineData(" ")]
[InlineData(null)]
[InlineData("")]
public void TestGetOneByNameWhenInvalidThenException(string name)
public async Task TestGetOneByNameWhenInvalidThenException(string name)
{
// Arrange
@ -215,14 +185,14 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Ernesto" });
mgr.Add(new() { Name = "Basil" });
await mgr.Add(new() { Name = "Ernesto" });
await mgr.Add(new() { Name = "Basil" });
void action() => mgr.GetOneByName(name);
async Task actionAsync() => await mgr.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
}
@ -230,7 +200,7 @@ namespace Tests.Data_UTs.Players
[InlineData("Caroline")]
[InlineData("Caroline ")]
[InlineData(" Caroline")]
public void TestGetOneByNameWhenValidAndExistsThenGetsIt(string name)
public async Task TestGetOneByNameWhenValidAndExistsThenGetsIt(string name)
{
// Arrange
@ -246,8 +216,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
mgr.Add(new() { Name = "Philip" });
await mgr.Add(expected);
await mgr.Add(new() { Name = "Philip" });
}
// Assert
@ -256,13 +226,13 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
mgr = new(db);
actual = mgr.GetOneByName(name);
actual = await mgr.GetOneByName(name);
Assert.Equal(expected, actual);
}
}
[Fact]
public void TestGetOneByNameWhenValidAndNotExistsThenException()
public async Task TestGetOneByNameWhenValidAndNotExistsThenException()
{
// Arrange
@ -276,19 +246,19 @@ namespace Tests.Data_UTs.Players
mgr = new(db);
//mgr.Add(expected);
mgr.Add(new() { Name = "Brett" });
mgr.Add(new() { Name = "Noah" });
await mgr.Add(new() { Name = "Brett" });
await mgr.Add(new() { Name = "Noah" });
void action() => mgr.GetOneByName("*r^a*éàru é^à");
async Task actionAsync() => await mgr.GetOneByName("*r^a*éàru é^à");
// Assert
Assert.Throws<InvalidOperationException>(action);
await Assert.ThrowsAsync<InvalidOperationException>(actionAsync);
}
}
[Fact]
public void TestIsPresentByNameWhenValidAndExistsThenTrue()
public async Task TestIsPresentByNameWhenValidAndExistsThenTrue()
{
// Arrange
@ -302,8 +272,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Philip" });
mgr.Add(new() { Name = name });
await mgr.Add(new() { Name = "Philip" });
await mgr.Add(new() { Name = name });
}
// Assert
@ -313,7 +283,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.True(mgr.IsPresentByName(name));
Assert.True(await mgr.IsPresentByName(name));
}
}
@ -321,8 +291,8 @@ namespace Tests.Data_UTs.Players
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
[InlineData("Barbara")]
public void TestIsPresentByNameWhenInvalidOrNonExistentThenFalse(string name)
[InlineData("nowaythatthisnameisalreadyinourdatabase")]
public async Task TestIsPresentByNameWhenInvalidOrNonExistentThenFalse(string name)
{
// Arrange
@ -335,8 +305,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Herman" });
mgr.Add(new() { Name = "Paulo" });
await mgr.Add(new() { Name = "Herman" });
await mgr.Add(new() { Name = "Paulo" });
}
// Assert
@ -346,7 +316,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(mgr.IsPresentByName(name));
Assert.False(await mgr.IsPresentByName(name));
}
}
@ -373,52 +343,43 @@ namespace Tests.Data_UTs.Players
}
[Fact]
public void TestRemoveWhenPreExistentThenRemoves()
public async Task TestRemoveWhenPreExistentThenRemoves()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity toRemove = new() { Name = "Filibert" };
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
PlayerEntity toRemove = new() { ID = Guid.NewGuid(), Name = "Please!" };
mgr.Add(new() { Name = "Xavier" });
mgr.Add(toRemove);
}
// Act
using (DiceAppDbContext db = new(options))
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
await mgr.Add(toRemove); // calls SaveChangesAsync()
mgr.Remove(toRemove);
}
// Assert
using (DiceAppDbContext db = new(options))
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(toRemove, mgr.GetAll());
Assert.DoesNotContain(toRemove, db.PlayerEntity);
}
}
[Fact]
public void TestRemoveWhenNonExistentThenStillNonExistent()
public async Task TestRemoveWhenNonExistentThenStillNonExistent()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity toRemove = new() { Name = "Filibert" };
PlayerEntity toRemove = new() { ID = Guid.NewGuid(), Name = "Filibert" };
// Act
@ -427,7 +388,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Bert" });
await mgr.Add(new() { ID = Guid.NewGuid(), Name = "Bert" });
mgr.Remove(toRemove);
}
@ -438,14 +399,14 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(toRemove, mgr.GetAll());
Assert.DoesNotContain(toRemove, await mgr.GetAll());
}
}
[Theory]
[InlineData("filiBert")]
[InlineData("Bertrand")]
public void TestUpdateWhenValidThenUpdates(string name)
public async Task TestUpdateWhenValidThenUpdates(string name)
{
// Arrange
@ -462,8 +423,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
mgr.Update(before, after);
await mgr.Add(before);
await mgr.Update(before, after);
}
// Assert
@ -473,8 +434,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(before, mgr.GetAll());
Assert.Contains(after, mgr.GetAll());
Assert.DoesNotContain(before, await mgr.GetAll());
Assert.Contains(after, await mgr.GetAll());
}
}
@ -482,7 +443,7 @@ namespace Tests.Data_UTs.Players
[InlineData("Valerie")]
[InlineData("Valerie ")]
[InlineData(" Valerie")]
public void TestUpdateWhenSameThenKeepsAndWorks(string name)
public async Task TestUpdateWhenSameThenKeepsAndWorks(string name)
{
// Arrange
@ -500,8 +461,8 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
mgr.Update(before, after);
await mgr.Add(before);
await mgr.Update(before, after);
}
// Assert
@ -511,14 +472,14 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.Contains(before, mgr.GetAll());
Assert.Contains(after, mgr.GetAll());
Assert.Contains(before, await mgr.GetAll());
Assert.Contains(after, await mgr.GetAll());
}
}
[Fact]
public void TestUpdateWhenNewIDThenException()
public async Task TestUpdateWhenNewIDThenException()
{
// Arrange
@ -534,12 +495,12 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, after);
await mgr.Add(before);
async Task actionAsync() => await mgr.Update(before, after);
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
}
@ -547,7 +508,7 @@ namespace Tests.Data_UTs.Players
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateWhenInvalidThenException(string name)
public async Task TestUpdateWhenInvalidThenException(string name)
{
// Arrange
@ -564,17 +525,17 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, after);
await mgr.Add(before);
async Task actionAsync() => await mgr.Update(before, after);
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
}
[Fact]
public void TestUpdateWhenNullThenException()
public async Task TestUpdateWhenNullThenException()
{
// Arrange
@ -589,17 +550,17 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, null);
await mgr.Add(before);
async Task actionAsync() => await mgr.Update(before, null);
// Assert
Assert.Throws<ArgumentNullException>(action);
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync);
}
}
[Fact]
public void TestGetOneByIDWhenExistsThenGetsIt()
public async Task TestGetOneByIDWhenExistsThenGetsIt()
{
// Arrange
@ -616,7 +577,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
await mgr.Add(expected);
}
// Assert
@ -625,13 +586,13 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
mgr = new(db);
actual = mgr.GetOneByID(id);
actual = await mgr.GetOneByID(id);
Assert.Equal(expected, actual);
}
}
[Fact]
public void TestGetOneByIDWhenNotExistsThenException()
public async Task TestGetOneByIDWhenNotExistsThenExceptionAsync()
{
// Arrange
@ -649,18 +610,18 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
await mgr.Add(expected);
void action() => mgr.GetOneByID(otherId);
async Task actionAsync() => await mgr.GetOneByID(otherId);
// Assert
Assert.Throws<InvalidOperationException>(action);
await Assert.ThrowsAsync<InvalidOperationException>(actionAsync);
}
}
[Fact]
public void TestIsPresentbyIdWhenExistsThenTrue()
public async Task TestIsPresentbyIdWhenExistsThenTrue()
{
// Arrange
@ -674,7 +635,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { ID = id, Name = "Bobby" });
await mgr.Add(new() { ID = id, Name = "Bobby" });
}
// Assert
@ -684,19 +645,20 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.True(mgr.IsPresentByID(id));
Assert.True(await mgr.IsPresentByID(id));
}
}
[Fact]
public void TestIsPresentbyIdWhenExistsThenFalse()
public async Task TestIsPresentbyIdWhenNotExistsThenFalse()
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
Guid otherId;
Guid otherId = Guid.NewGuid();
PlayerEntity presentEntity;
// Act
@ -705,7 +667,11 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { ID = id, Name = "Victor" });
presentEntity = new() { ID = id, Name = "Victor" };
await mgr.Add(presentEntity);
otherId = Guid.NewGuid();
// not added
}
// Assert
@ -715,7 +681,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(mgr.IsPresentByID(otherId));
Assert.False(await mgr.IsPresentByID(otherId));
}
}
}

@ -1,6 +1,5 @@
using Data.EF.Players;
using System;
using Tests.Model_UTs;
using Xunit;
namespace Tests.Data_UTs.Players

@ -1,12 +1,51 @@
using System;
using Model.Dice.Faces;
using Model.Dice;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Xunit;
namespace Tests.Model_UTs.Dice
{
public class ColorDieTest
{
public static IEnumerable<object[]> Data_Uri()
{
yield return new object[]
{
new ColorFace(Color.FromName("Chocolate")),
new ColorFace(Color.FromName("Aqua")),
new ColorFace(Color.FromName("Beige")),
new ColorFace(Color.FromName("Black")),
new ColorFace(Color.FromName("BurlyWood")),
};
}
[Theory]
[MemberData(nameof(Data_Uri))]
public void RndmFaceTest(ColorFace f1, ColorFace f2, ColorFace f3, ColorFace f4, ColorFace f5)
{
//Arrange
List<ColorFace> listFaces = new() {
f1,f2,f3,f4,f5
};
ColorDie die = new(
listFaces[1],
listFaces[2],
listFaces[3],
listFaces[4]
);
//Act
ColorFace actual = (ColorFace)die.GetRandomFace();
//Assert
Assert.Contains(listFaces, face => face == actual);
}
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tests.Model_UTs.Dice
{
public class DiceGroupManagerTest
{
}
}

@ -1,12 +1,70 @@
using System;
using Model.Dice.Faces;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Xunit;
namespace Tests.Model_UTs.Dice.Faces
{
public class ColorFaceTest
{
public static IEnumerable<object[]> Data_Colors()
{
yield return new object[]
{
Color.FromName("Chocolate"),
Color.FromArgb(144, 255, 78, 240),
};
yield return new object[]
{
Color.FromName("Chocolate"),
Color.FromArgb(144, 255, 78, 240),
};
}
[Theory]
[MemberData(nameof(Data_Colors))]
public void ColorFaceValueTest(Color clrA, Color clrB)
{
//Arrage
ColorFace face1 = new(clrA);
ColorFace face2 = new(clrB);
//Act
Color expected1 = clrA;
Color actual1 = face1.Value;
Color expected2 = clrB;
Color actual2 = face2.Value;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
[Theory]
[MemberData(nameof(Data_Colors))]
public void ColorFaceValueToStringTest(Color clrA, Color clrB)
{
//Arrage
ColorFace face1 = new(clrA);
ColorFace face2 = new(clrB);
//Act
string expected1 = clrA.ToString();
string actual1 = face1.StringValue;
string expected2 = clrB.ToString();
string actual2 = face2.StringValue;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
}
}

@ -1,12 +1,70 @@
using System;
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Model_UTs.Dice.Faces
{
public class ImageFaceTest
{
public static IEnumerable<object[]> Data_Colors()
{
yield return new object[]
{
new Uri("http://www.contoso.com/"),
new Uri("https://www.pedagojeux.fr/wp-content/uploads/2019/11/1280x720_LoL.jpg"),
};
yield return new object[]
{
new Uri("https://www.lacremedugaming.fr/wp-content/uploads/creme-gaming/2022/02/media-13411.jpg"),
new Uri("https://i1.moyens.net/io/images/2022/01/1642321015_Mises-a-jour-de-VALORANT-en-melee-a-venir-dans.jpg"),
};
}
[Theory]
[MemberData(nameof(Data_Colors))]
public void ImageFaceValueTest(Uri uriA, Uri uriB)
{
//Arrage
ImageFace face1 = new(uriA);
ImageFace face2 = new(uriB);
//Act
Uri expected1 = uriA;
Uri actual1 = face1.Value;
Uri expected2 = uriB;
Uri actual2 = face2.Value;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
[Theory]
[MemberData(nameof(Data_Colors))]
public void ImageFaceValueToStringTest(Uri uriA, Uri uriB)
{
//Arrage
ImageFace face1 = new(uriA);
ImageFace face2 = new(uriB);
//Act
string expected1 = uriA.ToString();
string actual1 = face1.StringValue;
string expected2 = uriB.ToString();
string actual2 = face2.StringValue;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
}
}

@ -1,12 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Dice.Faces;
using Xunit;
namespace Tests.Model_UTs.Dice.Faces
{
public class NumberFaceTest
{
[Fact]
public void NumberFaceValueTest()
{
//Arrage
NumberFace face1 = new NumberFace(3);
NumberFace face2 = new NumberFace(5);
//Act
int expected1 = 3;
int actual1 = face1.Value;
int expected2 = 5;
int actual2 = face2.Value;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
[Fact]
public void NumberFaceValueToStringTest()
{
//Arrage
NumberFace face1 = new(3);
NumberFace face2 = new(5);
//Act
string expected1 = 3.ToString();
string actual1 = face1.StringValue;
string expected2 = 5.ToString();
string actual2 = face2.StringValue;
//Assert
Assert.Equal(expected1, actual1);
Assert.Equal(expected2, actual2);
}
}
}

@ -1,12 +1,53 @@
using System;
using Model.Dice.Faces;
using Model.Dice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Model_UTs.Dice
{
public class ImageDieTest
{
public static IEnumerable<object[]> Data_Uri()
{
yield return new object[]
{
new ImageFace(new Uri("https://nothing1/")),
new ImageFace(new Uri("https://nothing2/")),
new ImageFace(new Uri("https://nothing3/")),
new ImageFace(new Uri("https://nothing4/")),
new ImageFace(new Uri("https://nothing5/")),
};
}
[Theory]
[MemberData(nameof(Data_Uri))]
public void RndmFaceTest(ImageFace f1, ImageFace f2, ImageFace f3, ImageFace f4, ImageFace f5)
{
//Arrange
List<ImageFace> listFaces = new() {
f1,f2,f3,f4,f5
};
ImageDie die = new(
listFaces[1],
listFaces[2],
listFaces[3],
listFaces[4]
);
//Act
ImageFace actual = (ImageFace)die.GetRandomFace();
//Assert
Assert.Contains(listFaces, face => face == actual);
}
}
}

@ -1,5 +1,6 @@
using Model.Dice;
using Model.Dice.Faces;
using System.Collections.Generic;
using Xunit;
namespace Tests.Model_UTs.Dice
@ -8,20 +9,33 @@ namespace Tests.Model_UTs.Dice
{
[Fact]
public void TestGetRandomFace()
public void RndmFaceTest()
{
// Arrange
int val1 = 1, val2 = 2;
NumberDie die = new(new NumberFace(val1), new NumberFace(val2));
//Arrange
List<NumberFace> listFaces = new() {
new NumberFace(1),
new NumberFace(2),
new NumberFace(3),
new NumberFace(4),
new NumberFace(5),
};
NumberDie die = new(
listFaces[1],
listFaces[2],
listFaces[3],
listFaces[4]
);
// Act
Face<int> thing = die.GetRandomFace();
//Act
NumberFace actual = (NumberFace)die.GetRandomFace();
//Assert
Assert.Contains(listFaces, face => face == actual);
// Assert
Assert.IsType<NumberFace>(thing);
Assert.True(thing.Value == val1 || thing.Value == val2);
}
}
}

@ -9,44 +9,45 @@ using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tests.Data_UTs.Games;
using Xunit;
namespace Tests.Model_UTs.Games
{
public class GameManagerTest
{
private readonly MasterOfCeremonies stubGameRunner = new Stub().LoadApp();
private readonly MasterOfCeremonies stubGameRunner = new Stub().LoadApp()?.Result;
[Fact]
public void TestConstructorReturnsEmptyEnumerable()
public async Task TestConstructorReturnsEmptyEnumerableAsync()
{
// Arrange
GameManager gm = new();
IEnumerable<Game> expected;
IEnumerable<Game> actual;
// Act
// Act
expected = new Collection<Game>();
actual = gm.GetAll();
actual = await gm.GetAll();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestAddWhenGamesThenDoAddAndReturnGames()
public async Task TestAddWhenGamesThenDoAddAndReturnGamesAsync()
{
// Arrange
GameManager gm = new();
Game game1 = stubGameRunner.GameManager.GetAll().First();
Game game2 = stubGameRunner.GameManager.GetAll().Last();
Game game1 = (await stubGameRunner.GameManager.GetAll()).First();
Game game2 = (await stubGameRunner.GameManager.GetAll()).Last();
// Act
IEnumerable<Game> expected = new List<Game>() { game1, game2 }.AsEnumerable();
IEnumerable<Game> actual = new List<Game>()
{
gm.Add(game1),
gm.Add(game2)
await gm.Add(game1),
await gm.Add(game2)
};
// Assert
@ -54,17 +55,17 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestAddWhenNullThenThrowsException()
public async Task TestAddWhenNullThenThrowsException()
{
// Arrange
GameManager gm = new();
// Act
void action() => gm.Add(null);// Add() returns the added element if succesful
async Task actionAsync() => await gm.Add(null);// Add() returns the added element if succesful
// Assert
Assert.Throws<ArgumentNullException>(action);
Assert.DoesNotContain(null, stubGameRunner.GameManager.GetAll());
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync);
Assert.DoesNotContain(null, await stubGameRunner.GameManager.GetAll());
}
[Fact]
@ -85,40 +86,40 @@ namespace Tests.Model_UTs.Games
[InlineData("")]
[InlineData(null)]
[InlineData(" ")]
public void TestGetOneByNameWhenInvalidThenThrowsException(string name)
public async Task TestGetOneByNameWhenInvalidThenThrowsExceptionAsync(string name)
{
// Arrange
GameManager gm = new();
// Act
void action() => gm.GetOneByName(name);
async Task actionAsync() => await gm.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
[Fact]
public void TestGetOneByNameWhenValidButNotExistThenReturnNull()
public async Task TestGetOneByNameWhenValidButNotExistThenReturnNullAsync()
{
// Arrange
GameManager gm = new();
// Act
Game result = gm.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists");
Game result = await gm.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists");
// Assert
Assert.Null(result);
}
[Fact]
public void TestGetOneByNameWhenValidThenReturnGame()
public async Task TestGetOneByNameWhenValidThenReturnGameAsync()
{
// Arrange
GameManager gm = new();
Game game = stubGameRunner.GameManager.GetAll().First();
Game game = (await stubGameRunner.GameManager.GetAll()).First();
// Act
Game actual = gm.Add(game);
Game actual = await gm.Add(game);
Game expected = game;
// Assert
@ -126,19 +127,19 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestWhenRemoveExistsThenSucceeds()
public async Task TestWhenRemoveExistsThenSucceeds()
{
// Arrange
GameManager gm = new();
Game game = new("blargh", new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice);
gm.Add(game);
Game game = new("blargh", new PlayerManager(), (await stubGameRunner.GameManager.GetAll()).First().Dice);
await gm.Add(game);
// Act
gm.Remove(game);
// Assert
Assert.DoesNotContain(game, gm.GetAll());
Assert.DoesNotContain(game, await gm.GetAll());
}
[Fact]
@ -155,46 +156,46 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestRemoveWhenGivenNonExistentThenFailsSilently()
public async Task TestRemoveWhenGivenNonExistentThenFailsSilentlyAsync()
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
Game notGame = new("blargh", new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice);
IEnumerable<Game> expected = stubGameRunner.GameManager.GetAll();
Game notGame = new("blargh", new PlayerManager(), (await stubGameRunner.GameManager.GetAll()).First().Dice);
IEnumerable<Game> expected = await stubGameRunner.GameManager.GetAll();
// Act
gm.Remove(notGame);
IEnumerable<Game> actual = gm.GetAll();
IEnumerable<Game> actual = await gm.GetAll();
// Assert
Assert.Equal(actual, expected);
}
[Fact]
public void TestUpdateWhenValidThenSucceeds()
public async Task TestUpdateWhenValidThenSucceeds()
{
// Arrange
GameManager gm = new();
string oldName = "blargh";
string newName = "blargh2.0";
Game game = new(oldName, new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice);
game.PlayerManager.Add(new("Alice"));
gm.Add(game);
Game game = new(oldName, new PlayerManager(), (await stubGameRunner.GameManager.GetAll()).First().Dice);
await game.PlayerManager.Add(new("Alice"));
await gm.Add(game);
Game oldGame = gm.GetAll().First();
Game oldGame = (await gm.GetAll()).First();
Game newGame = new(newName, oldGame.PlayerManager, oldGame.Dice);
// Act
int expectedSize = gm.GetAll().Count();
gm.Update(oldGame, newGame);
int actualSize = gm.GetAll().Count();
int expectedSize = (await gm.GetAll()).Count;
await gm.Update(oldGame, newGame);
int actualSize = (await gm.GetAll()).Count;
// Assert
Assert.NotEqual(oldName, newName);
Assert.DoesNotContain(oldGame, gm.GetAll());
Assert.Contains(newGame, gm.GetAll());
Assert.DoesNotContain(oldGame, await gm.GetAll());
Assert.Contains(newGame, await gm.GetAll());
Assert.Equal(expectedSize, actualSize);
}
@ -202,57 +203,57 @@ namespace Tests.Model_UTs.Games
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGo(string badName)
public async Task TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGoAsync(string badName)
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = gm.GetAll().Count();
Game oldGame = gm.GetAll().First();
int expectedSize = (await gm.GetAll()).Count;
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice));
int actualSize = gm.GetAll().Count();
int actualSize = (await gm.GetAll()).Count;
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
Assert.Contains(oldGame, await gm.GetAll()); // still there
Assert.Equal(expectedSize, actualSize);
}
[Fact]
public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo()
public async Task TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGoAsync()
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = gm.GetAll().Count();
Game oldGame = gm.GetAll().First();
int expectedSize = (await gm.GetAll()).Count;
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(oldGame, null);
int actualSize = gm.GetAll().Count();
async Task actionAsync() => await gm.Update(oldGame, null);
int actualSize = (await gm.GetAll()).Count;
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by constructor
Assert.Contains(oldGame, await gm.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestUpdateDoesNotGoWithValidAfterAndNullBefore()
public async Task TestUpdateDoesNotGoWithValidAfterAndNullBefore()
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = gm.GetAll().Count();
Game oldGame = gm.GetAll().First();
int expectedSize = (await gm.GetAll()).Count;
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice));
int actualSize = gm.GetAll().Count();
async Task actionAsync() => await gm.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice));
int actualSize = (await gm.GetAll()).Count;
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by constructor
Assert.Contains(oldGame, await gm.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
@ -260,20 +261,20 @@ namespace Tests.Model_UTs.Games
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName)
public async Task TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGoAsync(string badName)
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = gm.GetAll().Count();
Game oldGame = gm.GetAll().First();
int expectedSize = (await gm.GetAll()).Count;
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice));
int actualSize = gm.GetAll().Count();
int actualSize = (await gm.GetAll()).Count;
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
Assert.Contains(oldGame, await gm.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}

@ -5,27 +5,26 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Model_UTs.Games
{
public class GameTest
{
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp();
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp()?.Result;
private static readonly string GAME_NAME = "my game";
private static readonly Player PLAYER_1 = new("Alice"), PLAYER_2 = new("Bob"), PLAYER_3 = new("Clyde");
private readonly IEnumerable<Die> DICE_1, DICE_2;
public GameTest()
{
DICE_1 = stubMasterOfCeremonies.DiceGroupManager.GetAll().First().Value;
DICE_2 = stubMasterOfCeremonies.DiceGroupManager.GetAll().Last().Value;
IEnumerable<DiceGroup> diceGroups = stubMasterOfCeremonies.DiceGroupManager.GetAll()?.Result;
DICE_1 = diceGroups.First().Dice;
DICE_2 = diceGroups.Last().Dice;
}
[Fact]
public void TestNamePropertyGet()
{
@ -78,13 +77,17 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestGetHistory()
public async Task TestGetHistory()
{
// Arrange
Dictionary<Die, Face> diceNFaces = (Dictionary<Die, Face>)stubMasterOfCeremonies.GameManager.GetAll().First().GetHistory().First().DiceNFaces;
IEnumerable<KeyValuePair<Die, Face>> diceNFaces =
(await stubMasterOfCeremonies.GameManager.GetAll())
.First()
.GetHistory()
.First().DiceNFaces;
Turn turn1 = Turn.CreateWithDefaultTime(PLAYER_1, diceNFaces);
Turn turn2 = Turn.CreateWithDefaultTime(PLAYER_2, diceNFaces); // yeah they rolled the same
Turn turn1 = Turn.CreateWithSpecifiedTime(new(1, 2, 3), PLAYER_1, diceNFaces);
Turn turn2 = Turn.CreateWithSpecifiedTime(new(1, 2, 3), PLAYER_2, diceNFaces); // yeah they rolled the same
IEnumerable<Turn> expected = new List<Turn>() { turn1, turn2 };
@ -118,30 +121,28 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestPerformTurnDoesAddOneTurn()
public async Task TestPerformTurnDoesAddOneTurnAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_1);
game.PlayerManager.Add(PLAYER_1);
game.PlayerManager.Add(PLAYER_2);
await game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_2);
int n = 5;
Player currentPlayer;
for (int i = 0; i < n; i++)
{
currentPlayer = game.GetWhoPlaysNow();
currentPlayer = await game.GetWhoPlaysNow();
game.PerformTurn(currentPlayer);
game.PrepareNextPlayer(currentPlayer);
await game.PrepareNextPlayer(currentPlayer);
}
Debug.WriteLine(game);
// Act
int actual = game.GetHistory().Count();
int actual = game.GetHistory().Count;
int expected = n;
// Assert
@ -149,23 +150,23 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public void TestGetWhoPlaysNowWhenValidThenCorrect()
public async Task TestGetWhoPlaysNowWhenValidThenCorrectAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_1);
game.PlayerManager.Add(PLAYER_1);
game.PlayerManager.Add(PLAYER_2);
await game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_2);
// Act
Player actual = game.GetWhoPlaysNow();
Player actual = await game.GetWhoPlaysNow();
Player expected = PLAYER_1;
game.PrepareNextPlayer(actual);
await game.PrepareNextPlayer(actual);
Player actual2 = game.GetWhoPlaysNow();
Player actual2 = await game.GetWhoPlaysNow();
Player expected2 = PLAYER_2;
// Assert
@ -183,10 +184,10 @@ namespace Tests.Model_UTs.Games
dice: DICE_1);
// Act
void action() => game.GetWhoPlaysNow(); // on an empty collection of players
async Task actionAsync() => await game.GetWhoPlaysNow(); // on an empty collection of players
// Assert
Assert.Throws<MemberAccessException>(action);
Assert.ThrowsAsync<MemberAccessException>(actionAsync);
}
[Fact]
@ -197,10 +198,10 @@ namespace Tests.Model_UTs.Games
playerManager: new PlayerManager(),
dice: DICE_1);
// Act
void action() => game.PrepareNextPlayer(PLAYER_1); // on an empty collection of players
async Task actionAsync() => await game.PrepareNextPlayer(PLAYER_1); // on an empty collection of players
// Assert
Assert.Throws<MemberAccessException>(action);
Assert.ThrowsAsync<MemberAccessException>(actionAsync);
}
[Fact]
@ -214,10 +215,10 @@ namespace Tests.Model_UTs.Games
game.PlayerManager.Add(PLAYER_1);
// Act
void action() => game.PrepareNextPlayer(null);
async Task actionAsync() => await game.PrepareNextPlayer(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
Assert.ThrowsAsync<ArgumentNullException>(actionAsync);
}
[Fact]
@ -231,59 +232,59 @@ namespace Tests.Model_UTs.Games
game.PlayerManager.Add(PLAYER_2);
// Act
void action() => game.PrepareNextPlayer(PLAYER_3);
async Task actionAsync() => await game.PrepareNextPlayer(PLAYER_3);
// Assert
Assert.Throws<ArgumentException>(action);
Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
[Fact]
public void TestPrepareNextPlayerWhenValidThenCorrectWithSeveralPlayers()
public async Task TestPrepareNextPlayerWhenValidThenCorrectWithSeveralPlayersAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_2);
game.PlayerManager.Add(PLAYER_1);
game.PlayerManager.Add(PLAYER_2);
await game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_2);
// Act
Player expected = PLAYER_2;
Assert.Equal(PLAYER_1, game.GetWhoPlaysNow());
game.PrepareNextPlayer(PLAYER_1);
Assert.Equal(PLAYER_1, await game.GetWhoPlaysNow());
await game.PrepareNextPlayer(PLAYER_1);
Player actual = game.GetWhoPlaysNow();
Player actual = await game.GetWhoPlaysNow();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestPrepareNextPlayerWhenValidThenCorrectWithOnePlayer()
public async Task TestPrepareNextPlayerWhenValidThenCorrectWithOnePlayerAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_1);
game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_1);
// Act
Player expected = PLAYER_1;
Assert.Equal(PLAYER_1, game.GetWhoPlaysNow());
game.PrepareNextPlayer(PLAYER_1);
Assert.Equal(PLAYER_1, await game.GetWhoPlaysNow());
await game.PrepareNextPlayer(PLAYER_1);
Player actual = game.GetWhoPlaysNow();
Player actual = await game.GetWhoPlaysNow();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestAddPlayerToGame()
public async Task TestAddPlayerToGameAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
@ -292,14 +293,14 @@ namespace Tests.Model_UTs.Games
// Act
Player expected = PLAYER_1;
Player actual = game.PlayerManager.Add(PLAYER_1);
Player actual = await game.PlayerManager.Add(PLAYER_1);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetPlayersFromGame()
public async Task TestGetPlayersFromGameAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
@ -307,46 +308,46 @@ namespace Tests.Model_UTs.Games
dice: DICE_1);
// Act
Assert.Empty(game.PlayerManager.GetAll());
game.PlayerManager.Add(PLAYER_1);
Assert.Empty(await game.PlayerManager.GetAll());
await game.PlayerManager.Add(PLAYER_1);
// Assert
Assert.Single(game.PlayerManager.GetAll());
Assert.Single(await game.PlayerManager.GetAll());
}
[Fact]
public void TestUpdatePlayerInGame()
public async Task TestUpdatePlayerInGameAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_2);
game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_1);
// Act
Player expected = PLAYER_2;
Player actual = game.PlayerManager.Update(PLAYER_1, PLAYER_2);
Player actual = await game.PlayerManager.Update(PLAYER_1, PLAYER_2);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestRemovePlayerFromGame()
public async Task TestRemovePlayerFromGameAsync()
{
// Arrange
Game game = new(name: GAME_NAME,
playerManager: new PlayerManager(),
dice: DICE_1);
game.PlayerManager.Add(PLAYER_1);
game.PlayerManager.Add(PLAYER_2);
await game.PlayerManager.Add(PLAYER_1);
await game.PlayerManager.Add(PLAYER_2);
game.PlayerManager.Remove(PLAYER_1);
// Act
IEnumerable<Player> expected = new List<Player>() { PLAYER_2 }.AsEnumerable();
IEnumerable<Player> actual = game.PlayerManager.GetAll();
IEnumerable<Player> actual = await game.PlayerManager.GetAll();
// Assert
Assert.Equal(expected, actual);

@ -5,43 +5,55 @@ using Model.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Model_UTs.Games
{
public class MasterOfCeremoniesTest
{
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp();
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp()?.Result;
[Fact]
public void TestPlayGameWhenPlayThenAddNewTurnToHistory()
public async Task TestPlayGameWhenPlayThenAddNewTurnToHistoryAsync()
{
// Arrange
MasterOfCeremonies masterOfCeremonies = stubMasterOfCeremonies;
Game game = masterOfCeremonies.GameManager.GetAll().First();
Game game = (await masterOfCeremonies.GameManager.GetAll()).First();
// Act
int turnsBefore = game.GetHistory().Count();
MasterOfCeremonies.PlayGame(game);
int turnsAfter = game.GetHistory().Count();
int turnsBefore = game.GetHistory().Count;
await MasterOfCeremonies.PlayGame(game);
int turnsAfter = game.GetHistory().Count;
// Assert
Assert.Equal(turnsBefore + 1, turnsAfter);
}
[Fact]
public void TestStartNewGame()
public async Task TestStartNewGame()
{
// Arrange
MasterOfCeremonies masterOfCeremonies = stubMasterOfCeremonies;
string name = "blargh";
// Act
Assert.DoesNotContain(masterOfCeremonies.GameManager.GetOneByName(name), masterOfCeremonies.GameManager.GetAll());
masterOfCeremonies.StartNewGame(name, new PlayerManager(), stubMasterOfCeremonies.GameManager.GetAll().First().Dice);
Assert.DoesNotContain(
await masterOfCeremonies.GameManager.GetOneByName(name),
await masterOfCeremonies.GameManager.GetAll()
);
await masterOfCeremonies.StartNewGame(
name,
new PlayerManager(),
stubMasterOfCeremonies.GameManager.GetAll()?.Result.First().Dice
);
// Assert
Assert.Contains(masterOfCeremonies.GameManager.GetOneByName(name), masterOfCeremonies.GameManager.GetAll());
Assert.Contains(
await masterOfCeremonies.GameManager.GetOneByName(name),
await masterOfCeremonies.GameManager.GetAll()
);
}
}
}

@ -5,6 +5,7 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xunit;
@ -13,14 +14,15 @@ namespace Tests.Model_UTs.Games
public class TurnTest
{
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp();
Dictionary<Die, Face> DICE_N_FACES_1, DICE_N_FACES_2;
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp()?.Result;
private readonly ReadOnlyDictionary<Die, Face> DICE_N_FACES_1;
private readonly ReadOnlyDictionary<Die, Face> DICE_N_FACES_2;
public TurnTest()
{
DICE_N_FACES_1 = (Dictionary<Die, Face>)stubMasterOfCeremonies.GameManager.GetAll().First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = (Dictionary<Die, Face>)stubMasterOfCeremonies.GameManager.GetAll().Last().GetHistory().Last().DiceNFaces;
DICE_N_FACES_1 = stubMasterOfCeremonies.GameManager.GetAll()?.Result.First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = stubMasterOfCeremonies.GameManager.GetAll()?.Result.Last().GetHistory().Last().DiceNFaces;
}
[Fact]
@ -92,10 +94,9 @@ namespace Tests.Model_UTs.Games
// Arrange
DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc);
Player player = new("Chucky");
DICE_N_FACES_1.Clear();
// Act
void action() => Turn.CreateWithSpecifiedTime(dateTime, player, DICE_N_FACES_1);
void action() => Turn.CreateWithSpecifiedTime(dateTime, player, new Dictionary<Die, Face>());
// Assert
Assert.Throws<ArgumentException>(action);

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Model_UTs.Players
@ -10,7 +11,7 @@ namespace Tests.Model_UTs.Players
public class PlayerManagerTest
{
[Fact]
public void TestConstructorReturnsEmptyEnumerable()
public async Task TestConstructorReturnsEmptyEnumerableAsync()
{
// Arrange
PlayerManager playerManager = new();
@ -19,14 +20,14 @@ namespace Tests.Model_UTs.Players
// Act
expected = new Collection<Player>();
actual = playerManager.GetAll();
actual = await playerManager.GetAll();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestAddIfPlayersThenDoAddAndReturnPlayers()
public async Task TestAddIfPlayersThenDoAddAndReturnPlayersAsync()
{
// Arrange
PlayerManager playerManager = new();
@ -37,8 +38,8 @@ namespace Tests.Model_UTs.Players
Collection<Player> expected = new() { alice, bob };
Collection<Player> actual = new()
{
playerManager.Add(alice),
playerManager.Add(bob)
await playerManager.Add(alice),
await playerManager.Add(bob)
};
// Assert
@ -46,7 +47,7 @@ namespace Tests.Model_UTs.Players
}
[Fact]
public void TestAddIfNullThrowsException()
public async Task TestAddIfNullThrowsException()
{
// Arrange
PlayerManager playerManager = new();
@ -54,26 +55,26 @@ namespace Tests.Model_UTs.Players
// Act
expected = null;
void action() => playerManager.Add(expected);// Add() returns the added element if succesful
async Task actionAsync() => await playerManager.Add(expected);// Add() returns the added element if succesful
// Assert
Assert.Null(expected);
Assert.Throws<ArgumentNullException>(action);
Assert.DoesNotContain(expected, playerManager.GetAll());
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync);
Assert.DoesNotContain(expected, await playerManager.GetAll());
}
[Fact]
public void TestAddIfAlreadyExistsThrowsException()
public async Task TestAddIfAlreadyExistsThrowsException()
{
// Arrange
PlayerManager playerManager = new();
// Act
playerManager.Add(new("Kevin"));
void action() => playerManager.Add(new("Kevin"));
await playerManager.Add(new("Kevin"));
async Task actionAsync() => await playerManager.Add(new("Kevin"));
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
@ -84,7 +85,7 @@ namespace Tests.Model_UTs.Players
PlayerManager playerManager = new();
// Act
void action() => playerManager.GetOneByID(new("1a276327-75fc-45b9-8854-e7c4101088f8"));
// Assert
@ -95,18 +96,18 @@ namespace Tests.Model_UTs.Players
[InlineData("")]
[InlineData(null)]
[InlineData(" ")]
public void TestGetOneByNameIfInvalidThrowsException(string name)
public async Task TestGetOneByNameIfInvalidThrowsException(string name)
{
// Arrange
PlayerManager playerManager = new();
Player player = new("Bob");
playerManager.Add(player);
await playerManager.Add(player);
// Act
void action() => playerManager.GetOneByName(name);
async Task actionAsync() => await playerManager.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(action);
await Assert.ThrowsAsync<ArgumentException>(actionAsync);
}
[Fact]
@ -118,7 +119,7 @@ namespace Tests.Model_UTs.Players
playerManager.Add(player);
// Act
Player result = playerManager.GetOneByName("Clyde");
Player result = playerManager.GetOneByName("Clyde")?.Result;
// Assert
Assert.Null(result);
@ -137,33 +138,33 @@ namespace Tests.Model_UTs.Players
playerManager.Add(expected);
// Act
Player actual = playerManager.GetOneByName(name);
Player actual = playerManager.GetOneByName(name)?.Result;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestRemoveWorksIfExists()
public async Task TestRemoveWorksIfExists()
{
// Arrange
PlayerManager playerManager = new();
Player p1 = new("Dylan");
playerManager.Add(p1);
await playerManager.Add(p1);
// Act
playerManager.Remove(p1);
// Assert
Assert.DoesNotContain(p1, playerManager.GetAll());
Assert.DoesNotContain(p1, await playerManager.GetAll());
}
[Fact]
public void TestRemoveThrowsExceptionIfGivenNull()
public async Task TestRemoveThrowsExceptionIfGivenNull()
{
// Arrange
PlayerManager playerManager = new();
playerManager.Add(new Player("Dylan"));
await playerManager.Add(new Player("Dylan"));
// Act
void action() => playerManager.Remove(null);
@ -173,37 +174,37 @@ namespace Tests.Model_UTs.Players
}
[Fact]
public void TestRemoveFailsSilentlyIfGivenNonExistent()
public async Task TestRemoveFailsSilentlyIfGivenNonExistent()
{
// Arrange
PlayerManager playerManager = new();
Player player = new("Dylan");
playerManager.Add(player);
await playerManager.Add(player);
Player notPlayer = new("Eric");
// Act
playerManager.Remove(notPlayer);
// Assert
Assert.DoesNotContain(notPlayer, playerManager.GetAll());
Assert.DoesNotContain(notPlayer, await playerManager.GetAll());
}
[Fact]
public void TestUpdateWorksIfValid()
public async Task TestUpdateWorksIfValid()
{
// Arrange
PlayerManager playerManager = new();
Player oldPlayer = new("Dylan");
playerManager.Add(oldPlayer);
await playerManager.Add(oldPlayer);
Player newPlayer = new("Eric");
// Act
playerManager.Update(oldPlayer, newPlayer);
await playerManager.Update(oldPlayer, newPlayer);
// Assert
Assert.DoesNotContain(oldPlayer, playerManager.GetAll());
Assert.Contains(newPlayer, playerManager.GetAll());
Assert.True(playerManager.GetAll().Count() == 1);
Assert.DoesNotContain(oldPlayer, await playerManager.GetAll());
Assert.Contains(newPlayer, await playerManager.GetAll());
Assert.True((await playerManager.GetAll()).Count == 1);
}
[Theory]
@ -211,20 +212,20 @@ namespace Tests.Model_UTs.Players
[InlineData("Filibert", " fiLibert")]
[InlineData("Filibert", "FIlibert ")]
[InlineData(" Filibert", " filiBErt ")]
public void TestUpdateDiscreetlyUpdatesCaseAndIgnoresExtraSpaceIfOtherwiseSame(string n1, string n2)
public async Task TestUpdateDiscreetlyUpdatesCaseAndIgnoresExtraSpaceIfOtherwiseSame(string n1, string n2)
{
// Arrange
PlayerManager playerManager = new();
Player oldPlayer = new(n1);
playerManager.Add(oldPlayer);
await playerManager.Add(oldPlayer);
Player newPlayer = new(n2);
// Act
playerManager.Update(oldPlayer, newPlayer);
await playerManager.Update(oldPlayer, newPlayer);
// Assert
Assert.Contains(oldPlayer, playerManager.GetAll());
Assert.Contains(newPlayer, playerManager.GetAll());
Assert.Contains(oldPlayer, await playerManager.GetAll());
Assert.Contains(newPlayer, await playerManager.GetAll());
Assert.Equal(oldPlayer, newPlayer);
}
@ -232,62 +233,62 @@ namespace Tests.Model_UTs.Players
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateDoesNotGoWithValidBeforeAndInvalidAfter(string badName)
public async Task TestUpdateDoesNotGoWithValidBeforeAndInvalidAfter(string badName)
{
// Arrange
PlayerManager playerManager = new();
Player oldPlayer = new("Ni!");
playerManager.Add(oldPlayer);
int size1 = playerManager.GetAll().Count();
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
// Act
Assert.Contains(oldPlayer, playerManager.GetAll());
void action() => playerManager.Update(oldPlayer, new Player(badName));// this is really testing the Player class...
int size2 = playerManager.GetAll().Count();
Assert.Contains(oldPlayer, await playerManager.GetAll());
async Task actionAsync() => await playerManager.Update(oldPlayer, new Player(badName));// this is really testing the Player class...
int size2 = (await playerManager.GetAll()).Count;
// Assert
Assert.Throws<ArgumentException>(action); // thrown by Player constructor
Assert.Contains(oldPlayer, playerManager.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentException>(actionAsync); // thrown by Player constructor
Assert.Contains(oldPlayer, await playerManager.GetAll()); // still there
Assert.True(size1 == size2);
}
[Fact]
public void TestUpdateDoesNotGoWithValidBeforeAndNullAfter()
public async Task TestUpdateDoesNotGoWithValidBeforeAndNullAfter()
{
// Arrange
PlayerManager playerManager = new();
Player oldPlayer = new("Ni!");
playerManager.Add(oldPlayer);
int size1 = playerManager.GetAll().Count();
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
// Act
Assert.Contains(oldPlayer, playerManager.GetAll());
void action() => playerManager.Update(oldPlayer, null);
int size2 = playerManager.GetAll().Count();
Assert.Contains(oldPlayer, await playerManager.GetAll());
async Task actionAsync() => await playerManager.Update(oldPlayer, null);
int size2 = (await playerManager.GetAll()).Count;
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by Update()
Assert.Contains(oldPlayer, playerManager.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by Update()
Assert.Contains(oldPlayer, await playerManager.GetAll()); // still there
Assert.True(size1 == size2);
}
[Fact]
public void TestUpdateDoesNotGoWithValidAfterAndNullBefore()
public async Task TestUpdateDoesNotGoWithValidAfterAndNullBefore()
{
// Arrange
PlayerManager playerManager = new();
Player newPlayer = new("Kevin");
Player oldPlayer = new("Ursula");
playerManager.Add(oldPlayer);
int size1 = playerManager.GetAll().Count();
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
// Act
void action() => playerManager.Update(null, newPlayer);
int size2 = playerManager.GetAll().Count();
async Task actionAsync() => await playerManager.Update(null, newPlayer);
int size2 = (await playerManager.GetAll()).Count;
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by Update()
Assert.Contains(oldPlayer, playerManager.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by Update()
Assert.Contains(oldPlayer, await playerManager.GetAll()); // still there
Assert.True(size1 == size2);
}
@ -295,21 +296,21 @@ namespace Tests.Model_UTs.Players
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateDoesNotGoWithValidAfterAndInvalidBefore(string name)
public async Task TestUpdateDoesNotGoWithValidAfterAndInvalidBefore(string name)
{
// Arrange
PlayerManager playerManager = new();
Player oldPlayer = new("Ursula");
playerManager.Add(oldPlayer);
int size1 = playerManager.GetAll().Count();
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
// Act
void action() => playerManager.Update(new Player(name), new Player("Vicky"));
int size2 = playerManager.GetAll().Count();
async Task actionAsync() => await playerManager.Update(new Player(name), new Player("Vicky"));
int size2 = (await playerManager.GetAll()).Count;
// Assert
Assert.Throws<ArgumentException>(action); // thrown by Player constructor
Assert.Contains(oldPlayer, playerManager.GetAll()); // still there
await Assert.ThrowsAsync<ArgumentException>(actionAsync); // thrown by Player constructor
Assert.Contains(oldPlayer, await playerManager.GetAll()); // still there
Assert.True(size1 == size2);
}
}

@ -65,20 +65,6 @@ namespace Tests.Model_UTs.Players
Assert.Throws<ArgumentException>(action);
}
[Fact]
public void TestToStringCorrectName()
{
// Arrange
string expected = "Bob";
Player player = new(expected);
// Act
string actual = player.ToString();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestEqualsFalseIfNotPlayer()
{

@ -1,13 +1,13 @@
namespace Tests.Model_UTs
{
public class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point(int x, int y)
{
X = x; Y = y;
}
}
}
namespace Tests
{
public class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point(int x, int y)
{
X = x; Y = y;
}
}
}

@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Xunit;
namespace Tests.Utils_UTs
{
public class EnumerablesTest
{
[Fact]
public void TestFeedListsToDict()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
string str3 = "azfyoaz";
int int1 = 5;
int int2 = 12;
int int3 = 3;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 },
{ str3, int3 }
};
List<string> strings = new() { str2, str3 };
List<int> ints = new() { int2, int3 };
Dictionary<string, int> actual = new()
{
{str1, int1 }
}; // we will add on top of this
// Act
actual = Enumerables.FeedListsToDict(actual, strings, ints);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetDictFromLists()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
int int1 = 5;
int int2 = 12;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 }
};
List<string> strings = new() { str1, str2 };
List<int> ints = new() { int1, int2 };
// Act
Dictionary<string, int> actual = Enumerables.GetDictFromLists(strings, ints);
// Assert
Assert.Equal(expected, actual);
}
public static IEnumerable<object[]> EmptyList()
{
yield return new object[] { new List<string>() };
}
[Theory]
[InlineData(null)]
[MemberData(nameof(EmptyList))]
public void TestGetDictFromListsWhenKeysNullOrEmptyThenNew(List<string> strings)
{
// Arrange
int int1 = 5;
int int2 = 12;
Dictionary<string, int> expected = new();
List<int> ints = new() { int1, int2 };
// Act
Dictionary<string, int> actual = Enumerables.GetDictFromLists(strings, ints);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null)]
[MemberData(nameof(EmptyList))]
public void TestGetDictFromListsWhenValuesNullOrEmptyThenNew(List<string> stringsB)
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
Dictionary<string, string> expected = new();
List<string> strings = new() { str1, str2 };
// Act
Dictionary<string, string> actual = Enumerables.GetDictFromLists(strings, stringsB);
// Assert
Assert.Equal(expected, actual);
}
}
}

@ -0,0 +1,29 @@
namespace Utils
{
public static class Enumerables
{
public static Dictionary<K, V> GetDictFromLists<K, V>(List<K> keys, List<V> values)
{
if (keys == null || values == null || keys.Count == 0 || values.Count == 0)
{
return new Dictionary<K, V>();
}
return keys.Zip(
values,
(key, value) => new { key, value })
.ToDictionary(kvp => kvp.key, kvp => kvp.value
);
}
public static Dictionary<K, V> FeedListsToDict<K, V>(Dictionary<K, V> kvps, List<K> keys, List<V> values)
{
foreach (KeyValuePair<K, V> kvp in GetDictFromLists(keys, values))
{
kvps.Add(kvp.Key, kvp.Value);
}
return kvps;
}
}
}

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Loading…
Cancel
Save