aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/testing
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/shedlang/compiler/testing')
-rw-r--r--src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java b/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java
new file mode 100644
index 00000000..6736659f
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java
@@ -0,0 +1,42 @@
+package org.shedlang.compiler.testing;
+
+import org.pcollections.Empty;
+import org.pcollections.PVector;
+import org.zwobble.precisely.MatchResult;
+import org.zwobble.precisely.Matcher;
+import org.zwobble.precisely.TextTree;
+
+import static org.zwobble.precisely.Matchers.instanceOf;
+
+public abstract class MatcherBuilder<T, U, This> implements Matcher<T> {
+ private final PVector<Matcher<? super U>> submatchers;
+
+ public MatcherBuilder() {
+ this(Empty.vector());
+ }
+
+ public MatcherBuilder(PVector<Matcher<? super U>> submatchers) {
+ this.submatchers = submatchers;
+ }
+
+ @Override
+ public MatchResult match(T actual) {
+ return toMatcher().match(actual);
+ }
+
+ @Override
+ public TextTree describe() {
+ return toMatcher().describe();
+ }
+
+ protected abstract Class<U> expectedType();
+ protected abstract This create(PVector<Matcher<? super U>> submatchers);
+
+ protected This with(Matcher<? super U> submatcher) {
+ return create(submatchers.plus(submatcher));
+ }
+
+ private Matcher<T> toMatcher() {
+ return instanceOf(expectedType(), submatchers);
+ }
+}