diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-02 22:26:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-02 22:26:45 +0000 |
| commit | 871df6bd1a1f7c45e750a49589c7a195c3506205 (patch) | |
| tree | 11b552f4c2e12ec1242852c047446d1f27df20f3 /src/test/java | |
| parent | d5005305b90afe47e639fc0fa1430242556f0385 (diff) | |
Add reading of style IDs
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java | 24 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java | 32 |
2 files changed, 55 insertions, 1 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 8ee0d8f..54b74ee 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -11,6 +11,7 @@ import org.hamcrest.TypeSafeDiagnosingMatcher; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; +import java.util.Optional; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; @@ -37,6 +38,9 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { mismatchDescription.appendText("was " + actual.getClass().getName()); return false; } + if (expected instanceof Optional && actual instanceof Optional) { + return matchesOptional(path, (Optional)expected, (Optional)actual, mismatchDescription); + } if (expected instanceof String) { return matchesString(path, expected, actual, mismatchDescription); @@ -75,6 +79,26 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { return true; } + private static boolean matchesOptional(String path, Optional expected, Optional actual, Description mismatchDescription) { + if (actual.isPresent() && !expected.isPresent()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("had value " + generateDescriptionOfValue(actual.get())); + return false; + } + + if (expected.isPresent() && !actual.isPresent()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("was empty"); + return false; + } + + if (actual.isPresent() && expected.isPresent()) { + return matchesSafely(path, expected.get(), actual.get(), mismatchDescription); + } + + return true; + } + private static <T> boolean matchesString(String path, T expected, T actual, Description mismatchDescription) { Matcher<Object> matcher = Matchers.equalTo(expected); if (!matcher.matches(actual)) { diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index e2ee5a9..b0152a5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -1,6 +1,7 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import org.hamcrest.Matcher; import org.junit.Test; import org.zwobble.mammoth.documents.DocumentElement; @@ -13,9 +14,12 @@ import org.zwobble.mammoth.xml.XmlNode; import org.zwobble.mammoth.xml.XmlNodes; import java.util.List; +import java.util.Optional; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasProperty; import static org.zwobble.mammoth.docx.BodyXml.readBodyXmlElement; import static org.zwobble.mammoth.xml.XmlNodes.element; @@ -42,6 +46,28 @@ public class BodyXmlTests { isParagraph(paragraph(run(text("Hello!"))))); } + @Test + public void paragraphHasNoStyleIfItHasNoProperties() { + XmlElement element = paragraphXml(); + assertThat( + readBodyXmlElement(element), + hasStyleId(Optional.empty())); + } + + @Test + public void paragraphHasStyleIdReadFromParagraphPropertiesIfPresent() { + XmlElement element = paragraphXml(ImmutableList.of( + element("w:pPr", ImmutableList.of( + element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); + assertThat( + readBodyXmlElement(element), + hasStyleId(Optional.of("Heading1"))); + } + + private XmlElement paragraphXml() { + return paragraphXml(ImmutableList.of()); + } + private XmlElement paragraphXml(List<XmlNode> children) { return element("w:p", children); } @@ -66,8 +92,12 @@ public class BodyXmlTests { return new DeepReflectionMatcher<>(new TextElement(value)); } + private Matcher<? super DocumentElement> hasStyleId(Optional<String> expected) { + return hasProperty("styleId", equalTo(expected)); + } + private ParagraphElement paragraph(DocumentElement... children) { - return new ParagraphElement(asList(children)); + return new ParagraphElement(Optional.empty(), asList(children)); } private RunElement run(DocumentElement... children) { |
