diff --git a/bin/TennisGame.class b/bin/TennisGame.class index 2da4e7a..7245097 100644 Binary files a/bin/TennisGame.class and b/bin/TennisGame.class differ diff --git a/bin/TennisGameTest.class b/bin/TennisGameTest.class index b202db2..eb73d41 100644 Binary files a/bin/TennisGameTest.class and b/bin/TennisGameTest.class differ diff --git a/src/TennisGame.java b/src/TennisGame.java index 327f284..b5eac74 100644 --- a/src/TennisGame.java +++ b/src/TennisGame.java @@ -1,91 +1,91 @@ -// This implementation is used for practicing unit tests. -// NOTE THAT it may contain bugs -// Write unit tests in TennisGameTest.java and try to find the errors in the code - -public class TennisGame { - private int player1Points; - private int player2Points; - - private boolean gameEnded; - - public TennisGame() { - player1Points = 0; - player2Points = 0; - gameEnded = false ; - } - - private void checkGameEnded() { - if (player1Points>=4 && player1Points-player2Points>=2) - gameEnded = true; - else if (player2Points>=4 && player2Points-player1Points>=2) - gameEnded = true; - } - - private String getScore(int points) { - switch (points) { - case 0: return "love"; - case 1: return "15" ; - case 2: return "30" ; - case 3: return "40"; - default: return "40" ; - } - } - - public void player1Scored() throws TennisGameException { - if (gameEnded) { - throw new TennisGameException(); - } - else { - player1Points++; - checkGameEnded(); - } - } - - public void player2Scored() throws TennisGameException { - if (gameEnded) { - throw new TennisGameException(); - } - else { - player2Points++; - checkGameEnded(); - } - } - - public String getScore() { -// Here is the format of the scores: -// "love - love" -// "15 - 15" -// "30 - 30" -// "deuce" -// "15 - love", "love - 15" -// "30 - love", "love - 30" -// "40 - love", "love - 40" -// "30 - 15", "15 - 30" -// "40 - 15", "15 - 40" -// "player1 has advantage" -// "player2 has advantage" -// "player1 wins" -// "player2 wins" - - String player1Score = getScore(player1Points); - String player2Score = getScore(player2Points); - - if (gameEnded) { - if (player1Points > player2Points) - return "player1 wins"; - else - return "player2 wins"; - } - - if (player1Points >= 4 && player1Points == player2Points) - return "deuce"; - - if (player1Points >= 4 && player1Points - player2Points == 1) - return "player1 has advantage"; - - if (player2Points > 4 && player2Points - player1Points == 1) - return "player2 has advantage"; - - return player2Score + " - " + player1Score ; - } +// This implementation is used for practicing unit tests. +// NOTE THAT it may contain bugs +// Write unit tests in TennisGameTest.java and try to find the errors in the code + +public class TennisGame { + private int player1Points; + private int player2Points; + + private boolean gameEnded; + + public TennisGame() { + player1Points = 0; + player2Points = 0; + gameEnded = false ; + } + + private void checkGameEnded() { + if (player1Points>=4 && player1Points-player2Points>=2) + gameEnded = true; + else if (player2Points>=4 && player2Points-player1Points>=2) + gameEnded = true; + } + + private String getScore(int points) { + switch (points) { + case 0: return "love"; + case 1: return "15" ; + case 2: return "30" ; + case 3: return "40"; + default: return "40" ; + } + } + + public void player1Scored() throws TennisGameException { + if (gameEnded) { + throw new TennisGameException(); + } + else { + player1Points++; + checkGameEnded(); + } + } + + public void player2Scored() throws TennisGameException { + if (gameEnded) { + throw new TennisGameException(); + } + else { + player2Points++; + checkGameEnded(); + } + } + + public String getScore() { +// Here is the format of the scores: +// "love - love" +// "15 - 15" +// "30 - 30" +// "deuce" +// "15 - love", "love - 15" +// "30 - love", "love - 30" +// "40 - love", "love - 40" +// "30 - 15", "15 - 30" +// "40 - 15", "15 - 40" +// "player1 has advantage" +// "player2 has advantage" +// "player1 wins" +// "player2 wins" + + String player1Score = getScore(player1Points); + String player2Score = getScore(player2Points); + + if (gameEnded) { + if (player1Points > player2Points) + return "player1 wins"; + else + return "player2 wins"; + } + + if (player1Points >= 4 && player1Points == player2Points) + return "deuce"; + + if (player1Points >= 4 && player1Points - player2Points == 1) + return "player1 has advantage"; + + if (player2Points >= 4 && player2Points - player1Points == 1) + return "player2 has advantage"; + + return player1Score + " - " + player2Score ; + } } \ No newline at end of file diff --git a/tests/TennisGameTest.java b/tests/TennisGameTest.java index 8674eba..16072e7 100644 --- a/tests/TennisGameTest.java +++ b/tests/TennisGameTest.java @@ -1,67 +1,199 @@ -import static org.junit.Assert.*; - -import org.junit.Test; - -import jdk.nashorn.internal.ir.annotations.Ignore; - -public class TennisGameTest { - -// Here is the format of the scores: "player1Score - player2Score" -// "love - love" -// "15 - 15" -// "30 - 30" -// "deuce" -// "15 - love", "love - 15" -// "30 - love", "love - 30" -// "40 - love", "love - 40" -// "30 - 15", "15 - 30" -// "40 - 15", "15 - 40" -// "player1 has advantage" -// "player2 has advantage" -// "player1 wins" -// "player2 wins" - @Ignore - public void testTennisGame_Start() { - //Arrange - TennisGame game = new TennisGame(); - //Act - String score = game.getScore() ; - // Assert - assertEquals("Initial score incorrect", "love - love", score); - } - - @Test - public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException { - //Arrange - TennisGame game = new TennisGame(); - - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - - game.player2Scored(); - game.player2Scored(); - game.player2Scored(); - - game.player1Scored(); - game.player2Scored(); - //Act - String score = game.getScore() ; - // Assert - assertEquals("Tie score incorrect", "deuce", score); - } - - @Test (expected = TennisGameException.class) - public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() throws TennisGameException { - //Arrange - TennisGame game = new TennisGame(); - //Act - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - //Act - // This statement should cause an exception - game.player1Scored(); - } -} +import static org.junit.Assert.*; + +import org.junit.Test; + +import jdk.nashorn.internal.ir.annotations.Ignore; + +public class TennisGameTest { + +// Here is the format of the scores: "player1Score - player2Score" +// "love - love" +// "15 - 15" +// "30 - 30" +// "deuce" +// "15 - love", "love - 15" +// "30 - love", "love - 30" +// "40 - love", "love - 40" +// "30 - 15", "15 - 30" +// "40 - 15", "15 - 40" +// "player1 has advantage" +// "player2 has advantage" +// "player1 wins" +// "player2 wins" + @Ignore + public void testTennisGame_Start() { + //Arrange + TennisGame game = new TennisGame(); + //Act + String score = game.getScore(); + // Assert + assertEquals("Initial score incorrect", "love - love", score); + } + + @Test + public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException { + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player1Scored(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Tie score incorrect", "deuce", score); + } + + @Test + public void testTennisGame_ScoreShouldBeLove15forPlayer2() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Score is love - 15", "love - 15", score); + } + + @Test + public void testTennisGame_ScoreShouldBe15LoveforPlayer1() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + String score = game.getScore(); + assertEquals("Score is 15 - love", "15 - love", score); + } + + @Test + public void testTennisGame_ScoreShouldBe3015Player1() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Score is 30 - 15", "30 - 15", score); + } + + @Test + public void testTennisGame_ScoreShouldBe4015Player1() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player1Scored(); + String score = game.getScore(); + assertEquals("Score is 40 - 15", "40 - 15", score); + } + + @Test + public void testTennisGame_Player1ShouldWin() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + String score = game.getScore(); + assertEquals("Player1 wins staright", "player1 wins", score); + } + + @Test + public void testTennisGame_Player2ShouldWin() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Player2 wins staright", "player2 wins", score); + } + + @Test + public void testTennisGame_Player1ShouldHaveAdvantage() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player1Scored(); + String score = game.getScore(); + assertEquals("Player1 has advantage", "player1 has advantage", score); + } + + @Test + public void testTennisGame_Player1ShouldWinAfterAdvantage() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player1Scored(); + String score = game.getScore(); + assertEquals("Player1 has advantage", "player1 has advantage", score); + game.player1Scored(); + score = game.getScore(); + assertEquals("Player1 wins after advantage", "player1 wins", score); + } + + @Test + public void testTennisGame_Player2ShouldHaveAdvantage() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Player2 has advantage", "player2 has advantage", score); + } + + @Test + public void testTennisGame_Player2ShouldWinAfterAdvantage() throws TennisGameException { + TennisGame game = new TennisGame(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + String score = game.getScore(); + assertEquals("Player2 has advantage", "player2 has advantage", score); + game.player2Scored(); + score = game.getScore(); + assertEquals("Player2 wins after advantage", "player2 wins", score); + } + + @Test (expected = TennisGameException.class) + public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() throws TennisGameException { + //Arrange + TennisGame game = new TennisGame(); + //Act + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + //Act + // This statement should cause an exception + game.player1Scored(); + } + + @Test (expected = TennisGameException.class) + public void testTennisGame_Player2WinsPointAfterGameEnded_ResultsException() throws TennisGameException { + //Arrange + TennisGame game = new TennisGame(); + //Act + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + //Act + // This statement should cause an exception + game.player2Scored(); + } +}