aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java
blob: 984ba3da9450c2273850137591b8b34c55ab67b7 (plain)
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
package org.shedlang.compiler.testing;

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(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);
    }
}