diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-26 09:27:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-26 09:27:45 +0000 |
| commit | abbcc3915fa783c648436ef52e15da923a246dd1 (patch) | |
| tree | 2db501fd1a82dc608217623cc53b8a6b13f71bb5 /src/test/java | |
| parent | 55ec6fed7278825bee4b6db24965b69b9c496ed5 (diff) | |
Lazily combine warnings in results
Diffstat (limited to 'src/test/java')
6 files changed, 83 insertions, 51 deletions
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<List<HtmlNode>> 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<HtmlNode> convertToHtml(DocumentElement element, StyleMap styleMap) { InternalResult<List<HtmlNode>> 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<? super InternalResult<List<HtmlNode>>> isSuccess(List<HtmlNode> 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("<p>Walking on imported air</p>"))); + isSuccess("<p>Walking on imported air</p>")); } @Test public void canReadFilesWithUtf8Bom() throws IOException { assertThat( convertToHtml("utf8-bom.docx"), - deepEquals(success("<p>This XML has a byte order mark.</p>"))); + isSuccess("<p>This XML has a byte order mark.</p>")); } @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("<p></p>"))); + isSuccess("<p></p>")); } @Test public void simpleListIsConvertedToListElements() throws IOException { assertThat( convertToHtml("simple-list.docx"), - deepEquals(success("<ul><li>Apple</li><li>Banana</li></ul>"))); + isSuccess("<ul><li>Apple</li><li>Banana</li></ul>")); } @Test public void wordTablesAreConvertedToHtmlTables() throws IOException { assertThat( convertToHtml("tables.docx"), - deepEquals(success( + isSuccess( "<p>Above</p>" + "<table>" + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + "</table>" + - "<p>Below</p>"))); + "<p>Below</p>")); } @Test public void inlineImagesAreIncludedInOutput() throws IOException { assertThat( convertToHtml("tiny-picture.docx"), - deepEquals(success("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>"))); + isSuccess("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")); } @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("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>"))); + isSuccess("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")); } 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("<p><img src=\"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>"))); + isSuccess("<p><img src=\"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")); } @Test public void footnotesAreAppendedToText() throws IOException { assertThat( convertToHtml("footnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "<p>Ouch" + "<sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup>." + "<sup><a href=\"#doc-42-footnote-2\" id=\"doc-42-footnote-ref-2\">[2]</a></sup></p>" + "<ol><li id=\"doc-42-footnote-1\"><p> A tachyon walks into a bar. <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li>" + - "<li id=\"doc-42-footnote-2\"><p> Fin. <a href=\"#doc-42-footnote-ref-2\">↑</a></p></li></ol>"))); + "<li id=\"doc-42-footnote-2\"><p> Fin. <a href=\"#doc-42-footnote-ref-2\">↑</a></p></li></ol>")); } @Test public void endNotesAreAppendedToText() throws IOException { assertThat( convertToHtml("endnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "<p>Ouch" + "<sup><a href=\"#doc-42-endnote-2\" id=\"doc-42-endnote-ref-2\">[1]</a></sup>." + "<sup><a href=\"#doc-42-endnote-3\" id=\"doc-42-endnote-ref-3\">[2]</a></sup></p>" + "<ol><li id=\"doc-42-endnote-2\"><p> A tachyon walks into a bar. <a href=\"#doc-42-endnote-ref-2\">↑</a></p></li>" + - "<li id=\"doc-42-endnote-3\"><p> Fin. <a href=\"#doc-42-endnote-ref-3\">↑</a></p></li></ol>"))); + "<li id=\"doc-42-endnote-3\"><p> Fin. <a href=\"#doc-42-endnote-ref-3\">↑</a></p></li></ol>")); } @Test public void relationshipsAreReadForEachFileContainingBodyXml() throws IOException { assertThat( convertToHtml("footnote-hyperlink.docx", mammoth -> mammoth.idPrefix("doc-42-")), - deepEquals(success( + isSuccess( "<p><sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup></p>" + - "<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>"))); + "<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>")); } @Test public void textBoxesAreRead() throws IOException { assertThat( convertToHtml("text-box.docx"), - deepEquals(success("<p>Datum plane</p>"))); + isSuccess("<p>Datum plane</p>")); } @Test public void canUseCustomStyleMap() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em")), - deepEquals(success("<p><strong>The <em>Sunset</em> Tree</strong></p>"))); + isSuccess("<p><strong>The <em>Sunset</em> Tree</strong></p>")); } @Test public void mostRecentlyAddedStyleMapTakesPrecedence() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("u => strong")), - deepEquals(success("<p><strong>The <strong>Sunset</strong> Tree</strong></p>"))); + isSuccess("<p><strong>The <strong>Sunset</strong> Tree</strong></p>")); } @Test public void rulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverriden() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("strike => del")), - deepEquals(success("<p><strong>The <em>Sunset</em> Tree</strong></p>"))); + isSuccess("<p><strong>The <em>Sunset</em> Tree</strong></p>")); } @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<String> 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<InternalResult<?>> isInternalSuccess(Object value) { + return isInternalResult(deepEquals(value), list()); + } + + public static Matcher<InternalResult<?>> isInternalSuccess(Matcher<?> valueMatcher) { + return isInternalResult(valueMatcher, list()); + } + + public static Matcher<InternalResult<?>> isInternalResult(Matcher<?> valueMatcher, Iterable<String> warnings) { + return new FeatureMatcher<InternalResult<?>, Result<?>>(isResult(valueMatcher, warnings), "InternalResult as Result", "Result") { + @Override + protected Result<?> featureValueOf(InternalResult<?> actual) { + return actual.toResult(); + } + }; + } + + public static Matcher<Result<?>> isSuccess(Object value) { + return isSuccess(deepEquals(value)); + } + + public static Matcher<Result<?>> isSuccess(Matcher<?> valueMatcher) { + return isResult(valueMatcher, list()); + } + public static Matcher<Result<?>> isResult(Matcher<?> valueMatcher, Iterable<String> warnings) { return Matchers.allOf( hasProperty("value", valueMatcher), hasWarnings(warnings)); } - public static Matcher<Result<?>> hasWarnings(Iterable<String> warnings) { - return hasProperty("warnings", deepEquals(ImmutableSet.copyOf(warnings))); + public static Matcher<Object> hasWarnings(Iterable<String> 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<List<Note>> notes = reader.readElement(element); - assertThat(notes, deepEquals(success(list()))); + assertThat(notes, isInternalSuccess(list())); } } |
