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
Binary file modified bin/TennisGame.class
Binary file not shown.
Binary file modified bin/TennisGameException.class
Binary file not shown.
Binary file modified bin/TennisGameTest.class
Binary file not shown.
14 changes: 7 additions & 7 deletions src/TennisGame.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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

//Try fix bugs
public class TennisGame {
private int player1Points;
private int player2Points;
Expand All @@ -11,7 +11,7 @@ public class TennisGame {
public TennisGame() {
player1Points = 0;
player2Points = 0;
gameEnded = false ;
gameEnded = false;
}

private void checkGameEnded() {
Expand All @@ -24,10 +24,10 @@ else if (player2Points>=4 && player2Points-player1Points>=2)
private String getScore(int points) {
switch (points) {
case 0: return "love";
case 1: return "15" ;
case 2: return "30" ;
case 1: return "15";
case 2: return "30";
case 3: return "40";
default: return "40" ;
default: return "40";
}
}

Expand Down Expand Up @@ -83,9 +83,9 @@ public String getScore() {
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 player2Score + " - " + player1Score;
}
}
162 changes: 157 additions & 5 deletions tests/TennisGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.junit.Test;

import jdk.nashorn.internal.ir.annotations.Ignore;

public class TennisGameTest {

// Here is the format of the scores: "player1Score - player2Score"
Expand All @@ -20,8 +18,9 @@ public class TennisGameTest {
// "player2 has advantage"
// "player1 wins"
// "player2 wins"
@Ignore
@Test
public void testTennisGame_Start() {
//Both have zero points
//Arrange
TennisGame game = new TennisGame();
//Act
Expand Down Expand Up @@ -62,6 +61,159 @@ public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() thr
game.player1Scored();
//Act
// This statement should cause an exception
game.player1Scored();
}
game.player1Scored();
String score = game.getScore();

//Assert
assertEquals("Player 1 win", "40", score);
}

@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
//T�M� EI TOIMI VIEL� OIKEIN!!!!!!!
game.player2Scored();
String score = game.getScore();

//Assert
assertEquals("Player 2 win", "40", score);
}
@Test
public void testTennisGame_Player1Have1MorePoint() throws TennisGameException {
TennisGame game = new TennisGame();

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

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

game.player1Scored();

String score = game.getScore() ;
// Assert
assertEquals("Player1 has advantage", "player1 has advantage", score);


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

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

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

game.player2Scored();

String score = game.getScore() ;
// Assert
assertEquals("Player2 has advantage", "player2 has advantage", score);
}
@Test
public void testTennisGame_Player1Have1Point() throws TennisGameException {
TennisGame game = new TennisGame();

game.player1Scored();

String score = game.getScore();

assertEquals("love - 15", "love - 15", score);
}
@Test
public void testTennisGame_Player1Have2Point() throws TennisGameException {
TennisGame game = new TennisGame();

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

String score = game.getScore();

assertEquals("love - 30", "love - 30", score);
}

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

game.player2Scored();

String score = game.getScore();

assertEquals("15 - love", "15 - love", score);
}
@Test
public void testTennisGame_Player2Have2Point() throws TennisGameException {
TennisGame game = new TennisGame();

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

String score = game.getScore();

assertEquals("30 - love", "30 - love", score);
}

@Test
public void testTennisGame_Player1Have1Point_Player2Have1Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("15 - 15", "15 - 15", score) ;
}

@Test
public void testTennisGame_Player1Have2Point_Player2Have1Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player1Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("15 - 30", "15 - 30", score) ;
}

@Test
public void testTennisGame_Player1Have1Point_Player2Have2Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("30 - 15", "30 - 15", score) ;
}

@Test
public void testTennisGame_Player1Have2Point_Player2Have2Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player1Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("30 - 30", "30 - 30", score) ;
}

}
56 changes: 56 additions & 0 deletions tests/TennisGameTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import static org.junit.Assert.*;

import org.junit.Test;

public class TennisGameTest2 {
@Test
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_Player1Have1MorePoint() throws TennisGameException {
TennisGame game = new TennisGame();

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

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

game.player1Scored();

String score = game.getScore() ;
// Assert
assertEquals("Player1 has advantage", "player1 has advantage", score);


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

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

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

game.player2Scored();

String score = game.getScore() ;
// Assert
assertEquals("Player2 has advantage", "player2 has advantage", score);


}
}
98 changes: 98 additions & 0 deletions tests/TennisGameTest3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import static org.junit.Assert.*;

import org.junit.Test;

public class TennisGameTest3 {

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

game.player1Scored();

String score = game.getScore();

assertEquals("love - 15", "love - 15", score);
}
@Test
public void testTennisGame_Player1Have2Point() throws TennisGameException {
TennisGame game = new TennisGame();

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

String score = game.getScore();

assertEquals("love - 30", "love - 30", score);
}

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

game.player2Scored();

String score = game.getScore();

assertEquals("15 - love", "15 - love", score);
}
@Test
public void testTennisGame_Player2Have2Point() throws TennisGameException {
TennisGame game = new TennisGame();

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

String score = game.getScore();

assertEquals("30 - love", "30 - love", score);
}

@Test
public void testTennisGame_Player1Have1Point_Player2Have1Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("15 - 15", "15 - 15", score) ;
}

@Test
public void testTennisGame_Player1Have2Point_Player2Have1Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player1Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("15 - 30", "15 - 30", score) ;
}

@Test
public void testTennisGame_Player1Have1Point_Player2Have2Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("30 - 15", "30 - 15", score) ;
}

@Test
public void testTennisGame_Player1Have2Point_Player2Have2Point() throws TennisGameException{
TennisGame game = new TennisGame();
game.player1Scored();
game.player1Scored();
game.player2Scored();
game.player2Scored();

String score = game.getScore();

assertEquals("30 - 30", "30 - 30", score) ;
}
}