Added unit tests and refactored code for testability
This commit is contained in:
parent
82006e9e09
commit
834fb70a40
8 changed files with 149 additions and 27 deletions
|
@ -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 {
|
||||
|
|
|
@ -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<String, String> 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()) {
|
||||
|
|
|
@ -5,7 +5,16 @@ import java.util.ArrayList;
|
|||
|
||||
public class Player {
|
||||
|
||||
protected List<Integer> guesses = new ArrayList<Integer>();
|
||||
public Player() {}
|
||||
|
||||
|
||||
public Player(int[] guesses) {
|
||||
for (int guess : guesses) {
|
||||
this.guesses.add(guess);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> guesses = new ArrayList<Integer>();
|
||||
|
||||
public void clearGuesses() {
|
||||
this.guesses = new ArrayList<Integer>();
|
||||
|
@ -15,6 +24,10 @@ public class Player {
|
|||
this.guesses.add(guess);
|
||||
}
|
||||
|
||||
public int getGuessCount() {
|
||||
return guesses.size();
|
||||
}
|
||||
|
||||
public int getLastGuess() {
|
||||
return this.guesses.get(this.guesses.size() - 1);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<string name="guess_button">Adivina</string>
|
||||
<string name="too_small">¡Ese número es demasiado pequeño!</string>
|
||||
<string name="too_big">¡Ese número es demasiado grande!</string>
|
||||
<string name="correct_answer">¡Correcto! Lo adivinaste en %1$s intentos.</string>
|
||||
<string name="correct_answer">¡Correcto! Lo adivinaste en %1$d intentos.</string>
|
||||
<string name="no">No</string>
|
||||
<string name="yes">Sí</string>
|
||||
<string name="ok">OK</string>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<string name="guess_button">Guess</string>
|
||||
<string name="too_small">That number is too small!</string>
|
||||
<string name="too_big">That number is too big!</string>
|
||||
<string name="correct_answer">Correct! You took %1$s tries to guess the right answer.</string>
|
||||
<string name="correct_answer">Correct! You took %1$d tries to guess the right answer.</string>
|
||||
<string name="game_over">You win!</string>
|
||||
<string name="yes">Yes</string>
|
||||
<string name="no">No</string>
|
||||
|
|
|
@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
64
app/src/test/java/com/wbrawner/numberguess/PlayerTest.java
Normal file
64
app/src/test/java/com/wbrawner/numberguess/PlayerTest.java
Normal file
|
@ -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<Integer> startingGuesses =
|
||||
Arrays.asList(25, 50, 75);
|
||||
assertEquals("getGuesses", startingGuesses, player.getGuesses());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue