From abbcc3915fa783c648436ef52e15da923a246dd1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 26 Mar 2016 09:27:45 +0000 Subject: Lazily combine warnings in results --- .../zwobble/mammoth/tests/DocumentToHtmlTests.java | 13 ++++-- .../org/zwobble/mammoth/tests/MammothTests.java | 47 +++++++++++----------- .../org/zwobble/mammoth/tests/ResultMatchers.java | 36 +++++++++++++++-- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 15 +++---- .../mammoth/tests/docx/DocumentXmlTests.java | 10 ++--- .../mammoth/tests/docx/NotesXmlReaderTests.java | 13 +++--- 6 files changed, 83 insertions(+), 51 deletions(-) (limited to 'src/test') diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentToHtmlTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentToHtmlTests.java index 0783ac2..fb8d2a5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentToHtmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentToHtmlTests.java @@ -1,5 +1,6 @@ package org.zwobble.mammoth.tests; +import org.hamcrest.Matcher; import org.junit.Test; import org.zwobble.mammoth.internal.DocumentToHtml; import org.zwobble.mammoth.internal.DocumentToHtmlOptions; @@ -21,11 +22,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUBSCRIPT; import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUPERSCRIPT; -import static org.zwobble.mammoth.internal.results.InternalResult.success; import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.internal.util.MammothMaps.map; import static org.zwobble.mammoth.internal.util.MammothSets.set; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; +import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; public class DocumentToHtmlTests { @@ -41,7 +42,7 @@ public class DocumentToHtmlTests { DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT.preserveEmptyParagraphs(); assertThat( DocumentToHtml.convertToHtml(make(a(PARAGRAPH)), options), - deepEquals(success(list(Html.element("p", list(Html.FORCE_WRITE)))))); + isSuccess(list(Html.element("p", list(Html.FORCE_WRITE))))); } @Test @@ -307,7 +308,7 @@ public class DocumentToHtmlTests { DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT .idPrefix("doc-42-"); InternalResult> result = DocumentToHtml.convertToHtml(document, options); - assertThat(result.getWarnings(), hasSize(0)); + assertThat(result.getWarnings(), emptyIterable()); return result.getValue(); } @@ -317,7 +318,7 @@ public class DocumentToHtmlTests { private List convertToHtml(DocumentElement element, StyleMap styleMap) { InternalResult> result = convertToHtmlResult(element, styleMap); - assertThat(result.getWarnings(), hasSize(0)); + assertThat(result.getWarnings(), emptyIterable()); return result.getValue(); } @@ -331,4 +332,8 @@ public class DocumentToHtmlTests { .addStyleMap(styleMap); return DocumentToHtml.convertToHtml(element, options); } + + private Matcher>> isSuccess(List expected) { + return isInternalSuccess(deepEquals(expected)); + } } diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 7dc373c..8ac7af3 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -12,65 +12,64 @@ import java.util.function.Function; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; -import static org.zwobble.mammoth.internal.results.InternalResult.success; import static org.zwobble.mammoth.internal.util.MammothLists.list; -import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.ResultMatchers.isResult; +import static org.zwobble.mammoth.tests.ResultMatchers.isSuccess; public class MammothTests { @Test public void docxContainingOneParagraphIsConvertedToSingleParagraphElement() throws IOException { assertThat( convertToHtml("single-paragraph.docx"), - deepEquals(success("

Walking on imported air

"))); + isSuccess("

Walking on imported air

")); } @Test public void canReadFilesWithUtf8Bom() throws IOException { assertThat( convertToHtml("utf8-bom.docx"), - deepEquals(success("

This XML has a byte order mark.

"))); + isSuccess("

This XML has a byte order mark.

")); } @Test public void emptyParagraphsAreIgnoredByDefault() throws IOException { assertThat( convertToHtml("empty.docx"), - deepEquals(success(""))); + isSuccess("")); } @Test public void emptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() throws IOException { assertThat( convertToHtml("empty.docx", mammoth -> mammoth.preserveEmptyParagraphs()), - deepEquals(success("

"))); + isSuccess("

")); } @Test public void simpleListIsConvertedToListElements() throws IOException { assertThat( convertToHtml("simple-list.docx"), - deepEquals(success("
  • Apple
  • Banana
"))); + isSuccess("
  • Apple
  • Banana
")); } @Test public void wordTablesAreConvertedToHtmlTables() throws IOException { assertThat( convertToHtml("tables.docx"), - deepEquals(success( + isSuccess( "

Above

" + "" + "" + "" + "

Top left

Top right

Bottom left

Bottom right

" + - "

Below

"))); + "

Below

")); } @Test public void inlineImagesAreIncludedInOutput() throws IOException { assertThat( convertToHtml("tiny-picture.docx"), - deepEquals(success("

"))); + isSuccess("

")); } @Test @@ -82,7 +81,7 @@ public class MammothTests { Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png")); assertThat( new DocumentConverter().convertToHtml(documentPath.toFile()), - deepEquals(success("

"))); + isSuccess("

")); } finally { tempDirectory.toFile().delete(); } @@ -126,68 +125,68 @@ public class MammothTests { public void contentTypesAreRead() throws IOException { assertThat( convertToHtml("tiny-picture-custom-content-type.docx"), - deepEquals(success("

"))); + isSuccess("

")); } @Test public void footnotesAreAppendedToText() throws IOException { assertThat( convertToHtml("footnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "

Ouch" + "[1]." + "[2]

" + "
  1. A tachyon walks into a bar. ↑

  2. " + - "
  3. Fin. ↑

"))); + "
  • Fin. ↑

  • ")); } @Test public void endNotesAreAppendedToText() throws IOException { assertThat( convertToHtml("endnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "

    Ouch" + "[1]." + "[2]

    " + "
    1. A tachyon walks into a bar. ↑

    2. " + - "
    3. Fin. ↑

    "))); + "
  • Fin. ↑

  • ")); } @Test public void relationshipsAreReadForEachFileContainingBodyXml() throws IOException { assertThat( convertToHtml("footnote-hyperlink.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "

    [1]

    " + - "
    1. Example ↑

    "))); + "
    1. Example ↑

    ")); } @Test public void textBoxesAreRead() throws IOException { assertThat( convertToHtml("text-box.docx"), - deepEquals(success("

    Datum plane

    "))); + isSuccess("

    Datum plane

    ")); } @Test public void canUseCustomStyleMap() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em")), - deepEquals(success("

    The Sunset Tree

    "))); + isSuccess("

    The Sunset Tree

    ")); } @Test public void mostRecentlyAddedStyleMapTakesPrecedence() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("u => strong")), - deepEquals(success("

    The Sunset Tree

    "))); + isSuccess("

    The Sunset Tree

    ")); } @Test public void rulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverriden() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("strike => del")), - deepEquals(success("

    The Sunset Tree

    "))); + isSuccess("

    The Sunset Tree

    ")); } @Test @@ -203,14 +202,14 @@ public class MammothTests { public void canExtractRawTextFromFile() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx")), - deepEquals(success("Apple\n\nBanana\n\n"))); + isSuccess("Apple\n\nBanana\n\n")); } @Test public void canExtractRawTextFromStream() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx").toURI().toURL().openStream()), - deepEquals(success("Apple\n\nBanana\n\n"))); + isSuccess("Apple\n\nBanana\n\n")); } private Result convertToHtml(String name) throws IOException { diff --git a/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java index a5b9585..c66fbff 100644 --- a/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java +++ b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java @@ -1,21 +1,49 @@ package org.zwobble.mammoth.tests; -import com.google.common.collect.ImmutableSet; +import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.zwobble.mammoth.Result; +import org.zwobble.mammoth.internal.results.InternalResult; -import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.*; +import static org.zwobble.mammoth.internal.util.MammothLists.eagerMap; +import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; public class ResultMatchers { + public static Matcher> isInternalSuccess(Object value) { + return isInternalResult(deepEquals(value), list()); + } + + public static Matcher> isInternalSuccess(Matcher valueMatcher) { + return isInternalResult(valueMatcher, list()); + } + + public static Matcher> isInternalResult(Matcher valueMatcher, Iterable warnings) { + return new FeatureMatcher, Result>(isResult(valueMatcher, warnings), "InternalResult as Result", "Result") { + @Override + protected Result featureValueOf(InternalResult actual) { + return actual.toResult(); + } + }; + } + + public static Matcher> isSuccess(Object value) { + return isSuccess(deepEquals(value)); + } + + public static Matcher> isSuccess(Matcher valueMatcher) { + return isResult(valueMatcher, list()); + } + public static Matcher> isResult(Matcher valueMatcher, Iterable warnings) { return Matchers.allOf( hasProperty("value", valueMatcher), hasWarnings(warnings)); } - public static Matcher> hasWarnings(Iterable warnings) { - return hasProperty("warnings", deepEquals(ImmutableSet.copyOf(warnings))); + public static Matcher hasWarnings(Iterable warnings) { + return hasProperty("warnings", containsInAnyOrder(eagerMap(warnings, Matchers::equalTo))); } } 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 3141620..2a4420b 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -28,6 +28,7 @@ import static org.zwobble.mammoth.internal.documents.NoteReference.endnoteRefere import static org.zwobble.mammoth.internal.documents.NoteReference.footnoteReference; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.ResultMatchers.hasWarnings; +import static org.zwobble.mammoth.tests.ResultMatchers.isInternalResult; import static org.zwobble.mammoth.tests.ResultMatchers.isResult; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*; @@ -90,7 +91,7 @@ public class BodyXmlTests { element("w:pStyle", map("w:val", "Heading1")))))); assertThat( read(a(bodyReader), element), - isResult( + isInternalResult( hasStyle(Optional.of(new Style("Heading1", Optional.empty()))), list("Paragraph style with ID Heading1 was referenced but not defined in the document"))); } @@ -179,7 +180,7 @@ public class BodyXmlTests { assertThat( read(a(bodyReader), element), - isResult( + isInternalResult( hasStyle(Optional.of(new Style("Heading1Char", Optional.empty()))), list("Run style with ID Heading1Char was referenced but not defined in the document"))); } @@ -309,7 +310,7 @@ public class BodyXmlTests { assertThat( readAll(a(bodyReader), element), - isResult(equalTo(list()), list("Unsupported break type: page"))); + isInternalResult(equalTo(list()), list("Unsupported break type: page"))); } @Test @@ -358,7 +359,7 @@ public class BodyXmlTests { XmlElement element = element("w:bookmarkStart", map("w:name", "_GoBack")); assertThat( readAll(a(bodyReader), element), - isResult(equalTo(list()), list())); + isInternalResult(equalTo(list()), list())); } @Test @@ -409,7 +410,7 @@ public class BodyXmlTests { assertThat( readAll(a(bodyReader), paragraph), - isResult(deepEquals(expected), list())); + isInternalResult(deepEquals(expected), list())); } @Test @@ -573,7 +574,7 @@ public class BodyXmlTests { assertThat( readAll(a(bodyReader), element), - isResult(equalTo(list()), list())); + isInternalResult(equalTo(list()), list())); } @Test @@ -581,7 +582,7 @@ public class BodyXmlTests { XmlElement element = element("w:huh"); assertThat( readAll(a(bodyReader), element), - isResult(equalTo(list()), list("An unrecognised element was ignored: w:huh"))); + isInternalResult(equalTo(list()), list("An unrecognised element was ignored: w:huh"))); } @Test diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java index dc3e2b7..770b290 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java @@ -13,12 +13,12 @@ import org.zwobble.mammoth.internal.xml.XmlNodes; import static com.natpryce.makeiteasy.MakeItEasy.a; import static com.natpryce.makeiteasy.MakeItEasy.make; import static org.hamcrest.MatcherAssert.assertThat; -import static org.zwobble.mammoth.internal.results.InternalResult.success; +import static org.zwobble.mammoth.internal.util.MammothLists.list; +import static org.zwobble.mammoth.internal.xml.XmlNodes.element; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; +import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.paragraphWithText; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.bodyReader; -import static org.zwobble.mammoth.internal.util.MammothLists.list; -import static org.zwobble.mammoth.internal.xml.XmlNodes.element; public class DocumentXmlTests { @@ -36,9 +36,9 @@ public class DocumentXmlTests { assertThat( document, - deepEquals(success(new Document( + isInternalSuccess(new Document( list(paragraphWithText("Hello!")), - Notes.EMPTY)))); + Notes.EMPTY))); } @Test diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java index 67ec515..e3a1530 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java @@ -1,11 +1,11 @@ package org.zwobble.mammoth.tests.docx; import org.junit.Test; -import org.zwobble.mammoth.internal.documents.NoteType; -import org.zwobble.mammoth.internal.results.InternalResult; import org.zwobble.mammoth.internal.documents.Note; +import org.zwobble.mammoth.internal.documents.NoteType; import org.zwobble.mammoth.internal.docx.BodyXmlReader; import org.zwobble.mammoth.internal.docx.NotesXmlReader; +import org.zwobble.mammoth.internal.results.InternalResult; import org.zwobble.mammoth.internal.xml.XmlElement; import java.util.List; @@ -13,12 +13,11 @@ import java.util.List; import static com.natpryce.makeiteasy.MakeItEasy.a; import static com.natpryce.makeiteasy.MakeItEasy.make; import static org.hamcrest.MatcherAssert.assertThat; -import static org.zwobble.mammoth.internal.results.InternalResult.success; -import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; -import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH; import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.internal.util.MammothMaps.map; import static org.zwobble.mammoth.internal.xml.XmlNodes.element; +import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess; +import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH; public class NotesXmlReaderTests { private static final BodyXmlReader bodyReader = make(a(BodyXmlReaderMakers.bodyReader)); @@ -34,7 +33,7 @@ public class NotesXmlReaderTests { assertThat( notes, - deepEquals(success(list(new Note(NoteType.FOOTNOTE, "1", list(make(a(PARAGRAPH)))))))); + isInternalSuccess(list(new Note(NoteType.FOOTNOTE, "1", list(make(a(PARAGRAPH))))))); } @Test @@ -55,6 +54,6 @@ public class NotesXmlReaderTests { NotesXmlReader reader = NotesXmlReader.footnote(bodyReader); InternalResult> notes = reader.readElement(element); - assertThat(notes, deepEquals(success(list()))); + assertThat(notes, isInternalSuccess(list())); } } -- cgit v1.2.3