From 77cc4cba4a6adf6fa35513c9700b745139dba12b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Jan 2016 22:49:32 +0000 Subject: Pass styles into body reader --- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 49 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/test/java') 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 8c37ea9..cc00d3c 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -2,9 +2,15 @@ package org.zwobble.mammoth.tests.docx; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.natpryce.makeiteasy.Instantiator; +import com.natpryce.makeiteasy.Maker; +import com.natpryce.makeiteasy.Property; +import com.natpryce.makeiteasy.PropertyLookup; import org.hamcrest.Matcher; import org.junit.Test; import org.zwobble.mammoth.documents.*; +import org.zwobble.mammoth.docx.BodyXml; +import org.zwobble.mammoth.docx.Styles; import org.zwobble.mammoth.tests.DeepReflectionMatcher; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlNode; @@ -13,25 +19,37 @@ import org.zwobble.mammoth.xml.XmlNodes; import java.util.List; import java.util.Optional; +import static com.natpryce.makeiteasy.MakeItEasy.a; +import static com.natpryce.makeiteasy.MakeItEasy.with; +import static com.natpryce.makeiteasy.Property.newProperty; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasProperty; -import static org.zwobble.mammoth.docx.BodyXml.readBodyXmlElement; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.xml.XmlNodes.element; public class BodyXmlTests { + private static final Property STYLES = newProperty(); + + private static final Instantiator bodyReader = new Instantiator() { + @Override + public BodyXml instantiate(PropertyLookup propertyLookup) { + return new BodyXml( + propertyLookup.valueOf(STYLES, new Styles(ImmutableMap.of(), ImmutableMap.of()))); + } + }; + @Test public void textFromTextElementIsRead() { XmlElement element = textXml("Hello!"); - assertThat(readBodyXmlElement(element), isTextElement("Hello!")); + assertThat(read(a(bodyReader), element), isTextElement("Hello!")); } @Test public void canReadTextWithinRun() { XmlElement element = runXml(ImmutableList.of(textXml("Hello!"))); assertThat( - readBodyXmlElement(element), + read(a(bodyReader), element), isRun(run(text("Hello!")))); } @@ -39,7 +57,7 @@ public class BodyXmlTests { public void canReadTextWithinParagraph() { XmlElement element = paragraphXml(ImmutableList.of(runXml(ImmutableList.of(textXml("Hello!"))))); assertThat( - readBodyXmlElement(element), + read(a(bodyReader), element), isParagraph(paragraph(run(text("Hello!"))))); } @@ -47,7 +65,7 @@ public class BodyXmlTests { public void paragraphHasNoStyleIfItHasNoProperties() { XmlElement element = paragraphXml(); assertThat( - readBodyXmlElement(element), + read(a(bodyReader), element), hasStyle(Optional.empty())); } @@ -57,10 +75,29 @@ public class BodyXmlTests { element("w:pPr", ImmutableList.of( element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); assertThat( - readBodyXmlElement(element), + read(a(bodyReader), element), hasStyle(Optional.of(new Style("Heading1", Optional.empty())))); } + @Test + public void whenParagraphHasStyleIdInStylesThenStyleNameIsReadFromStyles() { + XmlElement element = paragraphXml(ImmutableList.of( + element("w:pPr", ImmutableList.of( + element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); + + Style style = new Style("Heading1", Optional.of("Heading 1")); + Styles styles = new Styles( + ImmutableMap.of("Heading1", style), + ImmutableMap.of()); + assertThat( + read(a(bodyReader, with(STYLES, styles)), element), + hasStyle(Optional.of(style))); + } + + private static DocumentElement read(Maker reader, XmlElement element) { + return reader.make().readElement(element).get(0); + } + private XmlElement paragraphXml() { return paragraphXml(ImmutableList.of()); } -- cgit v1.2.3