aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
blob: 95fbe5d83445dca141bdab13c1e0ff0aa3dbf9e8 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package org.zwobble.mammoth.tests;

import org.junit.Test;
import org.zwobble.mammoth.DocumentConverter;
import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.html.HtmlNode;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.styles.HtmlPath;
import org.zwobble.mammoth.styles.ParagraphMatcher;
import org.zwobble.mammoth.styles.RunMatcher;
import org.zwobble.mammoth.styles.StyleMap;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Optional;

import static com.natpryce.makeiteasy.MakeItEasy.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.documents.VerticalAlignment.SUBSCRIPT;
import static org.zwobble.mammoth.documents.VerticalAlignment.SUPERSCRIPT;
import static org.zwobble.mammoth.results.Result.success;
import static org.zwobble.mammoth.results.Warning.warning;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;
import static org.zwobble.mammoth.util.MammothLists.list;
import static org.zwobble.mammoth.util.MammothMaps.map;

public class DocumentConverterTests {
    // TODO: styles (paragraph, run)

    @Test
    public void plainParagraphIsConvertedToPlainParagraph() {
        assertThat(
            convertToHtml(make(a(PARAGRAPH, with(CHILDREN, list(runWithText("Hello")))))),
            deepEquals(list(Html.element("p", list(Html.text("Hello"))))));
    }

    @Test
    public void forceWriteIsInsertedIntoParagraphIfEmptyParagraphsShouldBePreserved() {
        assertThat(
            DocumentConverter.convertToHtml("", true, StyleMap.EMPTY, make(a(PARAGRAPH))),
            deepEquals(success(list(Html.element("p", list(Html.FORCE_WRITE))))));
    }

    @Test
    public void multipleParagraphsInDocumentAreConvertedToMultipleParagraphs() {
        assertThat(
            convertToHtml(new Document(list(
                make(a(PARAGRAPH, with(CHILDREN, list(runWithText("Hello"))))),
                make(a(PARAGRAPH, with(CHILDREN, list(runWithText("there")))))), Notes.EMPTY)),

            deepEquals(list(
                Html.element("p", list(Html.text("Hello"))),
                Html.element("p", list(Html.text("there"))))));
    }

    @Test
    public void paragraphStyleMappingsCanBeUsedToMapParagraphs() {
        assertThat(
            convertToHtml(
                make(a(PARAGRAPH, with(STYLE, Optional.of(new Style("TipsParagraph", Optional.empty()))))),
                StyleMap.builder()
                    .mapParagraph(
                        ParagraphMatcher.styleId("TipsParagraph"),
                        HtmlPath.element("p", map("class", "tip")))
                    .build()),

            deepEquals(list(Html.element("p", map("class", "tip")))));
    }

    @Test
    public void warningIfParagraphHasUnrecognisedStyle() {
        assertThat(
            convertToHtmlResult(
                make(a(PARAGRAPH, with(STYLE, Optional.of(new Style("TipsParagraph", Optional.of("Tips Paragraph"))))))),

            deepEquals(new Result<>(
                list(Html.element("p")),
                list(warning("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)")))));
    }

    @Test
    public void runStyleMappingsCanBeUsedToMapRuns() {
        assertThat(
            convertToHtml(
                make(a(RUN, with(STYLE, Optional.of(new Style("TipsRun", Optional.empty()))))),
                StyleMap.builder()
                    .mapRun(
                        RunMatcher.styleId("TipsRun"),
                        HtmlPath.element("span", map("class", "tip")))
                    .build()),

            deepEquals(list(Html.element("span", map("class", "tip")))));
    }

    @Test
    public void warningIfRunHasUnrecognisedStyle() {
        assertThat(
            convertToHtmlResult(
                make(a(RUN,
                    with(STYLE, Optional.of(new Style("TipsRun", Optional.of("Tips Run")))),
                    with(CHILDREN, list(new Text("Hello")))))),

            deepEquals(new Result<>(
                list(Html.text("Hello")),
                list(warning("Unrecognised run style: Tips Run (Style ID: TipsRun)")))));
    }

