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.java151
1 files changed, 127 insertions, 24 deletions
diff --git a/src/cz/crcs/ectester/common/test/CompoundTest.java b/src/cz/crcs/ectester/common/test/CompoundTest.java
index 10ecf9c..ba4ad4f 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;
/**
@@ -9,30 +10,64 @@ import java.util.function.Function;
*
* @author Jan Jancar johny@neuromancer.sk
*/
-public class CompoundTest extends Test {
- private Function<Test[], Result> callback;
+public class CompoundTest extends Test implements Cloneable {
+ private Function<Test[], Result> resultCallback;
+ private Consumer<Test[]> runCallback;
private Test[] tests;
- private String description;
+ 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 final static Consumer<Test[]> RUN_GREEDY_ALL = tests -> {
+ for (Test t : tests) {
+ t.run();
+ if (!t.ok()) {
+ break;
+ }
+ }
+ };
+
+ private final static Consumer<Test[]> RUN_GREEDY_ANY = tests -> {
+ for (Test t : tests) {
+ t.run();
+ if (t.ok()) {
+ break;
+ }
+ }
+ };
+
+ 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, Consumer<Test[]> runCallback, Test... tests) {
+ return new CompoundTest(callback, runCallback, 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) {
+ public static CompoundTest function(Function<Test[], Result> callback, Consumer<Test[]> runCallback, String description, Test... tests) {
+ return new CompoundTest(callback, runCallback, description, tests);
+ }
+
+ private static CompoundTest expectAll(Result.ExpectedValue what, Consumer<Test[]> runCallback, Test[] all) {
return new CompoundTest((tests) -> {
for (Test test : tests) {
if (!Result.Value.fromExpected(what, test.ok()).ok()) {
@@ -40,7 +75,11 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
- }, all);
+ }, runCallback, all);
+ }
+
+ public static CompoundTest all(Result.ExpectedValue what, Test... all) {
+ return expectAll(what, RUN_ALL, all);
}
public static CompoundTest all(Result.ExpectedValue what, String description, Test... all) {
@@ -49,7 +88,47 @@ 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 expectAll(what, RUN_GREEDY_ALL, 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 greedyAllTry(Result.ExpectedValue what, Test... all) {
+ return new CompoundTest((tests) -> {
+ int run = 0;
+ int ok = 0;
+ for (Test test : tests) {
+ if (test.hasRun()) {
+ run++;
+ if (Result.Value.fromExpected(what, test.ok()).ok()) {
+ ok++;
+ }
+ }
+ }
+ if (run == tests.length) {
+ if (ok == run) {
+ return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
+ } else {
+ return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
+ }
+ } else {
+ return new Result(Result.Value.SUCCESS, "All considered sub-tests had the expected result.");
+ }
+ }, RUN_GREEDY_ALL, all);
+ }
+
+ public static CompoundTest greedyAllTry(Result.ExpectedValue what, String description, Test... all) {
+ CompoundTest result = CompoundTest.greedyAllTry(what, all);
+ result.setDescription(description);
+ return result;
+ }
+
+ private static CompoundTest expectAny(Result.ExpectedValue what, Consumer<Test[]> runCallback, Test[] any) {
return new CompoundTest((tests) -> {
for (Test test : tests) {
if (Result.Value.fromExpected(what, test.ok()).ok()) {
@@ -57,7 +136,21 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
- }, any);
+ }, runCallback, any);
+ }
+
+ public static CompoundTest greedyAny(Result.ExpectedValue what, Test... any) {
+ return expectAny(what, RUN_GREEDY_ANY, 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 expectAny(what, RUN_ALL, any);
}
public static CompoundTest any(Result.ExpectedValue what, String description, Test... any) {
@@ -74,7 +167,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 +177,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) {
@@ -108,4 +206,9 @@ public class CompoundTest extends Test {
public String getDescription() {
return description;
}
+
+ @Override
+ public CompoundTest clone() throws CloneNotSupportedException {
+ return (CompoundTest) super.clone();
+ }
}