Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/TennisGame.class
/TennisGameException.class
/TennisGameTest.class
Binary file modified bin/TennisGame.class
Binary file not shown.
Binary file modified bin/TennisGameTest.class
Binary file not shown.
6 changes: 3 additions & 3 deletions src/TennisGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public String getScore() {
return "player2 wins";
}

if (player1Points >= 4 && player1Points == player2Points)
if (player1Points >= 3 && player1Points == player2Points)
return "deuce";

if (player1Points >= 4 && player1Points - player2Points == 1)
return "player1 has advantage";

if (player2Points > 4 && player2Points - player1Points == 1)
if (player2Points >= 4 && player2Points - player1Points == 1)
return "player2 has advantage";

return player2Score + " - " + player1Score ;
return player1Score + " - " + player2Score;
}
}
180 changes: 177 additions & 3 deletions tests/TennisGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class TennisGameTest {
// "player2 has advantage"
// "player1 wins"
// "player2 wins"
@Ignore
public void testTennisGame_Start() {
@Test
public void testTennisGame_Start_Score_love_love() {
//Arrange
TennisGame game = new TennisGame();
//Act
Expand All @@ -30,6 +30,98 @@ public void testTennisGame_Start() {
assertEquals("Initial score incorrect", "love - love", score);
}

@Test
public void testTennisGame_Player1Win1stBall_Score_15_love() throws TennisGameException {
TennisGame game = new TennisGame();
game.player1Scored();
String score = game.getScore();

assertEquals("Player1 1st score incorrect","15 - love", score);
}

@Test
public void testTennisGame_Player2Win1stBall_Score_love_15() throws TennisGameException {
TennisGame game = new TennisGame();
game.player2Scored();
String score = game.getScore();

assertEquals("Player2 1st score incorrect","love - 15", score);
}

@Test
public void testTennisGame_EachPlayerWin1Point_Score_15_15() throws TennisGameException {
TennisGame game = new TennisGame();

game.player1Scored();

game.player2Scored();

String score = game.getScore();

assertEquals("Player1,2 both 1st score incorrect","15 - 15", score);
}

@Test
public void testTennisGame_EachPlayerWin2Points_Score_30_30() throws TennisGameException {
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("Player1,2 both 1st score incorrect","30 - 30", score);
}

@Test
public void testTennisGame_Player1Win4Points() throws TennisGameException {
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();
game.player1Scored();

String score = game.getScore();

assertEquals("Player1 win 4 points incorrect","player1 wins", score);
}

@Test
public void testTennisGame_Player2Win4Points() throws TennisGameException {
TennisGame game = new TennisGame();

game.player2Scored();
game.player2Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("Player2 win 4 points incorrect","player2 wins", score);
}

@Test
public void testTennisGame_EachPlayerWin3Points_Score_Deuce() throws TennisGameException {
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("Player1,2 win 3 balls deuce incorrect","deuce", score);
}


@Test
public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException {
//Arrange
Expand Down Expand Up @@ -63,5 +155,87 @@ public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() thr
//Act
// This statement should cause an exception
game.player1Scored();
}
}

@Test
public void testTennisGame_Player1Advantage() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();
game.player2Scored();

game.player1Scored();
//Act
String score = game.getScore() ;
// Assert
assertEquals("Player1 Advantage is incorrect", "player1 has advantage", score);
}

@Test
public void testTennisGame_Player2Advantage() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();
game.player2Scored();

game.player2Scored();
//Act
String score = game.getScore() ;
// Assert
assertEquals("Player2 Advantage is incorrect", "player2 has advantage", score);
}

@Test
public void testTennisGame_Player1Win_AfterAdvantage() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();
game.player2Scored();

game.player1Scored();
game.player1Scored();
//Act
String score = game.getScore() ;
// Assert
assertEquals("Player1 win after advantage is incorrect", "player1 wins", score);
}

@Test
public void testTennisGame_Player2Win_AfterAdvantage() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();

game.player1Scored();
game.player1Scored();
game.player1Scored();

game.player2Scored();
game.player2Scored();
game.player2Scored();

game.player2Scored();
game.player2Scored();
//Act
String score = game.getScore() ;
// Assert
assertEquals("Player2 win after advantage is incorrect", "player2 wins", score);
}
}