    @Test
    public void boldRunsAreWrappedInStrongTags() {
        assertThat(
            convertToHtml(make(a(RUN, with(BOLD, true), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.collapsibleElement("strong", list(Html.text("Hello"))))));
    }

    @Test
    public void italicRunsAreWrappedInEmphasisTags() {
        assertThat(
            convertToHtml(make(a(RUN, with(ITALIC, true), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.collapsibleElement("em", list(Html.text("Hello"))))));
    }

    @Test
    public void underliningIsIgnoredByDefault() {
        assertThat(
            convertToHtml(make(a(RUN, with(UNDERLINE, true), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.text("Hello"))));
    }

    @Test
    public void underliningCanBeMappedUsingStyleMapping() {
        assertThat(
            convertToHtml(
                make(a(RUN, with(UNDERLINE, true), with(CHILDREN, list(new Text("Hello"))))),
                StyleMap.builder().underline(HtmlPath.element("em")).build()),

            deepEquals(list(Html.element("em", list(Html.text("Hello"))))));
    }

    @Test
    public void struckthroughRunsAreWrappedInStrikethroughTagsByDefault() {
        assertThat(
            convertToHtml(make(a(RUN, with(STRIKETHROUGH, true), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.collapsibleElement("s", list(Html.text("Hello"))))));
    }

    @Test
    public void struckthroughRunsCanBeMappedUsingStyleMapping() {
        assertThat(
            convertToHtml(
                make(a(RUN, with(STRIKETHROUGH, true), with(CHILDREN, list(new Text("Hello"))))),
                StyleMap.builder().strikethrough(HtmlPath.element("del")).build()),
            deepEquals(list(Html.element("del", list(Html.text("Hello"))))));
    }

    @Test
    public void superscriptRunsAreWrappedInSuperscriptTags() {
        assertThat(
            convertToHtml(make(a(RUN, with(VERTICAL_ALIGNMENT, SUPERSCRIPT), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.collapsibleElement("sup", list(Html.text("Hello"))))));
    }

    @Test
    public void subscriptRunsAreWrappedInSubscriptTags() {
        assertThat(
            convertToHtml(make(a(RUN, with(VERTICAL_ALIGNMENT, SUBSCRIPT), with(CHILDREN, list(new Text("Hello")))))),
            deepEquals(list(Html.collapsibleElement("sub", list(Html.text("Hello"))))));
    }

    @Test
    public void tabIsConvertedToTabInHtmlText() {
        assertThat(
            convertToHtml(Tab.TAB),
            deepEquals(list(Html.text("\t"))));
    }

    @Test
    public void lineBreakIsConvertedToBreakElement() {
        assertThat(
            convertToHtml(LineBreak.LINE_BREAK),
            deepEquals(list(Html.selfClosingElement("br"))));
    }

    @Test
    public void tableIsConvertedToHtmlTable() {
        assertThat(
            convertToHtml(new Table(list(
                new TableRow(list(
                    new TableCell(list(paragraphWithText("Top left"))),
                    new TableCell(list(paragraphWithText("Top right"))))),
                new TableRow(list(
                    new TableCell(list(paragraphWithText("Bottom left"))),
                    new TableCell(list(paragraphWithText("Bottom right")))))))),

            deepEquals(list(Html.element("table", list(
                Html.element("tr", list(
                    Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top left"))))),
                    Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top right"))))))),
                Html.element("tr", list(
                    Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Bottom left"))))),
                    Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Bottom right"))))))))))));
    }

    @Test
    public void hyperlinkWithHrefIsConvertedToAnchorTag() {
        assertThat(
            convertToHtml(Hyperlink.href("http://example.com", list(new Text("Hello")))),
            deepEquals(list(Html.collapsibleElement("a", map("href", "http://example.com"), list(Html.text("Hello"))))));
    }

    @Test
    public void hyperlinkWithInternalAnchorReferenceIsConvertedToAnchorTag() {
        assertThat(
            convertToHtml(Hyperlink.anchor("start", list(new Text("Hello")))),
            deepEquals(list(Html.collapsibleElement("a", map("href", "#doc-42-start"), list(Html.text("Hello"))))));
    }

    @Test
    public void bookmarksAreConvertedToAnchorsWithIds() {
        assertThat(
            convertToHtml(new Bookmark("start")),
            deepEquals(list(Html.element("a", map("id", "doc-42-start"), list(Html.FORCE_WRITE)))));
    }

    @Test
    public void noteReferencesAreConvertedToLinksToReferenceBodyAfterMainBody() {
        Document document = new Document(
            list(make(a(PARAGRAPH, with(CHILDREN, list(
                runWithText("Knock knock"),
                make(a(RUN, with(CHILDREN, list(new NoteReference(NoteType.FOOTNOTE, "4")))))))))),
            new Notes(list(new Note(NoteType.FOOTNOTE, "4", list(paragraphWithText("Who's there?"))))));

        assertThat(
            convertToHtml(document),
            deepEquals(list(
                Html.element("p", list(
                    Html.text("Knock knock"),
                    Html.element("sup", list(
                        Html.element("a", map("href", "#doc-42-footnote-4", "id", "doc-42-footnote-ref-4"), list(Html.text("[1]"))))))),
                Html.element("ol", list(
                    Html.element("li", map("id", "doc-42-footnote-4"), list(
                        Html.element("p", list(
                            Html.text("Who's there?"))),
                        Html.collapsibleElement("p", list(
                            Html.text(" "),
                            Html.element("a", map("href", "#doc-42-footnote-ref-4"), list(Html.text("↑"))))))))))));
    }

    @Test
    public void noteReferencesAreConvertedWithSequentialNumbers() {
        Run run = make(a(RUN, with(CHILDREN, list(
            new NoteReference(NoteType.FOOTNOTE, "4"),
            new NoteReference(NoteType.FOOTNOTE, "7")))));

        assertThat(
            convertToHtml(run),
            deepEquals(list(
                Html.element("sup", list(
                    Html.element("a", map("href", "#doc-42-footnote-4", "id", "doc-42-footnote-ref-4"), list(Html.text("[1]"))))),
                Html.element("sup", list(
                    Html.element("a", map("href", "#doc-42-footnote-7", "id", "doc-42-footnote-ref-7"), list(Html.text("[2]"))))))));
    }

    @Test
    public void imagesAreConvertedToImageTagsWithDataUriByDefault() {
        Image image = new Image(
            Optional.empty(),
            Optional.of("image/png"),
            () -> new ByteArrayInputStream(new byte[]{97, 98, 99}));
        assertThat(
            convertToHtml(image),
            deepEquals(list(Html.selfClosingElement("img", map("src", "data:image/png;base64,YWJj")))));
    }

    @Test
    public void imagesHaveAltTagsIfAvailable() {
        Image image = new Image(
            Optional.of("It's a hat"),
            Optional.of("image/png"),
            () -> new ByteArrayInputStream(new byte[]{97, 98, 99}));
        assertThat(
            convertToHtml(image),
            contains(hasProperty("attributes", hasEntry("alt", "It's a hat"))));
    }

    private List<HtmlNode> convertToHtml(Document document) {
        Result<List<HtmlNode>> result = DocumentConverter.convertToHtml("doc-42-", false, StyleMap.EMPTY, document);
        assertThat(result.getWarnings(), hasSize(0));
        return result.getValue();
    }

    private List<HtmlNode> convertToHtml(DocumentElement element) {
        return convertToHtml(element, StyleMap.EMPTY);
    }

    private List<HtmlNode> convertToHtml(DocumentElement element, StyleMap styleMap) {
        Result<List<HtmlNode>> result = convertToHtmlResult(element, styleMap);
        assertThat(result.getWarnings(), hasSize(0));
        return result.getValue();
    }

    private Result<List<HtmlNode>> convertToHtmlResult(DocumentElement element) {
        return convertToHtmlResult(element, StyleMap.EMPTY);
    }

    private Result<List<HtmlNode>> convertToHtmlResult(DocumentElement element, StyleMap styleMap) {
        return DocumentConverter.convertToHtml("doc-42-", false, styleMap, element);
    }
}