Commit 7b30bbd9 authored by Ethan Ordentlich's avatar Ethan Ordentlich
Browse files

Merge branch 'tests' into 'master'

Review tests for Project 2

Closes #2 and #5

See merge request !1
1 merge request!1Review tests for Project 2
Pipeline #36441 canceled with stage
Showing with 91 additions and 10 deletions
+91 -10
......@@ -10,7 +10,7 @@ import java.util.Scanner;
import java.util.Set;
public class HangmanGame {
public static void main(String[] args) {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to the cs2 console hangman game.");
System.out.println();
......@@ -30,7 +30,7 @@ public class HangmanGame {
}
// Plays one game with the user
public static void playGame(IHangmanChooser chooser, IHangmanGuesser guesser) {
public static void playGame(IHangmanChooser chooser, IHangmanGuesser guesser) throws FileNotFoundException {
while (!chooser.isGameOver()) {
String pattern = chooser.getPattern();
Set<Character> guesses = chooser.getGuesses();
......
......@@ -7,6 +7,10 @@ import java.io.FileNotFoundException;
import java.util.*;
public class EvilHangmanChooser implements IHangmanChooser {
public EvilHangmanChooser(int wordLength, int maxGuesses) throws FileNotFoundException {
}
@Override
public int makeGuess(char letter) {
......
......@@ -5,6 +5,10 @@ import edu.caltech.cs2.project02.interfaces.IHangmanChooser;
import java.util.*;
public class RandomHangmanChooser implements IHangmanChooser {
public RandomHangmanChooser(int wordLength, int maxGuesses) throws FileNotFoundException {
}
@Override
public int makeGuess(char letter) {
......
......@@ -7,7 +7,7 @@ import java.util.*;
public class AIHangmanGuesser implements IHangmanGuesser {
@Override
public char getGuess(String pattern, Set<Character> guesses) {
public char getGuess(String pattern, Set<Character> guesses) throws FileNotFoundException {
return 0;
}
}
......@@ -3,5 +3,5 @@ package edu.caltech.cs2.project02.interfaces;
import java.util.Set;
public interface IHangmanGuesser {
public char getGuess(String pattern, Set<Character> guesses);
public char getGuess(String pattern, Set<Character> guesses) throws FileNotFoundException;
}
......@@ -232,6 +232,21 @@ public class Reflection {
assertFieldsEqualTo(clazz, getFields(clazz).filter(doesNotHaveModifier("static")),
"public", null, 0);
}
public static void assertNoProtectedFields(Class clazz) {
assertFieldsEqualTo(clazz, getFields(clazz).filter(doesNotHaveModifier("static")),
"protected", null, 0);
}
public static void assertFieldModifiers(Class clazz) {
List<Field> fields = getFields(clazz).collect(Collectors.toList());
for (Field f : fields) {
int m = f.getModifiers();
assertTrue(Modifier.isPrivate(m) || Modifier.isProtected(m) || Modifier.isPublic(m),
"Field \"" + f.getName() + "\" does not have one of {public, private, protected}");
}
}
public static Field getFieldByName(Class clazz, String name) {
try {
......
......@@ -11,6 +11,7 @@ import edu.caltech.cs2.project02.interfaces.IHangmanChooser;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
......@@ -52,6 +53,22 @@ public class ChooserTests {
Reflection.assertNoPublicFields(RandomHangmanChooser.class);
}
@Order(3)
@Tag("C")
@DisplayName("There are no protected fields in RandomHangmanChooser")
@Test
public void testNoProtectedFieldsRHC() {
Reflection.assertNoProtectedFields(RandomHangmanChooser.class);
}
@Order(3)
@Tag("C")
@DisplayName("All fields in RandomHangmanChooser have modifiers")
@Test
public void testFieldModifiersRHC() {
Reflection.assertFieldModifiers(RandomHangmanChooser.class);
}
@Order(3)
@Tag("C")
@DisplayName("Chosen word and random are the only final fields")
......@@ -95,7 +112,7 @@ public class ChooserTests {
IntStream.range(0, 20).forEach(i -> assertThrows(IllegalArgumentException.class, () -> Reflection.invoke(m, chooser, (char) ('z' + (i + 1)))));
}
public void runTestGame(Class<? extends IHangmanChooser> clazz, int wordLength, int wrongAnswersAllowed, String guesses) {
public void runTestGame(Class<? extends IHangmanChooser> clazz, int wordLength, int wrongAnswersAllowed, String guesses) throws FileNotFoundException {
Constructor<? extends IHangmanChooser> constructor = Reflection.getConstructor(clazz, int.class, int.class);
HangmanGame.playGame(
Reflection.newInstance(constructor, wordLength, wrongAnswersAllowed),
......@@ -107,7 +124,7 @@ public class ChooserTests {
@Tag("C")
@DisplayName("Expected game end after revealing word for RandomHangmanChooser")
@Test
public void testGameEndOnRevealWordRHC() {
public void testGameEndOnRevealWordRHC() throws FileNotFoundException {
RandomHangmanChooser chooser = new RandomHangmanChooser(8, 1);
chooser.getWord();
assertThrows(IllegalStateException.class, () -> chooser.makeGuess('a'));
......@@ -154,7 +171,7 @@ public class ChooserTests {
"trace14.txt",
}
)
public void testPlayGameWithRandomChooser(Map<String, String> arguments, String expectedOutput, CaptureSystemOutput.OutputCapture capture) {
public void testPlayGameWithRandomChooser(Map<String, String> arguments, String expectedOutput, CaptureSystemOutput.OutputCapture capture) throws FileNotFoundException {
// Parse arguments
int seed = Integer.parseInt(arguments.get("seed"));
int length = Integer.parseInt(arguments.get("word length"));
......@@ -188,6 +205,22 @@ public class ChooserTests {
Reflection.assertNoPublicFields(EvilHangmanChooser.class);
}
@Order(2)
@Tag("B")
@DisplayName("There are no protected fields in EvilHangmanChooser")
@Test
public void testNoProtectedFieldsEHC() {
Reflection.assertNoProtectedFields(EvilHangmanChooser.class);
}
@Order(2)
@Tag("B")
@DisplayName("All fields in EvilHangmanChooser have modifiers")
@Test
public void testFieldModifiersEHC() {
Reflection.assertFieldModifiers(EvilHangmanChooser.class);
}
@Order(2)
@Tag("B")
@DisplayName("There is no map field in EvilHangmanChooser")
......@@ -225,7 +258,7 @@ public class ChooserTests {
@Tag("B")
@DisplayName("Expected game end after revealing word for EvilHangmanChooser")
@Test
public void testGameEndOnRevealWordEHC() {
public void testGameEndOnRevealWordEHC() throws FileNotFoundException {
EvilHangmanChooser chooser = new EvilHangmanChooser(8, 1);
chooser.getWord();
assertThrows(IllegalStateException.class, () -> chooser.makeGuess('a'));
......@@ -271,7 +304,7 @@ public class ChooserTests {
"trace14-evil.txt",
}
)
public void testPlayGameWithEvilChooser(Map<String, String> arguments, String expectedOutput, CaptureSystemOutput.OutputCapture capture) {
public void testPlayGameWithEvilChooser(Map<String, String> arguments, String expectedOutput, CaptureSystemOutput.OutputCapture capture) throws FileNotFoundException {
// Parse arguments
int length = Integer.parseInt(arguments.get("word length"));
int wrongAllowed = Integer.parseInt(arguments.get("max wrong guesses"));
......
......@@ -5,6 +5,7 @@ import edu.caltech.cs2.helpers.Reflection;
import edu.caltech.cs2.project02.guessers.AIHangmanGuesser;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
......@@ -32,8 +33,32 @@ public class GuesserTests {
public void testNoFields() {
Reflection.assertFieldsLessThan(AIHangmanGuesser.class, "private", 1);
}
@Order(1)
@Tag("A")
@DisplayName("There are no public fields in AIHangmanGuesser")
@Test
public void testNoPublicFieldsAIHG() {
Reflection.assertNoPublicFields(AIHangmanGuesser.class);
}
@Order(1)
@Tag("A")
@DisplayName("There are no protected fields in AIHangmanGuesser")
@Test
public void testNoProtectedFieldsAIHG() {
Reflection.assertNoProtectedFields(AIHangmanGuesser.class);
}
@Order(1)
@Tag("A")
@DisplayName("All fields in AIHangmanGuesser have modifiers")
@Test
public void testFieldModifiersAIHG() {
Reflection.assertFieldModifiers(AIHangmanGuesser.class);
}
@Order(1)
@Tag("A")
@DisplayName("Test that the dictionary is static")
@Test
......@@ -46,7 +71,7 @@ public class GuesserTests {
@Tag("A")
@DisplayName("Test getGuess Method in AIHangmanGuesser")
@Test
public void testGetGuess() {
public void testGetGuess() throws FileNotFoundException {
AIHangmanGuesser guesser = new AIHangmanGuesser();
// test character with most occurrences is chosen
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment