From 834fb70a40a9e2501f0a7fe10c9070f0de92c4c8 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Fri, 28 Jul 2017 18:37:10 -0500 Subject: [PATCH] Added unit tests and refactored code for testability --- .../wbrawner/numberguess/GameFragment.java | 4 +- .../com/wbrawner/numberguess/NumberGuess.java | 11 ++-- .../java/com/wbrawner/numberguess/Player.java | 15 ++++- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- .../wbrawner/numberguess/ExampleUnitTest.java | 17 ----- .../numberguess/NumberGuessUnitTest.java | 61 ++++++++++++++++++ .../com/wbrawner/numberguess/PlayerTest.java | 64 +++++++++++++++++++ 8 files changed, 149 insertions(+), 27 deletions(-) delete mode 100644 app/src/test/java/com/wbrawner/numberguess/ExampleUnitTest.java create mode 100644 app/src/test/java/com/wbrawner/numberguess/NumberGuessUnitTest.java create mode 100644 app/src/test/java/com/wbrawner/numberguess/PlayerTest.java diff --git a/app/src/main/java/com/wbrawner/numberguess/GameFragment.java b/app/src/main/java/com/wbrawner/numberguess/GameFragment.java index f3ef24f..fa37f66 100644 --- a/app/src/main/java/com/wbrawner/numberguess/GameFragment.java +++ b/app/src/main/java/com/wbrawner/numberguess/GameFragment.java @@ -97,7 +97,7 @@ public class GameFragment extends Fragment { ); } setResponse( - ng.checkAnswer(user) + ng.checkAnswer(user.getLastGuess()) ); guessInput.setText(null); } @@ -137,7 +137,7 @@ public class GameFragment extends Fragment { int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, null, duration); if (response.get("code").equals("correct")) { - String popup = getActivity().getString(R.string.correct_answer, response.get("count")); + String popup = getActivity().getString(R.string.correct_answer, user.getGuessCount()); text = getString(R.string.game_over); gameOver(popup); } else { diff --git a/app/src/main/java/com/wbrawner/numberguess/NumberGuess.java b/app/src/main/java/com/wbrawner/numberguess/NumberGuess.java index 7801b27..e961a67 100644 --- a/app/src/main/java/com/wbrawner/numberguess/NumberGuess.java +++ b/app/src/main/java/com/wbrawner/numberguess/NumberGuess.java @@ -5,22 +5,23 @@ import java.util.Map; public class NumberGuess { - protected int answer = 0; + private int answer = 0; public void setAnswer() { this.answer = (int) (Math.random() * 100); } + public void setAnswer(int answer) { + this.answer = answer; + } + public int getAnswer() { return this.answer; } - public Map checkAnswer(Player user) { + public Map checkAnswer(int guess) { Map response = new HashMap<>(); - int guess = user.getLastGuess(); - String guessCount = String.valueOf(user.getGuesses().size()); if (guess == this.getAnswer()) { - response.put("count", guessCount); response.put("code", "correct"); } else { if (guess > this.getAnswer()) { diff --git a/app/src/main/java/com/wbrawner/numberguess/Player.java b/app/src/main/java/com/wbrawner/numberguess/Player.java index 5724ad5..b9c16fb 100644 --- a/app/src/main/java/com/wbrawner/numberguess/Player.java +++ b/app/src/main/java/com/wbrawner/numberguess/Player.java @@ -5,7 +5,16 @@ import java.util.ArrayList; public class Player { - protected List guesses = new ArrayList(); + public Player() {} + + + public Player(int[] guesses) { + for (int guess : guesses) { + this.guesses.add(guess); + } + } + + private List guesses = new ArrayList(); public void clearGuesses() { this.guesses = new ArrayList(); @@ -14,6 +23,10 @@ public class Player { public void addGuess(int guess) { this.guesses.add(guess); } + + public int getGuessCount() { + return guesses.size(); + } public int getLastGuess() { return this.guesses.get(this.guesses.size() - 1); diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index ba3bd64..07e42b1 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -5,7 +5,7 @@ Adivina ¡Ese número es demasiado pequeño! ¡Ese número es demasiado grande! - ¡Correcto! Lo adivinaste en %1$s intentos. + ¡Correcto! Lo adivinaste en %1$d intentos. No OK diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52126af..5b28e3f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ Guess That number is too small! That number is too big! - Correct! You took %1$s tries to guess the right answer. + Correct! You took %1$d tries to guess the right answer. You win! Yes No diff --git a/app/src/test/java/com/wbrawner/numberguess/ExampleUnitTest.java b/app/src/test/java/com/wbrawner/numberguess/ExampleUnitTest.java deleted file mode 100644 index a77c101..0000000 --- a/app/src/test/java/com/wbrawner/numberguess/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.wbrawner.numberguess; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/app/src/test/java/com/wbrawner/numberguess/NumberGuessUnitTest.java b/app/src/test/java/com/wbrawner/numberguess/NumberGuessUnitTest.java new file mode 100644 index 0000000..f28ce10 --- /dev/null +++ b/app/src/test/java/com/wbrawner/numberguess/NumberGuessUnitTest.java @@ -0,0 +1,61 @@ +package com.wbrawner.numberguess; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by billy on 7/28/2017. + */ + +public class NumberGuessUnitTest { + private static NumberGuess ng; + + @BeforeClass + public static void setUp() throws Exception { + ng = new NumberGuess(); + ng.setAnswer(); + } + + @Test + public void setAnswer() throws Exception { + boolean result = 0 < ng.getAnswer() && ng.getAnswer() < 101; + assertTrue("Generated number is larger than 0 and less than 101", result); + } + + @Test + public void setAnswer1() throws Exception { + int previous = ng.getAnswer(); + ng.setAnswer(74); + assertNotEquals("setAnswer with parameter", previous, ng.getAnswer()); + ng.setAnswer(previous); + } + + @Test + public void getAnswer() throws Exception { + int previous = ng.getAnswer(); + ng.setAnswer(23); + assertEquals("getAnswer", 23, ng.getAnswer()); + ng.setAnswer(previous); + } + + @Test + public void checkAnswer() throws Exception { + Map tooBig = ng.checkAnswer(ng.getAnswer() + 1); + assertTrue("'Too big' response map contains code key", tooBig.containsKey("code")); + assertEquals("'Too big' response code equal 'too_big'", "too_big", tooBig.get("code")); + + Map tooSmall = ng.checkAnswer(ng.getAnswer() - 1); + assertTrue("'Too small' response map contains code key", tooSmall.containsKey("code")); + assertEquals("'Too small' response code equals 'too_small'", "too_small", tooSmall.get("code")); + + Map correct = ng.checkAnswer(ng.getAnswer()); + assertTrue("'Correct' response map contains code key", correct.containsKey("code")); + assertEquals("'Correct' response code equal 'correct'", "correct", correct.get("code")); + } +} diff --git a/app/src/test/java/com/wbrawner/numberguess/PlayerTest.java b/app/src/test/java/com/wbrawner/numberguess/PlayerTest.java new file mode 100644 index 0000000..0ecaf57 --- /dev/null +++ b/app/src/test/java/com/wbrawner/numberguess/PlayerTest.java @@ -0,0 +1,64 @@ +package com.wbrawner.numberguess; + +import android.support.annotation.NonNull; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import static org.junit.Assert.*; + +/** + * Created by billy on 7/28/2017. + */ +public class PlayerTest { + private Player player; + + @Before + public void setUp() { + player = new Player(new int[] {25, 50, 75}); + } + + @Test + public void player() { + Player newPlayer = new Player(); + assertEquals("New player has no guesses", 0, newPlayer.getGuessCount()); + } + + @Test + public void clearGuesses() throws Exception { + player.clearGuesses(); + assertEquals("clearGuesses", 0, player.getGuessCount()); + } + + @Test + public void addGuess() throws Exception { + player.addGuess(90); + assertEquals("Added guess 90 - should be last guess", 90, player.getLastGuess()); + assertEquals("Added guess 90 - guess count should be 4", 4, player.getGuessCount()); + } + + @Test + public void getGuessCount() throws Exception { + assertEquals("Guess count equals 3", 3, player.getGuessCount()); + } + + @Test + public void getLastGuess() throws Exception { + assertEquals("Last guess is 75", 75, player.getLastGuess()); + } + + @Test + public void getGuesses() throws Exception { + List startingGuesses = + Arrays.asList(25, 50, 75); + assertEquals("getGuesses", startingGuesses, player.getGuesses()); + } + +} \ No newline at end of file