package org.zwobble.mammoth.tests.docx; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; import org.zwobble.mammoth.internal.documents.*; import org.zwobble.mammoth.tests.DeepReflectionMatcher; import java.util.Optional; import static java.util.Arrays.asList; import static org.hamcrest.Matchers.*; public class DocumentMatchers { private DocumentMatchers() {} static Matcher isParagraph(Paragraph expected) { return new DeepReflectionMatcher<>(expected); } static Matcher isRun(Run expected) { return new DeepReflectionMatcher<>(expected); } static Matcher isTextElement(String value) { return new DeepReflectionMatcher<>(new Text(value)); } @SafeVarargs static Matcher hasChildren(Matcher... children) { Matcher> contains = children.length == 0 ? emptyIterable() : contains(asList(children)); return hasProperty("children", contains); } @SafeVarargs static Matcher isParagraph(Matcher... matchers) { return cast(Paragraph.class, allOf(matchers)); } static Matcher hasParagraphStyle(Matcher> style) { return hasProperty("style", style); } static Matcher isEmptyRun() { return isRun(hasChildren()); } @SafeVarargs static Matcher isRun(Matcher... matchers) { return cast(Run.class, allOf(matchers)); } @SafeVarargs static Matcher isTable(Matcher... matchers) { return cast(Table.class, allOf(matchers)); } @SafeVarargs static Matcher isRow(Matcher... matchers) { return cast(TableRow.class, allOf(matchers)); } static Matcher isHeader(boolean isHeader) { return hasProperty("header", equalTo(isHeader)); } private static Matcher cast(Class clazz, Matcher downcastMatcher) { return new TypeSafeDiagnosingMatcher() { @Override public void describeTo(Description description) { downcastMatcher.describeTo(description); } @Override protected boolean matchesSafely(Object item, Description mismatchDescription) { if (!clazz.isInstance(item)) { mismatchDescription.appendText("was a " + item.getClass().getSimpleName()); return false; } if (downcastMatcher.matches(item)) { return true; } else { downcastMatcher.describeMismatch(item, mismatchDescription); return false; } } }; } @SafeVarargs static Matcher isHyperlink(Matcher... matchers) { return cast(Hyperlink.class, allOf(matchers)); } static Matcher isCheckbox() { return instanceOf(Checkbox.class); } static Matcher isCheckbox(boolean checked) { return new DeepReflectionMatcher<>(new Checkbox(checked)); } static Matcher hasHref(String href) { return hasProperty("href", equalTo(Optional.of(href))); } static Matcher hasNoHref() { return hasProperty("href", equalTo(Optional.empty())); } static Matcher hasAnchor(String anchor) { return hasProperty("anchor", equalTo(Optional.of(anchor))); } static Matcher hasNoAnchor() { return hasProperty("anchor", equalTo(Optional.empty())); } static Matcher hasTargetFrame(String targetFrame) { return hasProperty("targetFrame", equalTo(Optional.of(targetFrame))); } static Matcher hasNoTargetFrame() { return hasProperty("targetFrame", equalTo(Optional.empty())); } }