diff options
Diffstat (limited to 'subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java')
-rw-r--r-- | subprojects/logic/src/testFixtures/java/tools/refinery/logic/tests/MismatchDescribingDnfEqualityChecker.java | 68 |
1 files changed, 68 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 | */ | ||
6 | package tools.refinery.logic.tests; | ||
7 | |||
8 | import org.hamcrest.Description; | ||
9 | import tools.refinery.logic.dnf.Dnf; | ||
10 | import tools.refinery.logic.dnf.SymbolicParameter; | ||
11 | import tools.refinery.logic.equality.DeepDnfEqualityChecker; | ||
12 | import tools.refinery.logic.literal.Literal; | ||
13 | |||
14 | import java.util.List; | ||
15 | |||
16 | class 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 | } | ||