using Data; using Model; using Model.Dice; using Model.Games; using Model.Players; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Xunit; namespace Tests.Model_UTs.Games { public class GameManagerTest { private readonly MasterOfCeremonies stubGameRunner = new Stub().LoadApp(); [Fact] public void TestConstructorReturnsEmptyEnumerable() { // Arrange GameManager gm = new(); IEnumerable expected; IEnumerable actual; // Act expected = new Collection(); actual = gm.GetAll(); // Assert Assert.Equal(expected, actual); } [Fact] public void TestAddWhenGamesThenDoAddAndReturnGames() { // Arrange GameManager gm = new(); Game game1 = stubGameRunner.GameManager.GetAll().First(); Game game2 = stubGameRunner.GameManager.GetAll().Last(); // Act IEnumerable expected = new List() { game1, game2 }.AsEnumerable(); IEnumerable actual = new List() { gm.Add(game1), gm.Add(game2) }; // Assert Assert.Equal(expected, actual); } [Fact] public void TestAddWhenNullThenThrowsException() { // Arrange GameManager gm = new(); // Act void action() => gm.Add(null);// Add() returns the added element if succesful // Assert Assert.Throws(action); Assert.DoesNotContain(null, stubGameRunner.GameManager.GetAll()); } [Fact] public void TestGetOneByIdThrowsException() { // Arrange GameManager gm = new(); // Act void action() => gm.GetOneByID(Guid.NewGuid()); // Assert Assert.Throws(action); } [Theory] [InlineData("")] [InlineData(null)] [InlineData(" ")] public void TestGetOneByNameWhenInvalidThenThrowsException(string name) { // Arrange GameManager gm = new(); // Act void action() => gm.GetOneByName(name); // Assert Assert.Throws(action); } [Fact] public void TestGetOneByNameWhenValidButNotExistThenReturnNull() { // Arrange GameManager gm = new(); // Act Game result = gm.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists"); // Assert Assert.Null(result); } [Fact] public void TestGetOneByNameWhenValidThenReturnGame() { // Arrange GameManager gm = new(); Game game = stubGameRunner.GameManager.GetAll().First(); // Act Game actual = gm.Add(game); Game expected = game; // Assert Assert.Equal(expected, actual); } [Fact] public void TestWhenRemoveExistsThenSucceeds() { // Arrange GameManager gm = new(); Game game = new("blargh", new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice); gm.Add(game); // Act gm.Remove(game); // Assert Assert.DoesNotContain(game, gm.GetAll()); } [Fact] public void TestRemoveWhenGivenNullThenThrowsException() { // Arrange GameManager gm = new(); // Act void action() => gm.Remove(null); // Assert Assert.Throws(action); } [Fact] public void TestRemoveWhenGivenNonExistentThenFailsSilently() { // Arrange IManager gm = stubGameRunner.GameManager; Game notGame = new("blargh", new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice); IEnumerable expected = stubGameRunner.GameManager.GetAll(); // Act gm.Remove(notGame); IEnumerable actual = gm.GetAll(); // Assert Assert.Equal(actual, expected); } [Fact] public void 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 oldGame = 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(); // Assert Assert.NotEqual(oldName, newName); Assert.DoesNotContain(oldGame, gm.GetAll()); Assert.Contains(newGame, gm.GetAll()); Assert.Equal(expectedSize, actualSize); } [Theory] [InlineData("")] [InlineData(" ")] [InlineData(null)] public void TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGo(string badName) { // Arrange IManager gm = stubGameRunner.GameManager; int expectedSize = gm.GetAll().Count(); Game oldGame = gm.GetAll().First(); // Act void action() => gm.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice)); int actualSize = gm.GetAll().Count(); // Assert Assert.Throws(action); // thrown by constructor Assert.Contains(oldGame, gm.GetAll()); // still there Assert.Equal(expectedSize, actualSize); } [Fact] public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo() { // Arrange IManager gm = stubGameRunner.GameManager; int expectedSize = gm.GetAll().Count(); Game oldGame = gm.GetAll().First(); // Act void action() => gm.Update(oldGame, null); int actualSize = gm.GetAll().Count(); // Assert Assert.Throws(action); // thrown by constructor Assert.Contains(oldGame, gm.GetAll()); // still there Assert.True(expectedSize == actualSize); } [Fact] public void TestUpdateDoesNotGoWithValidAfterAndNullBefore() { // Arrange IManager gm = stubGameRunner.GameManager; int expectedSize = gm.GetAll().Count(); Game oldGame = gm.GetAll().First(); // Act void action() => gm.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice)); int actualSize = gm.GetAll().Count(); // Assert Assert.Throws(action); // thrown by constructor Assert.Contains(oldGame, gm.GetAll()); // still there Assert.True(expectedSize == actualSize); } [Theory] [InlineData("")] [InlineData(" ")] [InlineData(null)] public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName) { // Arrange IManager gm = stubGameRunner.GameManager; int expectedSize = gm.GetAll().Count(); Game oldGame = 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(); // Assert Assert.Throws(action); // thrown by constructor Assert.Contains(oldGame, gm.GetAll()); // still there Assert.True(expectedSize == actualSize); } } }