From 058109f13f86429b7650e3a5de507e832b6b7267 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Thu, 20 Sep 2018 15:06:24 +0300 Subject: [PATCH 1/4] testing if commit works --- bin/TennisGame.class | Bin 1823 -> 1823 bytes src/TennisGame.java | 52 +++++++++++++++++++++-------------- src/TennisGameException.java | 3 +- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/bin/TennisGame.class b/bin/TennisGame.class index 2da4e7a578a8ea5a5a076c2766b0eebecc4efc75..5836d0cb67bdb12eb8786a7c409368eeb9c34cde 100644 GIT binary patch delta 175 zcmWN~%L)N;6vy#zjUkH}#$7S4xh0}B3zuwc?8vo7M3youkKhIV58xF%hMm1^you9q zb2^`X=RC}Z`Fv}JK@d|CB1%|&wW}2)F3FOhOA?2aa(!MrLmrzFIVv*MDbS>-e!iPk zBS1s?Ez-25*HJ&+R(E}B^+X(zzQ{miB=YLacF1QTYF=o$_0~?N9 LIN@QBBXMg*X8&3cL diff --git a/src/TennisGame.java b/src/TennisGame.java index 327f284..566ce38 100644 --- a/src/TennisGame.java +++ b/src/TennisGame.java @@ -2,56 +2,67 @@ // NOTE THAT it may contain bugs // Write unit tests in TennisGameTest.java and try to find the errors in the code -public class TennisGame { +public class TennisGame +{ private int player1Points; private int player2Points; - private boolean gameEnded; - public TennisGame() { + public TennisGame() + { player1Points = 0; player2Points = 0; gameEnded = false ; } - private void checkGameEnded() { + 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" ; + + 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) { + public void player1Scored() throws TennisGameException + { + if (gameEnded) + { throw new TennisGameException(); } - else { + else + { player1Points++; checkGameEnded(); } } - public void player2Scored() throws TennisGameException { - if (gameEnded) { + public void player2Scored() throws TennisGameException + { + if (gameEnded) + { throw new TennisGameException(); } - else { + else + { player2Points++; checkGameEnded(); } } - public String getScore() { + public String getScore() + { // Here is the format of the scores: // "love - love" // "15 - 15" @@ -70,7 +81,8 @@ public String getScore() { String player1Score = getScore(player1Points); String player2Score = getScore(player2Points); - if (gameEnded) { + if (gameEnded) + { if (player1Points > player2Points) return "player1 wins"; else diff --git a/src/TennisGameException.java b/src/TennisGameException.java index 23fa24f..ac586b7 100644 --- a/src/TennisGameException.java +++ b/src/TennisGameException.java @@ -1,4 +1,5 @@ -public class TennisGameException extends Exception { +public class TennisGameException extends Exception +{ } From 626df4cbec1413602cf38a18b13ca901271f1fb6 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Thu, 20 Sep 2018 15:41:55 +0300 Subject: [PATCH 2/4] test done and code fidxed v1 --- bin/TennisGame.class | Bin 1823 -> 1835 bytes src/TennisGame.java | 41 ++++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/bin/TennisGame.class b/bin/TennisGame.class index 5836d0cb67bdb12eb8786a7c409368eeb9c34cde..1a97a7c0ead45d31089d5a42ad132161bbd2bbdc 100644 GIT binary patch delta 543 zcmZXP&rcIk5XV1nOWWeZ{+ z6DK6`=B0@Qj3)j89{f{0dx1C`tOw0|Z)V=-J2UUiZsW&BWa?~Gr;NWm?S zdm*)3)g{WUy)3zkmd>usbEo1YH=SZ3nJbqziiMPcqSvI2cAb8OxwYxMaZA1S>y6^J z&fs6t_lI0n1oQ5zHCN}FB34gdceZUOUvY|Mryx?QE_tn*rAEANt^L9%<6*`WXa4>k@7&6LSB8SjUd($m~Gc|h`0H1JcKy-ZSu zGC`dpAPe3m68khAawX#jQ3+VfEE>Ll||#c@Fs#sbc7j|t;OPT KNyrJ-nEMT$5OVkc delta 463 zcmX|-OHUI~7>1v73Z0=D3N6%;0%jH_Bvq*(V71&7?_8yLA(7xnCk28HDYeEGTejw` zT%id&7uHC?XyOm>C-@^=yOHos0wm{r@5%eT-@7=U1)kPP%OR70%{{|1PNXd2iRsTQ&n{PXCEBq-ZP#Z&m`EoFa7^U-h>vcHEeRa>%u7G7GsLmb-(6kH~exbTc}pn$|Z+e+%^?GXy>`(5aX`-qSJ}{ ziimf`i;?G{!y_imADx%Tw8IR&-?}4zb)=JC6OX#N>{KSRPvVg5v}oH0+C^GIYmaEM zub#YC4-M-RiBs@Vz@S-*y4%-7maz6{3pM1$hLGN<|0KV7`8y+rOeF6e@Zgf%1E%Wo ze-TnLhiI1hm?q{)vOotMp$B;WuEYo=e*)ID{K;QPPRPhfE{u!=jGAF hHnYs3q^Ks9b7<1yx6t^X9*a>qI_0_&m={}9I0gw~U!(v4 diff --git a/src/TennisGame.java b/src/TennisGame.java index 566ce38..f20ba7d 100644 --- a/src/TennisGame.java +++ b/src/TennisGame.java @@ -23,7 +23,7 @@ else if (player2Points>=4 && player2Points-player1Points>=2) gameEnded = true; } - private String getScore(int points) + private String getAScore(int points) { switch (points) { @@ -77,27 +77,22 @@ public String getScore() // "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 ; + String player1Score = getAScore(player1Points); + String player2Score = getAScore(player2Points); + if (gameEnded) + { + if (player1Points > player2Points) + return "player1 wins"; + else + return "player2 wins"; + } + if (player1Points >= 3 && player1Points == player2Points) + return "deuce"; + else if (player1Points >= 4 && player1Points - player2Points == 1) + return "player1 has advantage"; + else if (player2Points >= 4 && player2Points - player1Points == 1) + return "player2 has advantage"; + return player1Score + " - " + player2Score ; + //return player2Score + " - " + player1Score ; } } \ No newline at end of file From 948ff504e60d87ab642e27c3fdb07f8cf4ddab88 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Thu, 20 Sep 2018 15:43:11 +0300 Subject: [PATCH 3/4] commit the tests --- bin/.gitignore | 5 + bin/TennisGameTest.class | Bin 1393 -> 0 bytes tests/TennisGameTest.java | 67 -------- tests/TennisGameTest2.java | 302 +++++++++++++++++++++++++++++++++++++ 4 files changed, 307 insertions(+), 67 deletions(-) create mode 100644 bin/.gitignore delete mode 100644 bin/TennisGameTest.class delete mode 100644 tests/TennisGameTest.java create mode 100644 tests/TennisGameTest2.java diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..87dbc07 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,5 @@ +/TennisGame.class +/TennisGameException.class +/TennisGameTest.class +/TennisGameTest2.class +/AllTests.class diff --git a/bin/TennisGameTest.class b/bin/TennisGameTest.class deleted file mode 100644 index b202db2907805d8b5bb4b6f773ec5588f796fb7a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1393 zcmZ`(*-{fh6g?f53?V3l9R-6c`^c&oqzV-+s-jXrVflnj(uT&N2WKV}{*Axji&$Fa z2l!EzrzatapbyjQ?R%EnJ->c`{|R6LkCI3TT-i~^=IWtFu*`GrS{SMz zN+XL=fwYWg>mSR~qnPEf%a9E#l{(}uuf)|Tv3ktd;3^$$i81MbW+p+gdl?%+CT0bqQPGa?70t1*N>nT2r8Sc7V zo#l<0$0C1I)~SkyrJ9DBs)p$X4U7NK&`vcI{FVxM5LZQ0Zh{Gi|05(8RQGfwKD11} iIbyGn?_=^^CEqpjeG+r)t0b&(>#cGVcu1fGmj3{BjV}}c diff --git a/tests/TennisGameTest.java b/tests/TennisGameTest.java deleted file mode 100644 index 8674eba..0000000 --- a/tests/TennisGameTest.java +++ /dev/null @@ -1,67 +0,0 @@ -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(); - } -} diff --git a/tests/TennisGameTest2.java b/tests/TennisGameTest2.java new file mode 100644 index 0000000..1336436 --- /dev/null +++ b/tests/TennisGameTest2.java @@ -0,0 +1,302 @@ +import static org.junit.Assert.*; +import org.junit.Test; +public class TennisGameTest2 +{ + @Test + public void before_game_start_score_is_love_love() + { + TennisGame aGame = new TennisGame(); + String test = aGame.getScore(); + assertEquals("love - love",test); + } + + @Test + public void test_player1_scores_score_15_0() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("15 - love",test); + } + + @Test + public void test_player1_scores_score_30_0() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("30 - love",test); + } + + @Test + public void test_player1_scores_score_40_0() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("40 - love",test); + } + + @Test + public void test_player1_wins() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); + //System.out.println(test); + assertEquals("player1 wins",test); + } + + //Player1 won, but someone is still trying to score + @Test (expected = TennisGameException.class ) + public void test_player1_won_already_throw_exception() throws TennisGameException + { + TennisGame aGame = new TennisGame(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + } + + @Test + public void test_player2_scores_score_0_15() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("love - 15",test); + } + @Test + public void test_player2_scores_score_0_30() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player2Scored(); + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("love - 30",test); + } + @Test + public void test_player2_scores_score_0_40() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("love - 40",test); + } + + @Test + public void test_player2_wins() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("player2 wins",test); + } + + //Player2 won, but someone is still trying to score + @Test (expected = TennisGameException.class ) + public void test_player2_won_already_throw_exception() throws TennisGameException + { + TennisGame aGame = new TennisGame(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + } + + @Test + public void player1_advantage() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("player1 has advantage",test); + } + @Test + public void player2_advantage() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("player2 has advantage",test); + } + + @Test + public void player2_wins_after_advantage() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("player2 wins",test); + } + + @Test + public void player1_wins_after_advantage() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("player1 wins",test); + } + + + @Test + public void deuce() + { + TennisGame aGame = new TennisGame(); + try + { + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player1Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + aGame.player2Scored(); + + } + catch(TennisGameException e) + { + + } + String test = aGame.getScore(); +// System.out.println(test); + assertEquals("deuce",test); + } +} From fbd9c8467792fa5a3fbcc77fef51dc4b32a57345 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Thu, 20 Sep 2018 15:47:08 +0300 Subject: [PATCH 4/4] commit tests --- tests/TennisGameTest2.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/TennisGameTest2.java b/tests/TennisGameTest2.java index 1336436..ab2f745 100644 --- a/tests/TennisGameTest2.java +++ b/tests/TennisGameTest2.java @@ -205,6 +205,7 @@ public void player1_advantage() // System.out.println(test); assertEquals("player1 has advantage",test); } + @Test public void player2_advantage() {