♻️ Refactor manager-y stuff into GameManager
continuous-integration/drone/push Build is passing Details

pull/125/head
Alexis Drai 2 years ago
parent d1c7f0674d
commit 08c1e415a9

@ -50,7 +50,7 @@ namespace App
// persist them as models !
gameRunner.GlobalPlayerManager.Add(entity.ToModel());
Debug.WriteLine($"{entity.ID} -- {entity.Name}");
}
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
@ -82,7 +82,7 @@ namespace App
case "l":
string loadName = ChooseGame(gameRunner);
if (gameRunner.GetOneByName(loadName) != null)
if (gameRunner.GameManager.GetOneByName(loadName) != null)
{
Play(gameRunner, loadName);
}
@ -90,7 +90,7 @@ namespace App
case "n":
if (!gameRunner.GlobalDieManager.GetAll().Any())
if (!gameRunner.DieGroupManager.GetAll().Any())
{
Console.WriteLine("make at least one dice group first, then try again");
break;
@ -112,7 +112,7 @@ namespace App
case "d":
string deleteName = ChooseGame(gameRunner);
gameRunner.Remove(gameRunner.GetOneByName(deleteName));
gameRunner.GameManager.Remove(gameRunner.GameManager.GetOneByName(deleteName));
break;
case "c":
@ -151,7 +151,7 @@ namespace App
newGroupDice.Add(die);
}
}
gameRunner.GlobalDieManager.Add(new KeyValuePair<string, IEnumerable<Die>>(newGroupName, newGroupDice));
gameRunner.DieGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(newGroupName, newGroupDice));
break;
case "p":
@ -197,7 +197,8 @@ namespace App
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
} catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
}
private static void Play(GameRunner gameRunner, string name)
@ -205,7 +206,7 @@ namespace App
string menuChoicePlay = "";
while (menuChoicePlay != "q")
{
Game game = gameRunner.GetOneByName(name);
Game game = gameRunner.GameManager.GetOneByName(name);
Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" +
"q... quit\n" +
"h... show history\n" +
@ -223,7 +224,7 @@ namespace App
}
break;
case "s":
gameRunner.Add(game);
gameRunner.GameManager.Add(game);
break;
default:
GameRunner.PlayGame(game);
@ -237,7 +238,7 @@ namespace App
{
string name;
Console.WriteLine("which of these games?\n(choose by name)\n>");
foreach (Game game in gameRunner.GetAll())
foreach (Game game in gameRunner.GameManager.GetAll())
{
Console.WriteLine(game);
}
@ -256,7 +257,7 @@ namespace App
private static void ShowDice(GameRunner gameRunner)
{
foreach ((string name, IEnumerable<Die> dice) in gameRunner.GlobalDieManager.GetAll())
foreach ((string name, IEnumerable<Die> dice) in gameRunner.DieGroupManager.GetAll())
{
Console.WriteLine($"{name} -- {dice}");
}
@ -344,7 +345,7 @@ namespace App
menuChoiceDice = Console.ReadLine();
if (!menuChoiceDice.Equals("ok"))
{
IEnumerable<Die> chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value;
IEnumerable<Die> chosenDice = gameRunner.DieGroupManager.GetOneByName(menuChoiceDice).Value;
foreach (Die die in chosenDice)
{
result.Add(die);

@ -10,7 +10,7 @@ namespace Data
{
public GameRunner LoadApp()
{
GameRunner gr = new(new PlayerManager(), new DieManager());
GameRunner gr = new(new PlayerManager(), new DieManager(), new GameManager());
Player player1 = new("Alice(Old Stub)"), player2 = new("Bob(Old Stub)"), player3 = new("Clyde(Old Stub)");
@ -63,26 +63,26 @@ namespace Data
dndDice.Add(new NumberDie(d20Faces));
gr.GlobalDieManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable()));
gr.GlobalDieManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable()));
gr.DieGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable()));
gr.DieGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable()));
string game1 = "Forgotten Realms", game2 = "4e", game3 = "The Coopers";
gr.Add(new(game1, new PlayerManager(), dndDice.AsEnumerable()));
gr.Add(new(game2, new PlayerManager(), dndDice.AsEnumerable()));
gr.Add(new(game3, new PlayerManager(), monopolyDice.AsEnumerable()));
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()));
gr.GetOneByName(game1).PlayerManager.Add(player1);
gr.GetOneByName(game1).PlayerManager.Add(player2);
gr.GameManager.GetOneByName(game1).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game1).PlayerManager.Add(player2);
gr.GetOneByName(game2).PlayerManager.Add(player1);
gr.GetOneByName(game2).PlayerManager.Add(player2);
gr.GetOneByName(game2).PlayerManager.Add(player3);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player2);
gr.GameManager.GetOneByName(game2).PlayerManager.Add(player3);
gr.GetOneByName(game3).PlayerManager.Add(player1);
gr.GetOneByName(game3).PlayerManager.Add(player3);
gr.GameManager.GetOneByName(game3).PlayerManager.Add(player1);
gr.GameManager.GetOneByName(game3).PlayerManager.Add(player3);
foreach (Game game in gr.GetAll())
foreach (Game game in gr.GameManager.GetAll())
{
for (int i = 0; i < 10; i++)
{

@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.Games
{
public class GameManager : IManager<Game>
{
/// <summary>
/// the games managed by this instance
/// </summary>
private readonly List<Game> games;
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();
/// <summary>
/// finds the game with that name and returns it
/// <br/>
/// that copy does not belong to this gamerunner's games, so it should not be modified
/// </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)
{
if (!string.IsNullOrWhiteSpace(name))
{
Game result = games.FirstOrDefault(g => g.Name == name);
return result; // may return null
}
throw new ArgumentException("param should not be null or blank", nameof(name));
}
/// <summary>
/// not implemented in the model
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Game GetOneByID(Guid ID)
{
throw new NotImplementedException();
}
/// <summary>
/// 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)
{
if (toAdd is null)
{
throw new ArgumentNullException(nameof(toAdd), "param should not be 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;
}
/// <summary>
/// removes a game. does nothing if the game doesn't exist
/// </summary>
/// <param name="toRemove">game to remove</param>
/// <exception cref="ArgumentNullException"></exception>
public void Remove(Game toRemove)
{
if (toRemove is null)
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
}
games.Remove(toRemove);
}
/// <summary>
/// updates a game
/// </summary>
/// <param name="before">original game</param>
/// <param name="after">new game</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public Game Update(Game before, Game after)
{
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);
}
}
}

@ -1,98 +1,33 @@
using Model.Dice;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Model.Games
{
public class GameRunner : IManager<Game>
public class GameRunner
{
public IManager<Player> GlobalPlayerManager { get; private set; }
public IManager<KeyValuePair<string, IEnumerable<Die>>> GlobalDieManager { get; private set; }
private readonly List<Game> games;
public IManager<KeyValuePair<string, IEnumerable<Die>>> DieGroupManager { get; private set; }
public IManager<Game> GameManager { get; private set; }
public GameRunner(IManager<Player> globalPlayerManager, IManager<KeyValuePair<string, IEnumerable<Die>>> globalDieManager, List<Game> games)
public GameRunner(IManager<Player> globalPlayerManager, IManager<KeyValuePair<string, IEnumerable<Die>>> globalDieManager, IManager<Game> gameManager)
{
GlobalPlayerManager = globalPlayerManager;
GlobalDieManager = globalDieManager;
this.games = games ?? new();
}
public GameRunner(IManager<Player> globalPlayerManager, IManager<KeyValuePair<string, IEnumerable<Die>>> globalDieManager)
: this(globalPlayerManager, globalDieManager, null) { }
public IEnumerable<Game> GetAll() => games.AsEnumerable();
/// <summary>
/// finds the game with that name and returns it
/// <br/>
/// that copy does not belong to this gamerunner's games, so it should not be modified
/// </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)
{
if (!string.IsNullOrWhiteSpace(name))
{
Game result = games.FirstOrDefault(g => g.Name == name);
return result; // may return null
}
throw new ArgumentException("param should not be null or blank", nameof(name));
}
/// <summary>
/// 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)
{
if (toAdd is null)
{
throw new ArgumentNullException(nameof(toAdd), "param should not be 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;
DieGroupManager = globalDieManager;
GameManager = gameManager;
}
/// <summary>
/// creates a new game
/// </summary>
/// <param name="name"></param>
/// <param name="playerManager"></param>
/// <param name="dice"></param>
/// <returns></returns>
public Game StartNewGame(string name, IManager<Player> playerManager, IEnumerable<Die> dice)
{
Game game = new(name, playerManager, dice);
return Add(game);
}
public void Remove(Game toRemove)
{
if (toRemove is null)
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
}
games.Remove(toRemove);
}
public Game Update(Game before, Game after)
{
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);
return GameManager.Add(game);
}
/// <summary>
@ -106,9 +41,5 @@ namespace Model.Games
game.PrepareNextPlayer(current);
}
public Game GetOneByID(Guid ID)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,281 @@
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 GameRunner stubGameRunner = new Stub().LoadApp();
[Fact]
public void TestConstructorReturnsEmptyEnumerable()
{
// Arrange
GameManager gm = new();
IEnumerable<Game> expected;
IEnumerable<Game> actual;
// Act
expected = new Collection<Game>();
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<Game> expected = new List<Game>() { game1, game2 }.AsEnumerable();
IEnumerable<Game> actual = new List<Game>()
{
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<ArgumentNullException>(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<NotImplementedException>(action);
}
[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData(" ")]
public void TestGetOneByNameWhenInvalidThenThrowsException(string name)
{
// Arrange
GameManager gm = new();
// Act
void action() => gm.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(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<ArgumentNullException>(action);
}
[Fact]
public void TestRemoveWhenGivenNonExistentThenFailsSilently()
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
Game notGame = new("blargh", new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice);
IEnumerable<Game> expected = stubGameRunner.GameManager.GetAll();
// Act
gm.Remove(notGame);
IEnumerable<Game> 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<Game> 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<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
Assert.Equal(expectedSize, actualSize);
}
[Fact]
public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo()
{
// Arrange
IManager<Game> 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<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestUpdateDoesNotGoWithValidAfterAndNullBefore()
{
// Arrange
IManager<Game> 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<ArgumentNullException>(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<Game> 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<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gm.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
}
}

@ -13,284 +13,12 @@ namespace Tests.Model_UTs.Games
{
private readonly GameRunner stubGameRunner = new Stub().LoadApp();
[Fact]
public void TestConstructorWhenNoGamesThenNewIEnumerable()
{
// Arrange
GameRunner gameRunner = new(new PlayerManager(), new DieManager());
IEnumerable<Game> expected;
IEnumerable<Game> actual;
// Act
expected = new List<Game>().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<Game> expected;
IEnumerable<Game> 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<Game> expected = new List<Game>() { game1, game2 }.AsEnumerable();
IEnumerable<Game> actual = new List<Game>()
{
gameRunner.Add(game1),
gameRunner.Add(game2)
};
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestAddWhenNullThenThrowsException()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
// Act
void action() => gameRunner.Add(null);// Add() returns the added element if succesful
// Assert
Assert.Throws<ArgumentNullException>(action);
Assert.DoesNotContain(null, stubGameRunner.GetAll());
}
[Fact]
public void TestGetOneByIdThrowsException()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
// Act
void action() => gameRunner.GetOneByID(new("62bb72e6-f9fb-442e-a879-e1b70f8f52f3"));
// Assert
Assert.Throws<NotImplementedException>(action);
}
[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData(" ")]
public void TestGetOneByNameWhenInvalidThenThrowsException(string name)
{
// Arrange
GameRunner gameRunner = stubGameRunner;
// Act
void action() => gameRunner.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(action);
}
[Fact]
public void TestGetOneByNameWhenValidButNotExistThenReturnNull()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
// Act
Game result = gameRunner.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
GameRunner gameRunner = stubGameRunner;
Game game = new("blargh", new PlayerManager(), gameRunner.GetAll().First().Dice);
gameRunner.Add(game);
// Act
gameRunner.Remove(game);
// Assert
Assert.DoesNotContain(game, gameRunner.GetAll());
}
[Fact]
public void TestRemoveWhenGivenNullThenThrowsException()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
// Act
void action() => gameRunner.Remove(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
[Fact]
public void TestRemoveWhenGiveenNonExistentThenFailsSilently()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
Game notGame = new("blargh", new PlayerManager(), gameRunner.GetAll().First().Dice);
IEnumerable<Game> expected = gameRunner.GetAll();
// Act
gameRunner.Remove(notGame);
IEnumerable<Game> actual = gameRunner.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
GameRunner gameRunner = stubGameRunner;
int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act
void action() => gameRunner.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice));
int actualSize = gameRunner.GetAll().Count();
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act
void action() => gameRunner.Update(oldGame, null);
int actualSize = gameRunner.GetAll().Count();
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestUpdateDoesNotGoWithValidAfterAndNullBefore()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act
void action() => gameRunner.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice));
int actualSize = gameRunner.GetAll().Count();
// Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName)
{
// Arrange
GameRunner gameRunner = stubGameRunner;
int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act
void action() => gameRunner.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice));
int actualSize = gameRunner.GetAll().Count();
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestPlayGameWhenPlayThenAddNewTurnToHistory()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
Game game = gameRunner.GetAll().First();
Game game = gameRunner.GameManager.GetAll().First();
// Act
int turnsBefore = game.GetHistory().Count();
@ -309,11 +37,11 @@ namespace Tests.Model_UTs.Games
string name = "blargh";
// Act
Assert.DoesNotContain(gameRunner.GetOneByName(name), gameRunner.GetAll());
gameRunner.StartNewGame(name, new PlayerManager(), stubGameRunner.GetAll().First().Dice);
Assert.DoesNotContain(gameRunner.GameManager.GetOneByName(name), gameRunner.GameManager.GetAll());
gameRunner.StartNewGame(name, new PlayerManager(), stubGameRunner.GameManager.GetAll().First().Dice);
// Assert
Assert.Contains(gameRunner.GetOneByName(name), gameRunner.GetAll());
Assert.Contains(gameRunner.GameManager.GetOneByName(name), gameRunner.GameManager.GetAll());
}
}
}

