Complete test suite, add checks to R and U
continuous-integration/drone/push Build is passing Details

pull/95/head
Alexis Drai 3 years ago
parent 8d8c2f445a
commit 455f1210ef

@ -56,14 +56,12 @@ namespace Model.Games
{
throw new ArgumentNullException(nameof(toAdd), "param should not be null");
}
else
{
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>
/// creates a new game
@ -75,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);
}
/// <summary>

@ -173,74 +173,103 @@ namespace Tests.Model_UTs
}
[Fact]
public void TestUpdateWorksIfValid()
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 TestUpdateDoesNotGoWithValidBeforeAndInvalidAfter(string badName)
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<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Fact]
public void TestUpdateDoesNotGoWithValidBeforeAndNullAfter()
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<ArgumentNullException>(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<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateDoesNotGoWithValidAfterAndInvalidBefore(string name)
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<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize);
}
}
}

Loading…
Cancel
Save