Add UTs for Game, 🐛 debug nextIndex
continuous-integration/drone/push Build is passing Details

pull/83/head
Alexis Drai 3 years ago
parent f0e74ae517
commit 2ea6feec0d

@ -97,7 +97,6 @@ namespace Model.Games
ThrowAll() ThrowAll()
); );
turns.Add(turn); turns.Add(turn);
nextIndex++;
} }
/// <summary> /// <summary>
@ -111,7 +110,6 @@ namespace Model.Games
{ {
throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened"); throw new MemberAccessException("you are exploring an empty collection\nthis should not have happened");
} }
return playerManager.GetAll().ElementAt(nextIndex); return playerManager.GetAll().ElementAt(nextIndex);
} }
@ -136,7 +134,6 @@ namespace Model.Games
{ {
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current)); throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
} }
if (playerManager.GetAll().Last() == current) if (playerManager.GetAll().Last() == current)
{ {
// if we've reached the last player, we need the index to loop back around // if we've reached the last player, we need the index to loop back around
@ -190,16 +187,20 @@ namespace Model.Games
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new(); StringBuilder sb = new();
sb.AppendFormat("Game: {0}===========\n" + sb.Append($"Game: {Name}");
"{1} are playing. {2} is next.\n" +
"Log:\n", sb.Append("\nPlayers:");
Name, foreach (Player player in GetPlayersFromGame())
playerManager.GetAll().ToString(), {
GetWhoPlaysNow()); sb.Append($" {player.ToString()}");
}
sb.Append($"\nNext: {GetWhoPlaysNow()}");
sb.Append("\nLog:\n");
foreach (Turn turn in this.turns) foreach (Turn turn in this.turns)
{ {
sb.Append("\t" + turn.ToString()); sb.Append($"\t{turn.ToString()}\n");
} }
return sb.ToString(); return sb.ToString();

@ -140,7 +140,38 @@ namespace Tests.Model_UTs
} }
[Fact] [Fact]
public void TestGetWhoPlaysNow() public void TestPerformTurnDoesAddOneTurn()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
game.AddPlayerToGame(PLAYER_1);
game.AddPlayerToGame(PLAYER_2);
int n = 5;
IEnumerable<Player> players = game.GetPlayersFromGame();
Debug.WriteLine(players);
Player currentPlayer;
for (int i = 0; i < n; i++)
{
currentPlayer = game.GetWhoPlaysNow();
game.PerformTurn(currentPlayer);
game.PrepareNextPlayer(currentPlayer);
}
Debug.WriteLine(game);
// Act
int actual = game.GetHistory().Count();
int expected = n;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetWhoPlaysNowWhenValidThenCorrect()
{ {
// Arrange // Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE); Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
@ -163,28 +194,142 @@ namespace Tests.Model_UTs
} }
[Fact] [Fact]
public void TestPerformTurnDoesAddOneTurn() public void TestGetWhoPlaysNowWhenInvalidThenException()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
// Act
void action() => game.GetWhoPlaysNow(); // on an empty collection of players
// Assert
Assert.Throws<MemberAccessException>(action);
}
[Fact]
public void TestPrepareNextPlayerWhenEmptyThenException()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
// Act
void action() => game.PrepareNextPlayer(PLAYER_1); // on an empty collection of players
// Assert
Assert.Throws<MemberAccessException>(action);
}
[Fact]
public void TestPrepareNextPlayerWhenNullThenException()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
game.AddPlayerToGame(PLAYER_1);
// Act
void action() => game.PrepareNextPlayer(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
[Fact]
public void TestPrepareNextPlayerWhenNonExistentThenException()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
game.AddPlayerToGame(PLAYER_2);
// Act
void action() => game.PrepareNextPlayer(PLAYER_3);
// Assert
Assert.Throws<ArgumentException>(action);
}
[Fact]
public void TestPrepareNextPlayerWhenValidThenCorrectWithSeveralPlayers()
{ {
// Arrange // Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE); Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
game.AddPlayerToGame(PLAYER_1); game.AddPlayerToGame(PLAYER_1);
game.AddPlayerToGame(PLAYER_2); game.AddPlayerToGame(PLAYER_2);
int n = 5; // Act
Player expected = PLAYER_2;
IEnumerable<Player> players = game.GetPlayersFromGame(); Assert.Equal(PLAYER_1, game.GetWhoPlaysNow());
Debug.WriteLine(players); game.PrepareNextPlayer(PLAYER_1);
for (int i = 0; i < n; i++) Player actual = game.GetWhoPlaysNow();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestPrepareNextPlayerWhenValidThenCorrectWithOnePlayer()
{
// Arrange
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE);
game.AddPlayerToGame(PLAYER_1);
// Act
Player expected = PLAYER_1;
Assert.Equal(PLAYER_1, game.GetWhoPlaysNow());
game.PrepareNextPlayer(PLAYER_1);
Player actual = game.GetWhoPlaysNow();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestToString()
{
// Arrange
DateTime dateTime = DateTime.UtcNow;
List<Turn> turns = new()
{ {
Player currentPlayer = game.GetWhoPlaysNow(); Turn.CreateWithSpecifiedTime(dateTime, PLAYER_1, new()
game.PerformTurn(currentPlayer); {
game.PrepareNextPlayer(currentPlayer); {NUM, new NumberDieFace(4)},
} {IMG, new ImageDieFace(40)},
{CLR, new ColorDieFace("A00FA0")},
}),
Turn.CreateWithSpecifiedTime(dateTime, PLAYER_2, new()
{
{NUM, new NumberDieFace(3)},
{IMG, new ImageDieFace(20)},
{CLR, new ColorDieFace("A00BB8")},
}),
};
Game game = new(name: GAME_NAME, playerManager: new PlayerManager(), dice: DICE, turns: turns);
game.AddPlayerToGame(PLAYER_1);
game.AddPlayerToGame(PLAYER_2);
// Act // Act
int actual = game.GetHistory().Count(); string[] dateTimeString = dateTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
int expected = n;
string date = dateTimeString[0];
string time = dateTimeString[1];
string expected =
"Game: my game" +
"\nPlayers: Alice Bob" +
"\nNext: Alice" +
"\nLog:" +
"\n\t" + date + " " + time + " -- Alice rolled: 4 Assets/images/40 #A00FA0" +
"\n\t" + date + " " + time + " -- Bob rolled: 3 Assets/images/20 #A00BB8" +
"\n";
string actual = game.ToString();
Debug.WriteLine("expected:\n" + expected);
Debug.WriteLine("actual:\n" + actual);
// Assert // Assert
Assert.Equal(expected, actual); Assert.Equal(expected, actual);

Loading…
Cancel
Save