aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
blob: 8ab2dd8be0ee65e98bb0f57d11ba7f37c29cf0ad (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
package org.zwobble.mammoth.tests.docx;

import com.google.common.collect.ImmutableList;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.zwobble.mammoth.documents.DocumentElement;
import org.zwobble.mammoth.documents.ParagraphElement;
import org.zwobble.mammoth.documents.RunElement;
import org.zwobble.mammoth.documents.TextElement;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlNode;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.docx.BodyXml.readBodyXmlElement;
import static org.zwobble.mammoth.xml.XmlNodes.element;
import static org.zwobble.mammoth.xml.XmlNodes.text;

public class BodyXmlTests {
    @Test
    public void textFromTextElementIsRead() {
        XmlElement element = textXml("Hello!");
        assertThat(readBodyXmlElement(element), isTextElement("Hello!"));
    }

    @Test
    public void canReadTextWithinRun() {
        XmlElement element = runXml(ImmutableList.of(textXml("Hello!")));
        assertThat(
            readBodyXmlElement(element),
            isRunElement(isTextElement("Hello!")));
    }

    @Test
    public void canReadTextWithinParagraph() {
        XmlElement element = paragraphXml(ImmutableList.of(runXml(ImmutableList.of(textXml("Hello!")))));
        assertThat(
            readBodyXmlElement(element),
            isParagraphElement(isRunElement(isTextElement("Hello!"))));
    }

    private XmlElement paragraphXml(List<XmlNode> children) {
        return element("w:p", children);
    }

    private XmlElement runXml(List<XmlNode> children) {
        return element("w:r", children);
    }

    private XmlElement textXml(String value) {
        return element("w:t", ImmutableList.of(text(value)));
    }

    private Matcher<DocumentElement> isParagraphElement(Matcher<DocumentElement> child) {
        return allOf(
            instanceOf(ParagraphElement.class),
            hasProperty("children", contains(child)));
    }

    private Matcher<DocumentElement> isRunElement(Matcher<DocumentElement> child) {
        return allOf(
            instanceOf(RunElement.class),
            hasProperty("children", contains(child)));
    }

    private Matcher<DocumentElement> isTextElement(String value) {
        return allOf(
            instanceOf(TextElement.class),
            hasProperty("value", equalTo(value)));
    }
}