aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/testFixtures/java
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2023-06-15 20:41:09 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2023-06-15 20:49:33 +0200
commit2599ceeccbcfb531b3a95427b1b8904caf102242 (patch)
tree9af7b51d7cafc4d966dc4915cc899fc116998c51 /subprojects/store-query/src/testFixtures/java
parentrefactor: simplified Dnf parameter directions (diff)
downloadrefinery-2599ceeccbcfb531b3a95427b1b8904caf102242.tar.gz
refinery-2599ceeccbcfb531b3a95427b1b8904caf102242.tar.zst
refinery-2599ceeccbcfb531b3a95427b1b8904caf102242.zip
refactor(query): structural equality matcher
Add the ability to create assertions without pre-processing Dnf clauses (raw matchin mode). Also fix tests broken by Dnf pre-processing.
Diffstat (limited to 'subprojects/store-query/src/testFixtures/java')
-rw-r--r--subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/MismatchDescribingDnfEqualityChecker.java32
-rw-r--r--subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/QueryMatchers.java27
-rw-r--r--subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualTo.java2
-rw-r--r--subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualToRaw.java51
4 files changed, 105 insertions, 7 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
index a5b7f85a..6a3301b3 100644
--- 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
@@ -6,27 +6,47 @@
6package tools.refinery.store.query.tests; 6package tools.refinery.store.query.tests;
7 7
8import org.hamcrest.Description; 8import org.hamcrest.Description;
9import tools.refinery.store.query.dnf.Dnf;
10import tools.refinery.store.query.dnf.SymbolicParameter;
9import tools.refinery.store.query.equality.DeepDnfEqualityChecker; 11import tools.refinery.store.query.equality.DeepDnfEqualityChecker;
12import tools.refinery.store.query.literal.Literal;
13
14import java.util.List;
10 15
11class MismatchDescribingDnfEqualityChecker extends DeepDnfEqualityChecker { 16class MismatchDescribingDnfEqualityChecker extends DeepDnfEqualityChecker {
12 private final Description description; 17 private final Description description;
13 private boolean described; 18 private boolean raw;
19 private boolean needsDescription = true;
14 20
15 MismatchDescribingDnfEqualityChecker(Description description) { 21 MismatchDescribingDnfEqualityChecker(Description description) {
16 this.description = description; 22 this.description = description;
17 } 23 }
18 24
19 public boolean isDescribed() { 25 public boolean needsDescription() {
20 return described; 26 return needsDescription;
27 }
28
29 @Override
30 public boolean dnfEqualRaw(List<SymbolicParameter> symbolicParameters, List<? extends List<? extends Literal>> clauses, Dnf other) {
31 try {
32 raw = true;
33 boolean result = super.dnfEqualRaw(symbolicParameters, clauses, other);
34 if (!result && needsDescription) {
35 description.appendText("was ").appendText(other.toDefinitionString());
36 }
37 return false;
38 } finally {
39 raw = false;
40 }
21 } 41 }
22 42
23 @Override 43 @Override
24 protected boolean doCheckEqual(Pair pair) { 44 protected boolean doCheckEqual(Pair pair) {
25 boolean result = super.doCheckEqual(pair); 45 boolean result = super.doCheckEqual(pair);
26 if (!result && !described) { 46 if (!result && needsDescription) {
27 describeMismatch(pair); 47 describeMismatch(pair);
28 // Only describe the first found (innermost) mismatch. 48 // Only describe the first found (innermost) mismatch.
29 described = true; 49 needsDescription = false;
30 } 50 }
31 return result; 51 return result;
32 } 52 }
@@ -34,7 +54,7 @@ class MismatchDescribingDnfEqualityChecker extends DeepDnfEqualityChecker {
34 private void describeMismatch(Pair pair) { 54 private void describeMismatch(Pair pair) {
35 var inProgress = getInProgress(); 55 var inProgress = getInProgress();
36 int size = inProgress.size(); 56 int size = inProgress.size();
37 if (size <= 1) { 57 if (size <= 1 && !raw) {
38 description.appendText("was ").appendText(pair.right().toDefinitionString()); 58 description.appendText("was ").appendText(pair.right().toDefinitionString());
39 return; 59 return;
40 } 60 }
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
index 8706ef70..cd449a6a 100644
--- 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
@@ -7,13 +7,40 @@ package tools.refinery.store.query.tests;
7 7
8import org.hamcrest.Matcher; 8import org.hamcrest.Matcher;
9import tools.refinery.store.query.dnf.Dnf; 9import tools.refinery.store.query.dnf.Dnf;
10import tools.refinery.store.query.dnf.SymbolicParameter;
11import tools.refinery.store.query.literal.Literal;
12
13import java.util.List;
10 14
11public final class QueryMatchers { 15public final class QueryMatchers {
12 private QueryMatchers() { 16 private QueryMatchers() {
13 throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); 17 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
14 } 18 }
15 19
20 /**
21 * Compare two {@link Dnf} instances up to renaming of variables.
22 *
23 * @param expected The expected {@link Dnf} instance.
24 * @return A Hamcrest matcher for equality up to renaming of variables.
25 */
16 public static Matcher<Dnf> structurallyEqualTo(Dnf expected) { 26 public static Matcher<Dnf> structurallyEqualTo(Dnf expected) {
17 return new StructurallyEqualTo(expected); 27 return new StructurallyEqualTo(expected);
18 } 28 }
29
30 /**
31 * Compare a {@link Dnf} instance to another predicate in DNF form without constructing it.
32 * <p>
33 * This matcher should be used instead of {@link #structurallyEqualTo(Dnf)} when the validation and
34 * pre-processing associated with the {@link Dnf} constructor, i.e., validation of parameter directions,
35 * topological sorting of literals, and the reduction of trivial predicates is not desired. In particular, this
36 * matcher can be used to test for exact order of literal after pre-processing.
37 *
38 * @param expectedSymbolicParameters The expected list of symbolic parameters.
39 * @param expectedLiterals The expected clauses. Each clause is represented by a list of literals.
40 * @return A Hamcrest matcher for equality up to renaming of variables.
41 */
42 public static Matcher<Dnf> structurallyEqualTo(List<SymbolicParameter> expectedSymbolicParameters,
43 List<? extends List<? extends Literal>> expectedLiterals) {
44 return new StructurallyEqualToRaw(expectedSymbolicParameters, expectedLiterals);
45 }
19} 46}
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
index ba51c084..86149141 100644
--- 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
@@ -29,7 +29,7 @@ public class StructurallyEqualTo extends TypeSafeMatcher<Dnf> {
29 if (describingChecker.dnfEqual(expected, item)) { 29 if (describingChecker.dnfEqual(expected, item)) {
30 throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison"); 30 throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison");
31 } 31 }
32 if (!describingChecker.isDescribed()) { 32 if (describingChecker.needsDescription()) {
33 super.describeMismatchSafely(item, mismatchDescription); 33 super.describeMismatchSafely(item, mismatchDescription);
34 } 34 }
35 } 35 }
diff --git a/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualToRaw.java b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualToRaw.java
new file mode 100644
index 00000000..2f8c2944
--- /dev/null
+++ b/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualToRaw.java
@@ -0,0 +1,51 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.query.tests;
7
8import org.hamcrest.Description;
9import org.hamcrest.TypeSafeMatcher;
10import tools.refinery.store.query.dnf.Dnf;
11import tools.refinery.store.query.dnf.SymbolicParameter;
12import tools.refinery.store.query.equality.DeepDnfEqualityChecker;
13import tools.refinery.store.query.literal.Literal;
14
15import java.util.List;
16
17public class StructurallyEqualToRaw extends TypeSafeMatcher<Dnf> {
18 private final List<SymbolicParameter> expectedSymbolicParameters;
19 private final List<? extends List<? extends Literal>> expectedClauses;
20
21 public StructurallyEqualToRaw(List<SymbolicParameter> expectedSymbolicParameters,
22 List<? extends List<? extends Literal>> expectedClauses) {
23 this.expectedSymbolicParameters = expectedSymbolicParameters;
24 this.expectedClauses = expectedClauses;
25 }
26
27 @Override
28 protected boolean matchesSafely(Dnf item) {
29 var checker = new DeepDnfEqualityChecker();
30 return checker.dnfEqualRaw(expectedSymbolicParameters, expectedClauses, item);
31 }
32
33 @Override
34 protected void describeMismatchSafely(Dnf item, Description mismatchDescription) {
35 var describingChecker = new MismatchDescribingDnfEqualityChecker(mismatchDescription);
36 if (describingChecker.dnfEqualRaw(expectedSymbolicParameters, expectedClauses, item)) {
37 throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison");
38 }
39 if (describingChecker.needsDescription()) {
40 super.describeMismatchSafely(item, mismatchDescription);
41 }
42 }
43
44 @Override
45 public void describeTo(Description description) {
46 description.appendText("structurally equal to ")
47 .appendValueList("(", ", ", ")", expectedSymbolicParameters)
48 .appendText(" <-> ")
49 .appendValueList("", ", ", ".", expectedClauses);
50 }
51}