diff options
Diffstat (limited to '')
| -rw-r--r-- | src/cz/crcs/ectester/common/test/CompoundTest.java | 108 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/test/Result.java (renamed from src/cz/crcs/ectester/reader/test/Result.java) | 4 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/test/Test.java | 50 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/test/TestException.java | 11 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/test/Testable.java | 13 |
5 files changed, 185 insertions, 1 deletions
diff --git a/src/cz/crcs/ectester/common/test/CompoundTest.java b/src/cz/crcs/ectester/common/test/CompoundTest.java new file mode 100644 index 0000000..bcf4a0e --- /dev/null +++ b/src/cz/crcs/ectester/common/test/CompoundTest.java @@ -0,0 +1,108 @@ +package cz.crcs.ectester.common.test; + +import java.util.function.Function; + +/** + * A compound test that runs many Tests and has a Result dependent on all/some of their Results. + * + * @author Jan Jancar johny@neuromancer.sk + */ +public class CompoundTest extends Test { + private Function<Test[], Result> callback; + private Test[] tests; + private String description; + + private CompoundTest(Function<Test[], Result> callback, Test... tests) { + this.callback = callback; + this.tests = tests; + } + + private CompoundTest(Function<Test[], Result> callback, String descripiton, Test... tests) { + this(callback, tests); + this.description = descripiton; + } + + public static CompoundTest function(Function<Test[], Result> callback, Test... tests) { + return new CompoundTest(callback, tests); + } + + public static CompoundTest function(Function<Test[], Result> callback, String description, Test... tests) { + return new CompoundTest(callback, description, tests); + } + + public static CompoundTest all(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, "At least one of the sub-tests did not have the expected result."); + } + } + return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result."); + }, all); + } + + public static CompoundTest all(Result.ExpectedValue what, String description, Test... all) { + CompoundTest result = CompoundTest.all(what, all); + 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, "At least one of the sub-tests did have the expected result."); + } + } + return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result."); + }, any); + } + + public static CompoundTest any(Result.ExpectedValue what, String description, Test... any) { + CompoundTest result = CompoundTest.any(what, any); + result.setDescription(description); + return result; + } + + public static CompoundTest mask(Result.ExpectedValue[] results, Test... masked) { + return new CompoundTest((tests) -> { + for (int i = 0; i < results.length; ++i) { + if (!Result.Value.fromExpected(results[i], tests[i].ok()).ok()) { + return new Result(Result.Value.FAILURE, "At least one of the sub-tests did not match the result mask."); + } + } + return new Result(Result.Value.SUCCESS, "All sub-tests matched the expected mask."); + }, masked); + } + + public static CompoundTest mask(Result.ExpectedValue[] results, String description, Test... masked) { + CompoundTest result = CompoundTest.mask(results, masked); + result.setDescription(description); + return result; + } + + public Test[] getTests() { + return tests; + } + + @Override + public void run() throws TestException { + if (hasRun) + return; + + for (Test test : tests) { + test.run(); + } + result = callback.apply(tests); + this.hasRun = true; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String getDescription() { + return description; + } +} diff --git a/src/cz/crcs/ectester/reader/test/Result.java b/src/cz/crcs/ectester/common/test/Result.java index 82f0f32..523a9d7 100644 --- a/src/cz/crcs/ectester/reader/test/Result.java +++ b/src/cz/crcs/ectester/common/test/Result.java @@ -1,6 +1,8 @@ -package cz.crcs.ectester.reader.test; +package cz.crcs.ectester.common.test; /** + * A Result of a Test. Has a Value and an optional String cause. + * * @author Jan Jancar johny@neuromancer.sk */ public class Result { diff --git a/src/cz/crcs/ectester/common/test/Test.java b/src/cz/crcs/ectester/common/test/Test.java new file mode 100644 index 0000000..8c025b8 --- /dev/null +++ b/src/cz/crcs/ectester/common/test/Test.java @@ -0,0 +1,50 @@ +package cz.crcs.ectester.common.test; + +import static cz.crcs.ectester.common.test.Result.Value; + +/** + * An abstract test that can be run and has a Result. + * + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class Test { + protected boolean hasRun = false; + protected Result result; + + public Result getResult() { + if (!hasRun) { + return null; + } + return result; + } + + public Value getResultValue() { + if (!hasRun) { + return null; + } + return result.getValue(); + } + + public String getResultCause() { + if (!hasRun) { + return null; + } + return result.getCause(); + } + + public boolean ok() { + if (!hasRun) { + return true; + } + return result.ok(); + } + + public abstract String getDescription(); + + public boolean hasRun() { + return hasRun; + } + + public abstract void run() throws TestException; + +} diff --git a/src/cz/crcs/ectester/common/test/TestException.java b/src/cz/crcs/ectester/common/test/TestException.java new file mode 100644 index 0000000..01d195c --- /dev/null +++ b/src/cz/crcs/ectester/common/test/TestException.java @@ -0,0 +1,11 @@ +package cz.crcs.ectester.common.test; + +/** + * + * @author Jan Jancar johny@neuromancer.sk + */ +public class TestException extends Exception { + public TestException(Throwable e) { + super(e); + } +} diff --git a/src/cz/crcs/ectester/common/test/Testable.java b/src/cz/crcs/ectester/common/test/Testable.java new file mode 100644 index 0000000..e033b0a --- /dev/null +++ b/src/cz/crcs/ectester/common/test/Testable.java @@ -0,0 +1,13 @@ +package cz.crcs.ectester.common.test; + +/** + * + * @author Jan Jancar johny@neuromancer.sk + */ +public interface Testable { + + boolean hasRun(); + void run() throws TestException; + boolean ok(); + boolean error(); +} |
