From 3c74fa496295fcab47df69001a436e84b4bc18d9 Mon Sep 17 00:00:00 2001 From: "alexis.drai" Date: Thu, 29 Sep 2022 09:43:47 +0200 Subject: [PATCH] :construction: WIP --- Sources/Model/Games/GameRunner.cs | 21 +- Sources/Tests/Model_UTs/GameRunnerTest.cs | 262 +++++++++++++++++++ Sources/Tests/Model_UTs/PlayerManagerTest.cs | 2 +- Sources/Tests/Tests.csproj | 1 + 4 files changed, 278 insertions(+), 8 deletions(-) create mode 100644 Sources/Tests/Model_UTs/GameRunnerTest.cs diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 866bacb..e772ea0 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -22,6 +22,10 @@ namespace Model.Games this.games = games ?? new(); } + public GameRunner(IManager globalPlayerManager, IManager>>> globalDieManager) + : this(globalPlayerManager, globalDieManager, null){ } + + public IEnumerable GetAll() => games.AsEnumerable(); /// @@ -44,18 +48,21 @@ namespace Model.Games /// /// saves a given game -- does not allow copies yet: if a game with the same name exists, it is overwritten /// - /// a game to save + /// a game to save /// - public Game Add(Game game) + public Game Add(Game toAdd) { - if (game != null) + if (toAdd is null) + { + throw new ArgumentNullException(nameof(toAdd), "param should not be null"); + } + else { - games.Remove(games.FirstOrDefault(g => g.Name == game.Name)); + 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(game); - return game; + games.Add(toAdd); + return toAdd; } - return null; } /// diff --git a/Sources/Tests/Model_UTs/GameRunnerTest.cs b/Sources/Tests/Model_UTs/GameRunnerTest.cs new file mode 100644 index 0000000..b35f774 --- /dev/null +++ b/Sources/Tests/Model_UTs/GameRunnerTest.cs @@ -0,0 +1,262 @@ +using Model.Games; +using Model.Dice; +using Model.Dice.Faces; +using Model.Players; +using Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Tests.Model_UTs +{ + public class GameRunnerTest + { + private readonly GameRunner stubGameRunner; + public GameRunnerTest() + { + stubGameRunner = new Stub().LoadApp(); + } + + [Fact] + public void TestConstructorWhenNoGamesThenNewIEnumerable() + { + // Arrange + GameRunner gameRunner = new(new PlayerManager(), new DieManager()); + IEnumerable expected; + IEnumerable actual; + + // Act + expected = new List().AsEnumerable(); + actual = gameRunner.GetAll(); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void TestConstructorWhenGamesThenGamesIEnumerable() + { + // Arrange + GameRunner gameRunner = new(new PlayerManager(), new DieManager(), stubGameRunner.GetAll().ToList()); + IEnumerable expected; + IEnumerable actual; + + // Act + expected = stubGameRunner.GetAll(); + actual = gameRunner.GetAll(); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void TestAddWhenGamesThenDoAddAndReturnGames() + { + // Arrange + GameRunner gameRunner = new(new PlayerManager(), new DieManager()); + Game game1 = stubGameRunner.GetAll().First(); + Game game2 = stubGameRunner.GetAll().Last(); + + // Act + IEnumerable expected = new List() { game1, game2 }.AsEnumerable(); + IEnumerable actual = new List() + { + gameRunner.Add(game1), + gameRunner.Add(game2) + }; + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void TestAddwhenNullThenThrowsException() + { + // Arrange + + + // Act + void action() => stubGameRunner.Add(null);// Add() returns the added element if succesful + + // Assert + Assert.Throws(action); + Assert.DoesNotContain(null, stubGameRunner.GetAll()); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + [InlineData(" ")] + public void TestGetOneByNameIfInvalidThrowsException(string name) + { + // Arrange + + + // Act + void action() => stubGameRunner.GetOneByName(name); + + // Assert + Assert.Throws(action); + } + + [Fact] + public void TestGetOneByNameIfValidButNotExistThenReturnNull() + { + // Arrange + + + // Act + Game result = stubGameRunner.GetOneByName("thereisbasicalllynowaythatthisgamenamealreadyexists"); + + // Assert + Assert.Null(result); + } + + [Theory] + [InlineData("Bob")] + [InlineData("bob")] + [InlineData("bob ")] + [InlineData(" boB ")] + public void TestGetOneByNameIfValidThenReturnPlayer(string name) + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestRemoveWorksIfExists() + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestRemoveThrowsExceptionIfGivenNull() + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestRemoveFailsSilentlyIfGivenNonExistent() + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestUpdateWorksIfValid() + { + // Arrange + + + // Act + + + // Assert + + } + + [Theory] + [InlineData("Filibert", "filibert")] + [InlineData("Filibert", " fiLibert")] + [InlineData("Filibert", "FIlibert ")] + [InlineData(" Filibert", " filiBErt ")] + public void TestUpdateDiscreetlyUpdatesCaseAndIgnoresExtraSpaceIfOtherwiseSame(string n1, string n2) + { + // Arrange + + + // Act + + + // Assert + + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void TestUpdateDoesNotGoWithValidBeforeAndInvalidAfter(string badName) + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestUpdateDoesNotGoWithValidBeforeAndNullAfter() + { + // Arrange + + + // Act + + + // Assert + + } + + [Fact] + public void TestUpdateDoesNotGoWithValidAfterAndNullBefore() + { + // Arrange + + + // Act + + + // Assert + + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void TestUpdateDoesNotGoWithValidAfterAndInvalidBefore(string name) + { + // Arrange + + + // Act + + + // Assert + + } + } +} diff --git a/Sources/Tests/Model_UTs/PlayerManagerTest.cs b/Sources/Tests/Model_UTs/PlayerManagerTest.cs index c698a95..fe94980 100644 --- a/Sources/Tests/Model_UTs/PlayerManagerTest.cs +++ b/Sources/Tests/Model_UTs/PlayerManagerTest.cs @@ -59,8 +59,8 @@ namespace Tests.Model_UTs // Assert Assert.Null(expected); - Assert.DoesNotContain(expected, playerManager.GetAll()); Assert.Throws(action); + Assert.DoesNotContain(expected, playerManager.GetAll()); } [Fact] diff --git a/Sources/Tests/Tests.csproj b/Sources/Tests/Tests.csproj index c899ea9..2d2506b 100644 --- a/Sources/Tests/Tests.csproj +++ b/Sources/Tests/Tests.csproj @@ -20,6 +20,7 @@ +