♻️ Fix #28 Refactor UTs and hope that SonarQube updates coverage #96

Merged
alexis.drai merged 1 commits from fix-#28 into main 3 years ago

@ -67,10 +67,10 @@ namespace Model.Games
/// creates a new game /// creates a new game
/// </summary> /// </summary>
/// <exception cref="NotSupportedException"></exception> /// <exception cref="NotSupportedException"></exception>
public void StartNewGame(string name, PlayerManager playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice) public Game StartNewGame(string name, PlayerManager playerManager, IEnumerable<AbstractDie<AbstractDieFace>> dice)
{ {
Game game = new(name, playerManager, dice); Game game = new(name, playerManager, dice);
Add(game); return Add(game);
} }
public void Remove(Game toRemove) public void Remove(Game toRemove)

@ -1,13 +1,10 @@
using Model.Games; using Data;
using Model.Dice; using Model.Dice;
using Model.Dice.Faces; using Model.Games;
using Model.Players; using Model.Players;
using Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit; using Xunit;
namespace Tests.Model_UTs namespace Tests.Model_UTs
@ -76,10 +73,10 @@ namespace Tests.Model_UTs
public void TestAddWhenNullThenThrowsException() public void TestAddWhenNullThenThrowsException()
{ {
// Arrange // Arrange
GameRunner gameRunner = stubGameRunner;
// Act // Act
void action() => stubGameRunner.Add(null);// Add() returns the added element if succesful void action() => gameRunner.Add(null);// Add() returns the added element if succesful
// Assert // Assert
Assert.Throws<ArgumentNullException>(action); Assert.Throws<ArgumentNullException>(action);
@ -93,10 +90,10 @@ namespace Tests.Model_UTs
public void TestGetOneByNameWhenInvalidThenThrowsException(string name) public void TestGetOneByNameWhenInvalidThenThrowsException(string name)
{ {
// Arrange // Arrange
GameRunner gameRunner = stubGameRunner;
// Act // Act
void action() => stubGameRunner.GetOneByName(name); void action() => gameRunner.GetOneByName(name);
// Assert // Assert
Assert.Throws<ArgumentException>(action); Assert.Throws<ArgumentException>(action);
@ -106,10 +103,10 @@ namespace Tests.Model_UTs
public void TestGetOneByNameWhenValidButNotExistThenReturnNull() public void TestGetOneByNameWhenValidButNotExistThenReturnNull()
{ {
// Arrange // Arrange
GameRunner gameRunner = stubGameRunner;
// Act // Act
Game result = stubGameRunner.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists"); Game result = gameRunner.GetOneByName("thereisbasicallynowaythatthisgamenamealreadyexists");
// Assert // Assert
Assert.Null(result); Assert.Null(result);
@ -134,24 +131,25 @@ namespace Tests.Model_UTs
public void TestWhenRemoveExistsThenSucceeds() public void TestWhenRemoveExistsThenSucceeds()
{ {
// Arrange // Arrange
Game game = new("blargh", new PlayerManager(), stubGameRunner.GetAll().First().Dice); GameRunner gameRunner = stubGameRunner;
stubGameRunner.Add(game); Game game = new("blargh", new PlayerManager(), gameRunner.GetAll().First().Dice);
gameRunner.Add(game);
// Act // Act
stubGameRunner.Remove(game); gameRunner.Remove(game);
// Assert // Assert
Assert.DoesNotContain(game, stubGameRunner.GetAll()); Assert.DoesNotContain(game, gameRunner.GetAll());
} }
[Fact] [Fact]
public void TestRemoveWhenGivenNullThenThrowsException() public void TestRemoveWhenGivenNullThenThrowsException()
{ {
// Arrange // Arrange
GameRunner gameRunner = stubGameRunner;
// Act // Act
void action() => stubGameRunner.Remove(null); void action() => gameRunner.Remove(null);
// Assert // Assert
Assert.Throws<ArgumentNullException>(action); Assert.Throws<ArgumentNullException>(action);
@ -161,12 +159,13 @@ namespace Tests.Model_UTs
public void TestRemoveWhenGiveenNonExistentThenFailsSilently() public void TestRemoveWhenGiveenNonExistentThenFailsSilently()
{ {
// Arrange // Arrange
Game notGame = new("blargh", new PlayerManager(), stubGameRunner.GetAll().First().Dice); GameRunner gameRunner = stubGameRunner;
IEnumerable<Game> expected = stubGameRunner.GetAll(); Game notGame = new("blargh", new PlayerManager(), gameRunner.GetAll().First().Dice);
IEnumerable<Game> expected = gameRunner.GetAll();
// Act // Act
stubGameRunner.Remove(notGame); gameRunner.Remove(notGame);
IEnumerable<Game> actual = stubGameRunner.GetAll(); IEnumerable<Game> actual = gameRunner.GetAll();
// Assert // Assert
Assert.Equal(actual, expected); Assert.Equal(actual, expected);
@ -205,16 +204,17 @@ namespace Tests.Model_UTs
public void TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGo(string badName) public void TestUpdateWhenValidBeforeAndInvalidAfterThenDoesNotGo(string badName)
{ {
// Arrange // Arrange
int expectedSize = stubGameRunner.GetAll().Count(); GameRunner gameRunner = stubGameRunner;
Game oldGame = stubGameRunner.GetAll().First(); int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act // Act
void action() => stubGameRunner.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice)); void action() => gameRunner.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice));
int actualSize = stubGameRunner.GetAll().Count(); int actualSize = gameRunner.GetAll().Count();
// Assert // Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize); Assert.True(expectedSize == actualSize);
} }
@ -222,16 +222,17 @@ namespace Tests.Model_UTs
public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo() public void TestUpdateWhenValidBeforeAndNullAfterThenDoesNotGo()
{ {
// Arrange // Arrange
int expectedSize = stubGameRunner.GetAll().Count(); GameRunner gameRunner = stubGameRunner;
Game oldGame = stubGameRunner.GetAll().First(); int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act // Act
void action() => stubGameRunner.Update(oldGame, null); void action() => gameRunner.Update(oldGame, null);
int actualSize = stubGameRunner.GetAll().Count(); int actualSize = gameRunner.GetAll().Count();
// Assert // Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize); Assert.True(expectedSize == actualSize);
} }
@ -239,16 +240,17 @@ namespace Tests.Model_UTs
public void TestUpdateDoesNotGoWithValidAfterAndNullBefore() public void TestUpdateDoesNotGoWithValidAfterAndNullBefore()
{ {
// Arrange // Arrange
int expectedSize = stubGameRunner.GetAll().Count(); GameRunner gameRunner = stubGameRunner;
Game oldGame = stubGameRunner.GetAll().First(); int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act // Act
void action() => stubGameRunner.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice)); void action() => gameRunner.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice));
int actualSize = stubGameRunner.GetAll().Count(); int actualSize = gameRunner.GetAll().Count();
// Assert // Assert
Assert.Throws<ArgumentNullException>(action); // thrown by constructor Assert.Throws<ArgumentNullException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize); Assert.True(expectedSize == actualSize);
} }
@ -259,17 +261,49 @@ namespace Tests.Model_UTs
public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName) public void TestUpdateWhenInvalidBeforeAndValidAfterThenDoesNotGo(string badName)
{ {
// Arrange // Arrange
int expectedSize = stubGameRunner.GetAll().Count(); GameRunner gameRunner = stubGameRunner;
Game oldGame = stubGameRunner.GetAll().First(); int expectedSize = gameRunner.GetAll().Count();
Game oldGame = gameRunner.GetAll().First();
// Act // Act
void action() => stubGameRunner.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice)); void action() => gameRunner.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice));
int actualSize = stubGameRunner.GetAll().Count(); int actualSize = gameRunner.GetAll().Count();
// Assert // Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor Assert.Throws<ArgumentException>(action); // thrown by constructor
Assert.Contains(oldGame, stubGameRunner.GetAll()); // still there Assert.Contains(oldGame, gameRunner.GetAll()); // still there
Assert.True(expectedSize == actualSize); Assert.True(expectedSize == actualSize);
} }
[Fact]
public void TestPlayGameWhenPlayThenAddNewTurnToHistory()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
Game game = gameRunner.GetAll().First();
// Act
int turnsBefore = game.GetHistory().Count();
GameRunner.PlayGame(game);
int turnsAfter = game.GetHistory().Count();
// Assert
Assert.Equal(turnsBefore + 1, turnsAfter);
}
[Fact]
public void TestStartNewGame()
{
// Arrange
GameRunner gameRunner = stubGameRunner;
string name = "blargh";
// Act
Assert.DoesNotContain(gameRunner.GetOneByName(name), gameRunner.GetAll());
gameRunner.StartNewGame(name, new PlayerManager(), stubGameRunner.GetAll().First().Dice);
// Assert
Assert.Contains(gameRunner.GetOneByName(name), gameRunner.GetAll());
}
} }
} }

Loading…
Cancel
Save