aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/testFixtures/java/tools/refinery/store/query/tests/StructurallyEqualTo.java
blob: a9a78f88d43cf80d130c62d7baa81a50c0cb9a65 (plain) (blame)
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
package tools.refinery.store.query.tests;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import tools.refinery.store.query.dnf.Dnf;
import tools.refinery.store.query.equality.DeepDnfEqualityChecker;

public class StructurallyEqualTo extends TypeSafeMatcher<Dnf> {
	private final Dnf expected;

	public StructurallyEqualTo(Dnf expected) {
		this.expected = expected;
	}

	@Override
	protected boolean matchesSafely(Dnf item) {
		var checker = new DeepDnfEqualityChecker();
		return checker.dnfEqual(expected, item);
	}

	@Override
	protected void describeMismatchSafely(Dnf item, Description mismatchDescription) {
		var describingChecker = new MismatchDescribingDnfEqualityChecker(mismatchDescription);
		if (describingChecker.dnfEqual(expected, item)) {
			throw new IllegalStateException("Mismatched Dnf was matching on repeated comparison");
		}
		if (!describingChecker.isDescribed()) {
			super.describeMismatchSafely(item, mismatchDescription);
		}
	}

	@Override
	public void describeTo(Description description) {
		description.appendText("structurally equal to ").appendText(expected.toDefinitionString());
	}
}