diff options
author | Kristóf Marussy <kristof@marussy.com> | 2023-02-24 20:21:15 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2023-02-24 23:29:49 +0100 |
commit | f8a3c575e400259a4985233c07b7a50e5d4d82c5 (patch) | |
tree | f5975a19fcce28eba17b5af8adde5a37ddba83c6 /subprojects/store-query/src/testFixtures | |
parent | refactor: split query and partial from store (diff) | |
download | refinery-f8a3c575e400259a4985233c07b7a50e5d4d82c5.tar.gz refinery-f8a3c575e400259a4985233c07b7a50e5d4d82c5.tar.zst refinery-f8a3c575e400259a4985233c07b7a50e5d4d82c5.zip |
feat: Dnf reduction and structural equality
Diffstat (limited to 'subprojects/store-query/src/testFixtures')
3 files changed, 93 insertions, 0 deletions
diff --git a/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/MismatchDescribingDnfEqualityChecker.java b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/MismatchDescribingDnfEqualityChecker.java new file mode 100644 index 00000000..aaab2e7e --- /dev/null +++ b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/MismatchDescribingDnfEqualityChecker.java | |||
@@ -0,0 +1,43 @@ | |||
1 | package tools.refinery.store.query.tests; | ||
2 | |||
3 | import org.hamcrest.Description; | ||
4 | import tools.refinery.store.query.equality.DeepDnfEqualityChecker; | ||
5 | |||
6 | class MismatchDescribingDnfEqualityChecker extends DeepDnfEqualityChecker { | ||
7 | private final Description description; | ||
8 | private boolean described; | ||
9 | |||
10 | MismatchDescribingDnfEqualityChecker(Description description) { | ||
11 | this.description = description; | ||
12 | } | ||
13 | |||
14 | public boolean isDescribed() { | ||
15 | return described; | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | protected boolean doCheckEqual(Pair pair) { | ||
20 | boolean result = super.doCheckEqual(pair); | ||
21 | if (!result && !described) { | ||
22 | describeMismatch(pair); | ||
23 | // Only describe the first found (innermost) mismatch. | ||
24 | described = true; | ||
25 | } | ||
26 | return result; | ||
27 | } | ||
28 | |||
29 | private void describeMismatch(Pair pair) { | ||
30 | var inProgress = getInProgress(); | ||
31 | int size = inProgress.size(); | ||
32 | if (size <= 1) { | ||
33 | description.appendText("was ").appendValue(pair.left()); | ||
34 | return; | ||
35 | } | ||
36 | var last = inProgress.get(size - 1); | ||
37 | description.appendText("expected ").appendValue(last.right()); | ||
38 | for (int i = size - 2; i >= 0; i--) { | ||
39 | description.appendText(" called from ").appendText(inProgress.get(i).left().name()); | ||
40 | } | ||
41 | description.appendText(" was not structurally equal to ").appendValue(last.right()); | ||
42 | } | ||
43 | } | ||
diff --git a/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/QueryMatchers.java b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/QueryMatchers.java new file mode 100644 index 00000000..83614278 --- /dev/null +++ b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/QueryMatchers.java | |||
@@ -0,0 +1,14 @@ | |||
1 | package tools.refinery.store.query.tests; | ||
2 | |||
3 | import org.hamcrest.Matcher; | ||
4 | import tools.refinery.store.query.Dnf; | ||
5 | |||
6 | public final class QueryMatchers { | ||
7 | private QueryMatchers() { | ||
8 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | ||
9 | } | ||
10 | |||
11 | public static Matcher<Dnf> structurallyEqualTo(Dnf expected) { | ||
12 | return new StructurallyEqualTo(expected); | ||
13 | } | ||
14 | } | ||
diff --git a/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualTo.java b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualTo.java new file mode 100644 index 00000000..a42396dd --- /dev/null +++ b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualTo.java | |||
@@ -0,0 +1,36 @@ | |||
1 | package tools.refinery.store.query.tests; | ||
2 | |||
3 | import org.hamcrest.Description; | ||
4 | import org.hamcrest.TypeSafeMatcher; | ||
5 | import tools.refinery.store.query.Dnf; | ||
6 | import tools.refinery.store.query.equality.DeepDnfEqualityChecker; | ||
7 | |||
8 | public class StructurallyEqualTo extends TypeSafeMatcher<Dnf> { | ||
9 | private final Dnf expected; | ||
10 | |||
11 | public StructurallyEqualTo(Dnf expected) { | ||
12 | this.expected = expected; | ||
13 | } | ||
14 | |||
15 | @Override | ||
16 | protected boolean matchesSafely(Dnf item) { | ||
17 | var checker = new DeepDnfEqualityChecker(); | ||
18 | return checker.dnfEqual(expected, item); | ||
19 | } | ||
20 | |||
21 | @Override | ||
22 | protected void describeMismatchSafely(Dnf item, Description mismatchDescription) { | ||
23 | var describingChecker = new MismatchDescribingDnfEqualityChecker(mismatchDescription); | ||
24 | if (describingChecker.dnfEqual(expected, item)) { | ||
25 | throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison"); | ||
26 | } | ||
27 | if (!describingChecker.isDescribed()) { | ||
28 | super.describeMismatchSafely(item, mismatchDescription); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public void describeTo(Description description) { | ||
34 | description.appendText("structurally equal to ").appendValue(expected); | ||
35 | } | ||
36 | } | ||