-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.java
More file actions
93 lines (77 loc) · 2.5 KB
/
Copy pathTests.java
File metadata and controls
93 lines (77 loc) · 2.5 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.function.Consumer;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import game.model.RandomManager;
import game.model.Reflections;
import game.view.OutputManager;
public class Tests {
Consumer<String> printer;
public Tests(PrintStream out) {
this.printer = out::println;
}
public void println(String fmt, Object... args) {
printer.accept(fmt.formatted(args));
}
public void checkTests(List<Class<?>> testClasses) {
Class<?>[] classesArray = testClasses.toArray(new Class<?>[0]);
int passed = 0;
int failed = 0;
for(int i = 0; i < classesArray.length; ++i) {
RandomManager.setSeed(0);
println(
"%s[INFO] attempting to run tests for class %s%s",
OutputManager.Color.GREEN,
classesArray[i].getCanonicalName(),
OutputManager.Color.RESET
);
Result result = JUnitCore.runClasses(classesArray[i]);
if (result.wasSuccessful()) {
println(
"%s[SUCCESS] %s%s",
OutputManager.Color.GREEN,
classesArray[i].getCanonicalName(),
OutputManager.Color.RESET
);
passed += 1;
} else {
for(Failure failure : result.getFailures()) {
String methodName = failure.getTestHeader().substring(0, failure.getTestHeader().indexOf('('));
println(
"%s[FAILURE] %s.%s()%s%s",
OutputManager.Color.RED,
classesArray[i].getCanonicalName(),
methodName,
failure.getMessage() != null ? (": " + failure.getMessage()) : "",
OutputManager.Color.RESET
);
failed += 1;
}
}
RandomManager.setSeed(0);
}
println(
"Passed: %s%% (%s), Failed: %s%% (%s)",
100*passed/(float)(passed + failed),
passed,
100*failed/(float)(passed + failed),
failed
);
}
public static void main(String[] args) throws FileNotFoundException {
PrintStream out = System.out;
System.setOut(new PrintStream(new File("tests_output.log")));
System.setErr(new PrintStream(new File("tests_output.log")));
Tests tests = new Tests(out);
List<Class<?>> testClasses = Reflections
.queryAll("tests")
.filter(c -> Modifier.isPublic(c.getModifiers()))
.toList();
tests.checkTests(testClasses);
}
}