aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-03 10:58:44 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-03 10:58:44 +0000
commitd5da134fe3107acc53987ed6ca89cefd72e0397e (patch)
tree7d318a6b260c62916c31686f0c45113f23e47418 /src/test
parent77cc4cba4a6adf6fa35513c9700b745139dba12b (diff)
Add reading of document.xml
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java7
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java53
2 files changed, 60 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
index 2668811..ef1fa6e 100644
--- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
+++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
@@ -126,6 +126,13 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
private static String generateDescriptionOfValue(Object value) {
if (value instanceof String) {
return value.toString();
+ } else if (value instanceof Optional) {
+ Optional<?> optional = (Optional)value;
+ if (optional.isPresent()) {
+ return generateDescriptionOfValue(optional.get());
+ } else {
+ return "(empty)";
+ }
} else if (value instanceof List) {
List<?> list = (List)value;
return "[" + indentedList(transform(list, DeepReflectionMatcher::generateDescriptionOfValue)) + "]";
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
new file mode 100644
index 0000000..114c1b0
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
@@ -0,0 +1,53 @@
+package org.zwobble.mammoth.tests.docx;
+
+import com.google.common.collect.ImmutableList;
+import com.natpryce.makeiteasy.Instantiator;
+import com.natpryce.makeiteasy.Property;
+import com.natpryce.makeiteasy.PropertyLookup;
+import org.junit.Test;
+import org.zwobble.mammoth.documents.*;
+import org.zwobble.mammoth.docx.BodyXml;
+import org.zwobble.mammoth.docx.DocumentXml;
+import org.zwobble.mammoth.xml.XmlElement;
+import org.zwobble.mammoth.xml.XmlNodes;
+
+import java.util.List;
+import java.util.Optional;
+
+import static com.natpryce.makeiteasy.MakeItEasy.*;
+import static com.natpryce.makeiteasy.Property.newProperty;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
+import static org.zwobble.mammoth.xml.XmlNodes.element;
+
+public class DocumentXmlTests {
+ private static final Property<HasChildren, List<DocumentElement>> CHILDREN = newProperty();
+
+ private static final Instantiator<ParagraphElement> paragraph = new Instantiator<ParagraphElement>() {
+ @Override
+ public ParagraphElement instantiate(PropertyLookup<ParagraphElement> propertyLookup) {
+ return new ParagraphElement(
+ Optional.empty(),
+ propertyLookup.valueOf(CHILDREN, ImmutableList.of()));
+ }
+ };
+
+ @Test
+ public void canReadTextWithinDocument() {
+ XmlElement documentElement = element("w:document", ImmutableList.of(
+ element("w:body", ImmutableList.of(
+ element("w:p", ImmutableList.of(
+ element("w:r", ImmutableList.of(
+ element("w:t", ImmutableList.of(
+ XmlNodes.text("Hello!")))))))))));
+
+ Document document = new DocumentXml(new BodyXml(null)).readDocumentXmlElement(documentElement);
+
+ assertThat(
+ document,
+ deepEquals(new Document(ImmutableList.of(
+ make(a(paragraph, with(CHILDREN, ImmutableList.of(
+ new RunElement(ImmutableList.of(
+ new TextElement("Hello!")))))))))));
+ }
+}