aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/test/CompoundTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/common/test/CompoundTest.java')
-rw-r--r--src/cz/crcs/ectester/common/test/CompoundTest.java102
1 files changed, 82 insertions, 20 deletions
diff --git a/src/cz/crcs/ectester/common/test/CompoundTest.java b/src/cz/crcs/ectester/common/test/CompoundTest.java
index 69122b0..4b1df16 100644
--- a/src/cz/crcs/ectester/common/test/CompoundTest.java
+++ b/src/cz/crcs/ectester/common/test/CompoundTest.java
@@ -2,6 +2,7 @@ package cz.crcs.ectester.common.test;
import java.util.Arrays;
import java.util.Objects;
+import java.util.function.Consumer;
import java.util.function.Function;
/**
@@ -10,26 +11,34 @@ import java.util.function.Function;
* @author Jan Jancar johny@neuromancer.sk
*/
public class CompoundTest extends Test {
- private Function<Test[], Result> callback;
+ private Function<Test[], Result> resultCallback;
+ private Consumer<Test[]> runCallback;
private Test[] tests;
private String description = "";
- private CompoundTest(Function<Test[], Result> callback, Test... tests) {
- this.callback = callback;
+ private final static Consumer<Test[]> RUN_ALL = tests -> {
+ for (Test t : tests) {
+ t.run();
+ }
+ };
+
+ private CompoundTest(Function<Test[], Result> resultCallback, Consumer<Test[]> runCallback, Test... tests) {
+ this.resultCallback = resultCallback;
+ this.runCallback = runCallback;
this.tests = Arrays.stream(tests).filter(Objects::nonNull).toArray(Test[]::new);
}
- private CompoundTest(Function<Test[], Result> callback, String descripiton, Test... tests) {
- this(callback, tests);
+ private CompoundTest(Function<Test[], Result> callback, Consumer<Test[]> runCallback, String descripiton, Test... tests) {
+ this(callback, runCallback, tests);
this.description = descripiton;
}
public static CompoundTest function(Function<Test[], Result> callback, Test... tests) {
- return new CompoundTest(callback, tests);
+ return new CompoundTest(callback, RUN_ALL, tests);
}
public static CompoundTest function(Function<Test[], Result> callback, String description, Test... tests) {
- return new CompoundTest(callback, description, tests);
+ return new CompoundTest(callback, RUN_ALL, description, tests);
}
public static CompoundTest all(Result.ExpectedValue what, Test... all) {
@@ -40,7 +49,7 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
- }, all);
+ }, RUN_ALL, all);
}
public static CompoundTest all(Result.ExpectedValue what, String description, Test... all) {
@@ -49,7 +58,31 @@ public class CompoundTest extends Test {
return result;
}
- public static CompoundTest any(Result.ExpectedValue what, Test... any) {
+ public static CompoundTest greedyAll(Result.ExpectedValue what, Test... all) {
+ return new CompoundTest((tests) -> {
+ for (Test test : tests) {
+ if (!Result.Value.fromExpected(what, test.ok()).ok()) {
+ return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
+ }
+ }
+ return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
+ }, (tests) -> {
+ for (Test t : tests) {
+ t.run();
+ if (!t.ok()) {
+ break;
+ }
+ }
+ }, all);
+ }
+
+ public static CompoundTest greedyAll(Result.ExpectedValue what, String description, Test... all) {
+ CompoundTest result = CompoundTest.greedyAll(what, all);
+ result.setDescription(description);
+ return result;
+ }
+
+ public static CompoundTest greedyAny(Result.ExpectedValue what, Test... any) {
return new CompoundTest((tests) -> {
for (Test test : tests) {
if (Result.Value.fromExpected(what, test.ok()).ok()) {
@@ -57,9 +90,33 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
+ }, (tests) -> {
+ for (Test t : tests) {
+ t.run();
+ if (t.ok()) {
+ break;
+ }
+ }
}, any);
}
+ public static CompoundTest greedyAny(Result.ExpectedValue what, String description, Test... any) {
+ CompoundTest result = CompoundTest.greedyAny(what, any);
+ result.setDescription(description);
+ return result;
+ }
+
+ public static CompoundTest any(Result.ExpectedValue what, Test... any) {
+ return new CompoundTest((tests) -> {
+ for (Test test : tests) {
+ if (Result.Value.fromExpected(what, test.ok()).ok()) {
+ return new Result(Result.Value.SUCCESS, "Some sub-tests did have the expected result.");
+ }
+ }
+ return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
+ }, RUN_ALL, any);
+ }
+
public static CompoundTest any(Result.ExpectedValue what, String description, Test... any) {
CompoundTest result = CompoundTest.any(what, any);
result.setDescription(description);
@@ -74,7 +131,7 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests matched the expected mask.");
- }, masked);
+ }, RUN_ALL, masked);
}
public static CompoundTest mask(Result.ExpectedValue[] results, String description, Test... masked) {
@@ -84,20 +141,25 @@ public class CompoundTest extends Test {
}
public Test[] getTests() {
- return tests;
+ return tests.clone();
}
- @Override
- public void run() throws TestException {
- if (hasRun)
- return;
+ public Test[] getRunTests() {
+ return Arrays.stream(tests).filter(Test::hasRun).toArray(Test[]::new);
+ }
- for (Test test : tests) {
- test.run();
- }
+ public Test[] getStartedTests() {
+ return Arrays.stream(tests).filter(Test::hasStarted).toArray(Test[]::new);
+ }
- result = callback.apply(tests);
- this.hasRun = true;
+ public Test[] getSkippedTests() {
+ return Arrays.stream(tests).filter((test) -> !test.hasRun()).toArray(Test[]::new);
+ }
+
+ @Override
+ protected void runSelf() {
+ runCallback.accept(tests);
+ result = resultCallback.apply(tests);
}
public void setDescription(String description) {