diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 866bacb..15391f7 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,19 @@ 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) { - games.Remove(games.FirstOrDefault(g => g.Name == game.Name)); - // will often be an update: if game with that name exists, it is removed, else, nothing happens above - games.Add(game); - return game; + throw new ArgumentNullException(nameof(toAdd), "param should not be null"); } - return null; + + 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; } /// @@ -68,14 +73,31 @@ namespace Model.Games Add(game); } - public void Remove(Game game) + public void Remove(Game toRemove) { - games.Remove(game); + if (toRemove is null) + { + throw new ArgumentNullException(nameof(toRemove), "param should not be null"); + } + games.Remove(toRemove); } - public Game Update(Game oldGame, Game newGame) + public Game Update(Game before, Game after) { - return Add(newGame); + + Game[] args = { before, after }; + + foreach (Game game in args) + { + if (game is null) + { + throw new ArgumentNullException(nameof(after), "param should not be null"); + // could also be because of before, but one param had to be chosen as an example + // and putting "player" there was raising a major code smell + } + } + Remove(before); + return Add(after); } /// diff --git a/Sources/Tests/Model_UTs/GameRunnerTest.cs b/Sources/Tests/Model_UTs/GameRunnerTest.cs new file mode 100644 index 0000000..c8a715b --- /dev/null +++ b/Sources/Tests/Model_UTs/GameRunnerTest.cs @@ -0,0 +1,275 @@ +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 TestGetOneByNameWhenInvalidThenThrowsException(string name) + { + // Arrange + + + // Act + void action() => stubGameRunner.GetOneByName(name); + + // Assert + Assert.Throws(action); + } + + [Fact] + public void TestGetOneByNameWhenValidButNotExistThenReturnNull() + { + // Arrange + + + // Act + Game result = stubGameRunner.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists"); + + // Assert + Assert.Null(result); + } + + [Fact] + public void TestGetOneByNameWhenValidThenReturnGame() + { + // Arrange + GameRunner gameRunner = new(new PlayerManager(), new DieManager()); + Game game = stubGameRunner.GetAll().First(); + + // Act + Game actual = gameRunner.Add(game); + Game expected = game; + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void TestWhenRemoveExistsThenSucceeds() + { + // Arrange + Game game = new("blargh", new PlayerManager(), stubGameRunner.GetAll().First().Dice); + stubGameRunner.Add(game); + + // Act + stubGameRunner.Remove(game); + + // Assert + Assert.DoesNotContain(game, stubGameRunner.GetAll()); + } + + [Fact] + public void TestRemoveWhenGivenNullThenThrowsException() + { + // Arrange + + + // Act + void action() => stubGameRunner.Remove(null); + + // Assert + Assert.Throws(action); + } + + [Fact] + public void TestRemoveWhenGiveenNonExistentThenFailsSilently() + { + // Arrange + Game notGame = new("blargh", new PlayerManager(), stubGameRunner.GetAll().First().Dice); + IEnumerable expected = stubGameRunner.GetAll(); + + // Act + stubGameRunner.Remove(notGame); + IEnumerable actual = stubGameRunner.GetAll(); + + // Assert + Assert.Equal(actual, expected); + } + + [Fact] + public void TestUpdateWhenValidThenSucceeds() + { + // Arrange + string oldName = "blargh"; + string newName = "blargh2.0"; + GameRunner gameRunner = new(new PlayerManager(), new DieManager()); + Game game = new(oldName, new PlayerManager(), stubGameRunner.GetAll().First().Dice); + game.PlayerManager.Add(new("Alice")); + gameRunner.Add(game); + + Game oldGame = gameRunner.GetAll().First(); + Game newGame = new(newName, oldGame.PlayerManager, oldGame.Dice); + + // Act + int oldSize = gameRunner.GetAll().Count(); + gameRunner.Update(oldGame, newGame); + int newSize = gameRunner.GetAll().Count(); + + // Assert + Assert.NotEqual(oldName, newName); + Assert.DoesNotContain(oldGame, gameRunner.GetAll()); + Assert.Contains(newGame, gameRunner.GetAll()); + Assert.Equal(oldSize, newSize); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGo(string badName) + { + // Arrange + int expectedSize = stubGameRunner.GetAll().Count(); + Game oldGame = stubGameRunner.GetAll().First(); + + // Act + void action() => stubGameRunner.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice)); + int actualSize = stubGameRunner.GetAll().Count(); + + // Assert + Assert.Throws(action); // thrown by constructor + Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there + Assert.True(expectedSize == actualSize); + } + + [Fact] + public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo() + { + // Arrange + int expectedSize = stubGameRunner.GetAll().Count(); + Game oldGame = stubGameRunner.GetAll().First(); + + // Act + void action() => stubGameRunner.Update(oldGame, null); + int actualSize = stubGameRunner.GetAll().Count(); + + // Assert + Assert.Throws(action); // thrown by constructor + Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there + Assert.True(expectedSize == actualSize); + } + + [Fact] + public void TestUpdateDoesNotGoWithValidAfterAndNullBefore() + { + // Arrange + int expectedSize = stubGameRunner.GetAll().Count(); + Game oldGame = stubGameRunner.GetAll().First(); + + // Act + void action() => stubGameRunner.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice)); + int actualSize = stubGameRunner.GetAll().Count(); + + // Assert + Assert.Throws(action); // thrown by constructor + Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there + Assert.True(expectedSize == actualSize); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName) + { + // Arrange + int expectedSize = stubGameRunner.GetAll().Count(); + Game oldGame = stubGameRunner.GetAll().First(); + + // Act + void action() => stubGameRunner.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice)); + int actualSize = stubGameRunner.GetAll().Count(); + + // Assert + Assert.Throws(action); // thrown by constructor + Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there + Assert.True(expectedSize == actualSize); + } + } +} 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 @@ +