diff options
Diffstat (limited to 'src')
12 files changed, 170 insertions, 94 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index d2d2cc9..ad95656 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -77,7 +77,7 @@ public class DocumentConverter { */ public Result<String> convertToHtml(InputStream stream) throws IOException { return withDocxFile(stream, zipFile -> - convertToHtml(Optional.empty(), zipFile)); + convertToHtml(Optional.empty(), zipFile)).toResult(); } /** @@ -85,7 +85,7 @@ public class DocumentConverter { */ public Result<String> convertToHtml(File file) throws IOException { return withDocxFile(file, zipFile -> - convertToHtml(Optional.of(file.toPath()), zipFile)); + convertToHtml(Optional.of(file.toPath()), zipFile)).toResult(); } private InternalResult<String> convertToHtml(Optional<Path> path, DocxFile zipFile) { @@ -103,7 +103,7 @@ public class DocumentConverter { */ public Result<String> extractRawText(InputStream stream) throws IOException { return withDocxFile(stream, zipFile -> - extractRawText(Optional.empty(), zipFile)); + extractRawText(Optional.empty(), zipFile)).toResult(); } /** @@ -113,7 +113,7 @@ public class DocumentConverter { */ public Result<String> extractRawText(File file) throws IOException { return withDocxFile(file, zipFile -> - extractRawText(Optional.of(file.toPath()), zipFile)); + extractRawText(Optional.of(file.toPath()), zipFile)).toResult(); } private InternalResult<String> extractRawText(Optional<Path> path, DocxFile zipFile) { diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java index 9951cbc..0edc7a7 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java @@ -18,7 +18,7 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static org.zwobble.mammoth.internal.docx.ReadResult.EMPTY_SUCCESS; import static org.zwobble.mammoth.internal.docx.ReadResult.success; -import static org.zwobble.mammoth.internal.util.MammothSets.set; +import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.internal.util.MammothStrings.trimLeft; public class BodyXmlReader { @@ -213,7 +213,7 @@ public class BodyXmlReader { } else { return new InternalResult<>( Optional.of(new Style(styleId, Optional.empty())), - set(styleType + " style with ID " + styleId + " was referenced but not defined in the document")); + list(styleType + " style with ID " + styleId + " was referenced but not defined in the document")); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java b/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java index 8f3053d..95b886f 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java @@ -1,32 +1,26 @@ package org.zwobble.mammoth.internal.docx; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; import org.zwobble.mammoth.internal.documents.DocumentElement; import org.zwobble.mammoth.internal.results.InternalResult; import java.util.List; -import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyConcat; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyFlatMap; +import static org.zwobble.mammoth.internal.util.MammothLists.eagerFlatMap; import static org.zwobble.mammoth.internal.util.MammothLists.list; -import static org.zwobble.mammoth.internal.util.MammothSets.set; public class ReadResult { public static final ReadResult EMPTY_SUCCESS = success(list()); public static ReadResult concat(Iterable<ReadResult> results) { - ImmutableList.Builder<DocumentElement> elements = ImmutableList.builder(); - ImmutableList.Builder<DocumentElement> extra = ImmutableList.builder(); - ImmutableSet.Builder<String> warnings = ImmutableSet.builder(); - for (ReadResult result : results) { - elements.addAll(result.elements); - extra.addAll(result.extra); - warnings.addAll(result.warnings); - } - return new ReadResult(elements.build(), extra.build(), warnings.build()); + return new ReadResult( + eagerFlatMap(results, result -> result.elements), + eagerFlatMap(results, result -> result.extra), + lazyFlatMap(results, result -> result.warnings)); } public static <T> ReadResult map( @@ -37,7 +31,7 @@ public class ReadResult { return new ReadResult( list(function.apply(first.getValue(), second.elements)), second.extra, - Sets.union(first.getWarnings(), second.warnings).immutableCopy()); + lazyConcat(first.getWarnings(), second.warnings)); } public static ReadResult success(DocumentElement element) { @@ -45,22 +39,22 @@ public class ReadResult { } public static ReadResult success(List<DocumentElement> elements) { - return new ReadResult(elements, list(), set()); + return new ReadResult(elements, list(), list()); } public static ReadResult emptyWithWarning(String warning) { - return new ReadResult(list(), list(), set(warning)); + return new ReadResult(list(), list(), list(warning)); } public static ReadResult withWarning(DocumentElement element, String warning) { - return new ReadResult(list(element), list(), set(warning)); + return new ReadResult(list(element), list(), list(warning)); } private final List<DocumentElement> elements; private final List<DocumentElement> extra; - private final Set<String> warnings; + private final Iterable<String> warnings; - public ReadResult(List<DocumentElement> elements, List<DocumentElement> extra, Set<String> warnings) { + public ReadResult(List<DocumentElement> elements, List<DocumentElement> extra, Iterable<String> warnings) { this.elements = elements; this.extra = extra; this.warnings = warnings; diff --git a/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java b/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java index 6ca3377..35c195d 100644 --- a/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java +++ b/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java @@ -1,8 +1,7 @@ package org.zwobble.mammoth.internal.results; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; +import org.zwobble.mammoth.Result; import org.zwobble.mammoth.internal.documents.Style; import java.util.List; @@ -11,17 +10,16 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; -import static org.zwobble.mammoth.internal.util.MammothSets.set; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyConcat; +import static org.zwobble.mammoth.internal.util.MammothIterables.lazyFlatMap; +import static org.zwobble.mammoth.internal.util.MammothLists.eagerMap; +import static org.zwobble.mammoth.internal.util.MammothLists.list; -public class InternalResult<T> implements org.zwobble.mammoth.Result<T> { +public class InternalResult<T> { public static <T> InternalResult<List<T>> concat(Iterable<InternalResult<T>> results) { - ImmutableList.Builder<T> elements = ImmutableList.builder(); - ImmutableSet.Builder<String> warnings = ImmutableSet.builder(); - for (InternalResult<T> result : results) { - elements.add(result.value); - warnings.addAll(result.warnings); - } - return new InternalResult<>(elements.build(), warnings.build()); + return new InternalResult<>( + eagerMap(results, result -> result.value), + lazyFlatMap(results, result -> result.warnings)); } public static <T1, T2, R> InternalResult<R> map( @@ -31,21 +29,21 @@ public class InternalResult<T> implements org.zwobble.mammoth.Result<T> { { return new InternalResult<>( function.apply(first.value, second.value), - Sets.union(first.warnings, second.warnings).immutableCopy()); + lazyConcat(first.warnings, second.warnings)); } public static InternalResult<Optional<Style>> empty() { - return new InternalResult<>(Optional.empty(), set()); + return new InternalResult<>(Optional.empty(), list()); } public static <T> InternalResult<T> success(T value) { - return new InternalResult<>(value, set()); + return new InternalResult<>(value, list()); } private final T value; - private final Set<String> warnings; + private final Iterable<String> warnings; - public InternalResult(T value, Set<String> warnings) { + public InternalResult(T value, Iterable<String> warnings) { this.value = value; this.warnings = warnings; } @@ -54,7 +52,7 @@ public class InternalResult<T> implements org.zwobble.mammoth.Result<T> { return value; } - public Set<String> getWarnings() { + public Iterable<String> getWarnings() { return warnings; } @@ -66,6 +64,21 @@ public class InternalResult<T> implements org.zwobble.mammoth.Result<T> { InternalResult<R> intermediateResult = function.apply(value); return new InternalResult<>( intermediateResult.value, - Sets.union(warnings, intermediateResult.warnings).immutableCopy()); + lazyConcat(warnings, intermediateResult.warnings)); + } + + public Result<T> toResult() { + Set<String> warnings = ImmutableSet.copyOf(this.warnings); + return new Result<T>() { + @Override + public T getValue() { + return value; + } + + @Override + public Set<String> getWarnings() { + return warnings; + } + }; } } diff --git a/src/main/java/org/zwobble/mammoth/internal/util/MammothIterables.java b/src/main/java/org/zwobble/mammoth/internal/util/MammothIterables.java index b9fd029..c0231e0 100644 --- a/src/main/java/org/zwobble/mammoth/internal/util/MammothIterables.java +++ b/src/main/java/org/zwobble/mammoth/internal/util/MammothIterables.java @@ -6,6 +6,8 @@ import java.util.Iterator; import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; public class MammothIterables { public static <T> Optional<T> tryGetLast(Iterable<T> iterable) { @@ -39,6 +41,30 @@ public class MammothIterables { } } + public static <T, R> Iterable<R> lazyFlatMap(Iterable<T> iterable, Function<T, Iterable<R>> function) { + return lazyFlatten(lazyMap(iterable, function)); + } + + public static <T> Iterable<T> lazyConcat(Iterable<T> iterable1, Iterable<T> iterable2) { + return new Iterable<T>() { + @Override + public Iterator<T> iterator() { + return Stream.concat(stream(iterable1), stream(iterable2)).iterator(); + } + }; + } + + public static <T> Iterable<T> lazyFlatten(Iterable<? extends Iterable<T>> iterables) { + return new Iterable<T>() { + @Override + public Iterator<T> iterator() { + return stream(iterables) + .flatMap(iterable -> stream(iterable)) + .iterator(); + } + }; + } + private static <T, R> Iterator<R> map(Iterator<T> iterator, Function<T, R> function) { return new Iterator<R>() { @Override @@ -52,4 +78,8 @@ public class MammothIterables { } }; } + + public static <T> Stream<T> stream(Iterable<T> iterable) { + return StreamSupport.stream(iterable.spliterator(), false); + } } diff --git a/src/main/java/org/zwobble/mammoth/internal/util/MammothLists.java b/src/main/java/org/zwobble/mammoth/internal/util/MammothLists.java index eff5411..aa45ebf 100644 --- a/src/main/java/org/zwobble/mammoth/internal/util/MammothLists.java +++ b/src/main/java/org/zwobble/mammoth/internal/util/MammothLists.java @@ -6,6 +6,9 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Ordering; import java.util.List; +import java.util.stream.Collectors; + +import static org.zwobble.mammoth.internal.util.MammothIterables.stream; public class MammothLists { public static <T> List<T> list() { @@ -47,4 +50,8 @@ public class MammothLists { private static <T, R extends Comparable<R>> Ordering<T> orderBy(Function<T, R> getKey) { return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second))); } + + public static <T> List<T> toList(Iterable<T> iterable) { + return stream(iterable).collect(Collectors.toList()); + } } 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())); } } |
