aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/styles/parsing/DocumentMatcherParsingTests.java
blob: dc704f1b735d898f862352d3d9dc93ee8e298385 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.zwobble.mammoth.tests.styles.parsing;

import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.styles.*;
import org.zwobble.mammoth.internal.styles.parsing.DocumentMatcherParser;
import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;
import org.zwobble.mammoth.internal.styles.parsing.TokenIterator;
import org.zwobble.mammoth.internal.styles.parsing.TokenType;

import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;

public class DocumentMatcherParsingTests {
    @Test
    public void readsPlainParagraph() {
        assertThat(
            parseDocumentMatcher("p"),
            hasParagraphMatcher(ParagraphMatcher.ANY)
        );
    }

    @Test
    public void readsParagraphWithStyleId() {
        assertThat(
            parseDocumentMatcher("p.Heading1"),
            hasParagraphMatcher(ParagraphMatcher.styleId("Heading1")));
    }

    @Test
    public void readsParagraphWithExactStyleName() {
        assertThat(
            parseDocumentMatcher("p[style-name='Heading 1']"),
            hasParagraphMatcher(ParagraphMatcher.styleName(new EqualToStringMatcher("Heading 1")))
        );
    }

    @Test
    public void readsParagraphWithStyleNamePrefix() {
        assertThat(
            parseDocumentMatcher("p[style-name^='Heading']"),
            hasParagraphMatcher(ParagraphMatcher.styleName(new StartsWithStringMatcher("Heading")))
        );
    }

    @Test
    public void readsParagraphOrderedList() {
        assertThat(
            parseDocumentMatcher("p:ordered-list(2)"),
            hasParagraphMatcher(ParagraphMatcher.orderedList("1")));
    }

    @Test
    public void readsParagraphUnorderedList() {
        assertThat(
            parseDocumentMatcher("p:unordered-list(2)"),
            hasParagraphMatcher(ParagraphMatcher.unorderedList("1")));
    }

    @Test
    public void readsPlainRun() {
        assertThat(
            parseDocumentMatcher("r"),
            hasRunMatcher(RunMatcher.ANY));
    }

    @Test
    public void readsRunWithStyleId() {
        assertThat(
            parseDocumentMatcher("r.Heading1Char"),
            hasRunMatcher(RunMatcher.styleId("Heading1Char")));
    }

    @Test
    public void readsRunWithStyleName() {
        assertThat(
            parseDocumentMatcher("r[style-name='Heading 1 Char']"),
            hasRunMatcher(RunMatcher.styleName("Heading 1 Char")));
    }

    @Test
    public void readsPlainTable() {
        assertThat(
            parseDocumentMatcher("table"),
            hasTableMatcher(TableMatcher.ANY));
    }

    @Test
    public void readsTableWithStyleId() {
        assertThat(
            parseDocumentMatcher("table.TableNormal"),
            hasTableMatcher(TableMatcher.styleId("TableNormal")));
    }

    @Test
    public void readsTableWithStyleName() {
        assertThat(
            parseDocumentMatcher("table[style-name='Normal Table']"),
            hasTableMatcher(TableMatcher.styleName("Normal Table")));
    }

    @Test
    public void readsBold() {
        assertThat(
            parseDocumentMatcher("b"),
            deepEquals(new StyleMapBuilder().bold(HTML_PATH).build())
        );
    }

    @Test
    public void readsItalic() {
        assertThat(
            parseDocumentMatcher("i"),
            deepEquals(new StyleMapBuilder().italic(HTML_PATH).build())
        );
    }

    @Test
    public void readsUnderline() {
        assertThat(
            parseDocumentMatcher("u"),
            deepEquals(new StyleMapBuilder().underline(HTML_PATH).build())
        );
    }

    @Test
    public void readsStrikethrough() {
        assertThat(
            parseDocumentMatcher("strike"),
            deepEquals(new StyleMapBuilder().strikethrough(HTML_PATH).build())
        );
    }

    @Test
    public void readsAllCaps() {
        assertThat(
            parseDocumentMatcher("all-caps"),
            deepEquals(new StyleMapBuilder().allCaps(HTML_PATH).build())
        );
    }

    @Test
    public void readsSmallCaps() {
        assertThat(
            parseDocumentMatcher("small-caps"),
            deepEquals(new StyleMapBuilder().smallCaps(HTML_PATH).build())
        );
    }

    @Test
    public void readsHighlightWithoutColor() {
        assertThat(
            parseDocumentMatcher("highlight"),
            deepEquals(
                new StyleMapBuilder()
                    .mapHighlight(new HighlightMatcher(Optional.empty()), HTML_PATH)
                    .build()
            )
        );
    }

    @Test
    public void readsHighlightWithColor() {
        assertThat(
            parseDocumentMatcher("highlight[color='yellow']"),
            deepEquals(
                new StyleMapBuilder()
                    .mapHighlight(new HighlightMatcher(Optional.of("yellow")), HTML_PATH)
                    .build()
            )
        );
    }

    @Test
    public void readsCommentReference() {
        assertThat(
            parseDocumentMatcher("comment-reference"),
            deepEquals(new StyleMapBuilder().commentReference(HTML_PATH).build())
        );
    }

    @Test
    public void readsLineBreaks() {
        assertThat(
            parseDocumentMatcher("br[type='line']"),
            deepEquals(
                new StyleMapBuilder()
                    .mapBreak(BreakMatcher.LINE_BREAK, HTML_PATH)
                    .build()
            )
        );
    }

    @Test
    public void readsPageBreaks() {
        assertThat(
            parseDocumentMatcher("br[type='page']"),
            deepEquals(
                new StyleMapBuilder()
                    .mapBreak(BreakMatcher.PAGE_BREAK, HTML_PATH)
                    .build()
            )
        );
    }

    @Test
    public void readsColumnBreaks() {
        assertThat(
            parseDocumentMatcher("br[type='column']"),
            deepEquals(
                new StyleMapBuilder()
                    .mapBreak(BreakMatcher.COLUMN_BREAK, HTML_PATH)
                    .build()
            )
        );
    }

    private static final HtmlPath HTML_PATH = HtmlPath.element("placeholder");

    private StyleMap parseDocumentMatcher(String input) {
        TokenIterator<TokenType> tokens = StyleMappingTokeniser.tokenise(input);
        StyleMapBuilder builder = new StyleMapBuilder();
        DocumentMatcherParser.parse(tokens).accept(builder, HTML_PATH);
        return builder.build();
    }

    private Matcher<StyleMap> hasParagraphMatcher(ParagraphMatcher matcher) {
        return deepEquals(new StyleMapBuilder().mapParagraph(matcher, HTML_PATH).build());
    }

    private Matcher<StyleMap> hasRunMatcher(RunMatcher matcher) {
        return deepEquals(new StyleMapBuilder().mapRun(matcher, HTML_PATH).build());
    }

    private Matcher<StyleMap> hasTableMatcher(TableMatcher matcher) {
        return deepEquals(new StyleMapBuilder().mapTable(matcher, HTML_PATH).build());
    }
}