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
55
56
package edu.caltech.cs2.lab02;
import edu.caltech.cs2.helpers.CaptureSystemOutput;
import edu.caltech.cs2.helpers.FileSource;
import edu.caltech.cs2.helpers.Reflection;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Tag("edu.caltech.cs2.lab02.Tests")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@CaptureSystemOutput
public class GameTraceTests {
@Order(1)
@ParameterizedTest
@Tag("A")
@DisplayName("Test full Adventure Game")
@FileSource(
inputs = {
"{stdin = correctInput.txt}",
"{stdin = incorrectInput.txt}",
},
outputFiles = {
"correctAnswers.txt",
"incorrectAnswers.txt"
}
)
public void testAdventureGame(Map<String, String> arguments, String expectedOutput, CaptureSystemOutput.OutputCapture capture)
throws FileNotFoundException {
try {
runTestGame(arguments.get("stdin"));
} catch (NoSuchElementException e) {
}
assertEquals(expectedOutput.replace("\r\n", "\n").strip(), capture.toString().replace("\r\n", "\n").strip());
}
public void runTestGame(String filePath) throws FileNotFoundException {
Scanner scan = new Scanner(new File("tests/data/" + filePath));
Constructor<AdventureGame> constructor = Reflection.getConstructor(AdventureGame.class, Scanner.class);
Field rand = Reflection.getFieldByType(AdventureGame.class, Random.class);
Reflection.<Random>getFieldValue(AdventureGame.class, rand.getName(), null).setSeed(1337);
AdventureGame game = Reflection.newInstance(constructor, scan);
game.play();
}
}