aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/logic/src/testFixtures/java/tools
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/logic/src/testFixtures/java/tools')
-rw-r--r--subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java68
-rw-r--r--subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/QueryMatchers.java46
-rw-r--r--subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualTo.java41
-rw-r--r--subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualToRaw.java51
4 files changed, 206 insertions, 0 deletions
diff --git a/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java
new file mode 100644
index 00000000..aa73baec
--- /dev/null
+++ b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java
@@ -0,0 +1,68 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.logic.tests;
7
8import org.hamcrest.Description;
9import tools.refinery.logic.dnf.Dnf;
10import tools.refinery.logic.dnf.SymbolicParameter;
11import tools.refinery.logic.equality.DeepDnfEqualityChecker;
12import tools.refinery.logic.literal.Literal;
13
14import java.util.List;
15
16class MismatchDescribingDnfEqualityChecker extends DeepDnfEqualityChecker {
17 private final Description description;
18 private boolean raw;
19 private boolean needsDescription = true;
20
21 MismatchDescribingDnfEqualityChecker(Description description) {
22 this.description = description;
23 }
24
25 public boolean needsDescription() {
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 }
41 }
42
43 @Override
44 protected boolean doCheckEqual(Pair pair) {
45 boolean result = super.doCheckEqual(pair);
46 if (!result && needsDescription) {
47 describeMismatch(pair);
48 // Only describe the first found (innermost) mismatch.
49 needsDescription = false;
50 }
51 return result;
52 }
53
54 private void describeMismatch(Pair pair) {
55 var inProgress = getInProgress();
56 int size = inProgress.size();
57 if (size <= 1 && !raw) {
58 description.appendText("was ").appendText(pair.right().toDefinitionString());
59 return;
60 }
61 var last = inProgress.get(size - 1);
62 description.appendText("expected ").appendText(last.left().toDefinitionString());
63 for (int i = size - 2; i >= 0; i--) {
64 description.appendText(" called from ").appendText(inProgress.get(i).left().toString());
65 }
66 description.appendText(" was not structurally equal to ").appendText(last.right().toDefinitionString());
67 }
68}
diff --git a/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/QueryMatchers.java b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/QueryMatchers.java
new file mode 100644
index 00000000..40332a8c
--- /dev/null
+++ b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/QueryMatchers.java
@@ -0,0 +1,46 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.logic.tests;
7
8import org.hamcrest.Matcher;
9import tools.refinery.logic.dnf.Dnf;
10import tools.refinery.logic.dnf.SymbolicParameter;
11import tools.refinery.logic.literal.Literal;
12
13import java.util.List;
14
15public final class QueryMatchers {
16 private QueryMatchers() {
17 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
18 }
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 */
26 public static Matcher<Dnf> structurallyEqualTo(Dnf expected) {
27 return new StructurallyEqualTo(expected);
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 }
46}
diff --git a/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualTo.java b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualTo.java
new file mode 100644
index 00000000..257e6850
--- /dev/null
+++ b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualTo.java
@@ -0,0 +1,41 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.logic.tests;
7
8import org.hamcrest.Description;
9import org.hamcrest.TypeSafeMatcher;
10import tools.refinery.logic.dnf.Dnf;
11import tools.refinery.logic.equality.DeepDnfEqualityChecker;
12
13public class StructurallyEqualTo extends TypeSafeMatcher<Dnf> {
14 private final Dnf expected;
15
16 public StructurallyEqualTo(Dnf expected) {
17 this.expected = expected;
18 }
19
20 @Override
21 protected boolean matchesSafely(Dnf item) {
22 var checker = new DeepDnfEqualityChecker();
23 return checker.dnfEqual(expected, item);
24 }
25
26 @Override
27 protected void describeMismatchSafely(Dnf item, Description mismatchDescription) {
28 var describingChecker = new MismatchDescribingDnfEqualityChecker(mismatchDescription);
29 if (describingChecker.dnfEqual(expected, item)) {
30 throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison");
31 }
32 if (describingChecker.needsDescription()) {
33 super.describeMismatchSafely(item, mismatchDescription);
34 }
35 }
36
37 @Override
38 public void describeTo(Description description) {
39 description.appendText("structurally equal to ").appendText(expected.toDefinitionString());
40 }
41}
diff --git a/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualToRaw.java b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/StructurallyEqualToRaw.java
new file mode 100644
index 00000000..944ab1ac
--- /dev/null
+++ b/subprojects/logic/src/testFixtures/java/tools/refinery/logic/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.logic.tests;
7
8import org.hamcrest.Description;
9import org.hamcrest.TypeSafeMatcher;
10import tools.refinery.logic.dnf.Dnf;
11import tools.refinery.logic.dnf.SymbolicParameter;
12import tools.refinery.logic.equality.DeepDnfEqualityChecker;
13import tools.refinery.logic.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}