forked from MetroCS/ConsoleGameHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJottoGameTest.java
More file actions
54 lines (49 loc) · 1.93 KB
/
JottoGameTest.java
File metadata and controls
54 lines (49 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
/**
* Tests for JottoGame.
* @version 1
*/
public class JottoGameTest {
@Test
public void testCountMatchingLetters_withNoMatches() {
JottoGame game = new JottoGame();
int matches = gameTestHelper_countMatchingLetters(game, "BRICK", "ZOOEY");
assertEquals(0, matches);
}
@Test
public void testCountMatchingLetters_withSomeMatches() {
JottoGame game = new JottoGame();
int matches = gameTestHelper_countMatchingLetters(game, "BRICK", "CRANE");
assertEquals(2, matches); // C and R
}
@Test
public void testCountMatchingLetters_withAllMatches() {
JottoGame game = new JottoGame();
int matches = gameTestHelper_countMatchingLetters(game, "BRICK", "BRICK");
assertEquals(5, matches);
}
/**
* Reflection-based access since countMatchingLetters is private.
* @param game the Jotto game under test
* @param secret the secret word
* @param guess the player's guess word
* @return the value returned by the game given the secret and guess
* or -1 if the reflection failed
*/
private int gameTestHelper_countMatchingLetters(final JottoGame game,
final String secret,
final String guess) {
try {
var method = JottoGame.class.getDeclaredMethod("countMatchingLetters",
String.class,
String.class);
method.setAccessible(true);
return (int) method.invoke(game, secret, guess);
} catch (Exception e) {
fail("Reflection failed: " + e.getMessage());
return -1;
}
}
}