Source

Kata Tennis C++ / src / test / GameTests.cpp

#include <CppUTest/TestHarness.h>

#include "../Game.h"
#include "../Player.h"

namespace PlayersNames
{
	const std::string PLAYER_ONE_NAME = "koko";
	const std::string PLAYER_TWO_NAME = "triki";
}

TEST_GROUP(GameTests)
{
	Game * game;
	Player * playerOne;
	Player * playerTwo;

	void setup() {

		playerOne = new Player(PlayersNames::PLAYER_ONE_NAME);
		playerTwo = new Player(PlayersNames::PLAYER_TWO_NAME);
		game = new Game(playerOne, playerTwo);
	}

	void teardown() {
		delete game;
	}

	void makePlayerWinSeveralPoints(Player * player, unsigned int numPoints)
	{
		for(unsigned int i=0; i < numPoints; ++i)
		{
			game->pointTo(player);
		}
	}

	void gameGetToDeuce()
	{
		makePlayerWinSeveralPoints(playerOne, 3);
		makePlayerWinSeveralPoints(playerTwo, 3);
	}

	void makePlayerWin(Player * player)
	{
		makePlayerWinSeveralPoints(player, 4);
	}
};

TEST(GameTests, initialScoreIsZero)
{
	CHECK("Love-Love" == game->score());
}

TEST(GameTests, playersCanWinSeveralPoints)
{
	game->pointTo(playerOne);
	CHECK("Fifteen-Love" == game->score());

	game->pointTo(playerOne);
	CHECK("Thirty-Love" == game->score());

	game->pointTo(playerOne);
	CHECK("Forty-Love" == game->score());

	game->pointTo(playerTwo);
	CHECK("Forty-Fifteen" == game->score());

	game->pointTo(playerTwo);
	CHECK("Forty-Thirty" == game->score());
}

TEST(GameTests, gameIsOverAfterPlayerOneWinsFirstForPoints)
{
	makePlayerWin(playerOne);
	CHECK(game->gameStateIsGameOver());
}

TEST(GameTests, gameIsOverAfterPlayerTwoWinsFirstForPoints)
{
	makePlayerWin(playerTwo);
	CHECK(game->gameStateIsGameOver());
}

TEST(GameTests, scoreDoesNotChangeAfterAPlayerWins)
{
	makePlayerWin(playerOne);

	std::string PLAYER_ONE_WON = PlayersNames::PLAYER_ONE_NAME + " won!";

	game->pointTo(playerOne);
	CHECK(PLAYER_ONE_WON == game->score());

	game->pointTo(playerTwo);
	CHECK(PLAYER_ONE_WON == game->score());
}

TEST(GameTests, gameIsNotOverIfNoOneGetsFirstToForty)
{
	CHECK(not game->gameStateIsGameOver());
	makePlayerWinSeveralPoints(playerOne, 3);
	makePlayerWinSeveralPoints(playerTwo, 3);
	CHECK(not game->gameStateIsGameOver());
}

TEST(GameTests, thereIsDeuce)
{
	gameGetToDeuce();
	CHECK("Deuce" == game->score());
}

TEST(GameTests, gameIsNotOverWhenThereIsDeuce)
{
	gameGetToDeuce();
	CHECK(not game->gameStateIsGameOver());
}

TEST(GameTests, thereIsAdvantage)
{
	gameGetToDeuce();
	game->pointTo(playerOne);

	CHECK("Advantage for koko" == game->score());
}

TEST(GameTests, backToDeuce)
{
	gameGetToDeuce();
	game->pointTo(playerOne);
	game->pointTo(playerTwo);
	CHECK("Deuce" == game->score());
}

TEST(GameTests, thePlayerWithAdvantageWins)
{
	gameGetToDeuce();
	game->pointTo(playerOne); // Advantage for player one
	game->pointTo(playerTwo); // Deuce again
	game->pointTo(playerTwo); // Advantage for player two
	game->pointTo(playerTwo);

	std::string PLAYER_TWO_WON = PlayersNames::PLAYER_TWO_NAME + " won!";

	CHECK(PLAYER_TWO_WON == game->score());
}