summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/DocumentMatchers.java
blob: 4f3628ecba20f4f7642b7f10aa1603f8eb991604 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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<DocumentElement> isParagraph(Paragraph expected) {
        return new DeepReflectionMatcher<>(expected);
    }

    static Matcher<DocumentElement> isRun(Run expected) {
        return new DeepReflectionMatcher<>(expected);
    }

    static Matcher<DocumentElement> isTextElement(String value) {
        return new DeepReflectionMatcher<>(new Text(value));
    }

    @SafeVarargs
    static Matcher<HasChildren> hasChildren(Matcher<DocumentElement>... children) {
        Matcher<Iterable<? extends DocumentElement>> contains = children.length == 0 ? emptyIterable() : contains(asList(children));
        return hasProperty("children", contains);
    }

    @SafeVarargs
    static Matcher<DocumentElement> isParagraph(Matcher<? super Paragraph>... matchers) {
        return cast(Paragraph.class, allOf(matchers));
    }

    static Matcher<Paragraph> hasParagraphStyle(Matcher<Optional<Style>> style) {
        return hasProperty("style", style);
    }

    static Matcher<DocumentElement> isEmptyRun() {
        return isRun(hasChildren());
    }

    @SafeVarargs
    static Matcher<DocumentElement> isRun(Matcher<? super Run>... matchers) {
        return cast(Run.class, allOf(matchers));
    }

    @SafeVarargs
    static Matcher<DocumentElement> isTable(Matcher<? super Table>... matchers) {
        return cast(Table.class, allOf(matchers));
    }

    @SafeVarargs
    static Matcher<DocumentElement> isRow(Matcher<? super TableRow>... matchers) {
        return cast(TableRow.class, allOf(matchers));
    }

    static Matcher<TableRow> isHeader(boolean isHeader) {
        return hasProperty("header", equalTo(isHeader));
    }

    private static <T, U> Matcher<U> cast(Class<T> clazz, Matcher<? super T> downcastMatcher) {
        return new TypeSafeDiagnosingMatcher<U>() {
            @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<DocumentElement> isHyperlink(Matcher<? super Hyperlink>... matchers) {
        return cast(Hyperlink.class, allOf(matchers));
    }

    static Matcher<Hyperlink> hasHref(String href) {
        return hasProperty("href", equalTo(Optional.of(href)));
    }

    static Matcher<Hyperlink> hasNoHref() {
        return hasProperty("href", equalTo(Optional.empty()));
    }

    static Matcher<Hyperlink> hasAnchor(String anchor) {
        return hasProperty("anchor", equalTo(Optional.of(anchor)));
    }

    static Matcher<Hyperlink> hasNoAnchor() {
        return hasProperty("anchor", equalTo(Optional.empty()));
    }

    static Matcher<Hyperlink> hasTargetFrame(String targetFrame) {
        return hasProperty("targetFrame", equalTo(Optional.of(targetFrame)));
    }

    static Matcher<Hyperlink> hasNoTargetFrame() {
        return hasProperty("targetFrame", equalTo(Optional.empty()));
    }
}