@ -20,8 +20,8 @@ namespace Tests.Model_UTs.Games
private readonly IEnumerable<Die> DICE_1, DICE_2;
public GameTest()
{
DICE_1 = stubGameRunner.GlobalDieManager.GetAll().First().Value;
DICE_2 = stubGameRunner.GlobalDieManager.GetAll().Last().Value;
DICE_1 = stubGameRunner.DieGroupManager.GetAll().First().Value;
DICE_2 = stubGameRunner.DieGroupManager.GetAll().Last().Value;
}
@ -81,7 +81,7 @@ namespace Tests.Model_UTs.Games
public void TestGetHistory()
{
// Arrange
Dictionary<Die, Face> diceNFaces = (Dictionary<Die, Face>)stubGameRunner.GetAll().First().GetHistory().First().DiceNFaces;
Dictionary<Die, Face> diceNFaces = (Dictionary<Die, Face>)stubGameRunner.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

@ -19,8 +19,8 @@ namespace Tests.Model_UTs.Games
public TurnTest()
{
DICE_N_FACES_1 = (Dictionary<Die, Face>)stubGameRunner.GetAll().First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = (Dictionary<Die, Face>)stubGameRunner.GetAll().Last().GetHistory().Last().DiceNFaces;
DICE_N_FACES_1 = (Dictionary<Die, Face>)stubGameRunner.GameManager.GetAll().First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = (Dictionary<Die, Face>)stubGameRunner.GameManager.GetAll().Last().GetHistory().Last().DiceNFaces;
}
[Fact]

Loading…
Cancel
Save