diff options
Diffstat (limited to 'src/test/java/org/zwobble')
5 files changed, 81 insertions, 17 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 72a6529..8538534 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -4,17 +4,16 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.hamcrest.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; @@ -35,11 +34,20 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { return matchesSafely("", expected, item, mismatchDescription); } + private static boolean deepEquals(Object expected, Object actual) { + Description description = new Description.NullDescription(); + return matchesSafely("", expected, actual, description); + } + private static <T> boolean matchesSafely(String path, T expected, T actual, Description mismatchDescription) { if (expected instanceof List && actual instanceof List) { return matchesList(path, (List)expected, (List)actual, mismatchDescription); } + if (expected instanceof Set && actual instanceof Set) { + return matchesSet(path, (Set)expected, (Set)actual, mismatchDescription); + } + if (expected instanceof Map && actual instanceof Map) { return matchesMap(path, (Map)expected, (Map)actual, mismatchDescription); } @@ -89,6 +97,32 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { return true; } + private static <T> boolean matchesSet(String path, Set<?> expected, Set<?> actual, Description mismatchDescription) { + List<?> missing = ImmutableList.copyOf(filter( + expected, + expectedElement -> !any(actual, actualElement -> deepEquals(expectedElement, actualElement)))); + + if (!missing.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("missing elements:" + + indentedList(transform(missing, DeepReflectionMatcher::describeValue))); + return false; + } + + List<?> extra = ImmutableList.copyOf(filter( + actual, + actualElement -> !any(expected, expectedElement -> deepEquals(expectedElement, actualElement)))); + + if (!extra.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("extra elements:" + + indentedList(transform(extra, DeepReflectionMatcher::describeValue))); + return false; + } + + return true; + } + private static <T> boolean matchesMap(String path, Map<?, ?> expected, Map<?, ?> actual, Description mismatchDescription) { if (!handleExtraElements(path, expected, actual, mismatchDescription, "extra elements:")) { return false; @@ -172,6 +206,9 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { } else if (value instanceof List) { List<?> list = (List)value; return "[" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "]"; + } else if (value instanceof Set) { + Set<?> list = (Set)value; + return "{" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "}"; } else if (value instanceof Map) { Map<?, ?> map = (Map)value; String entries = indentedList( diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java index befba22..aa89eb8 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java @@ -24,6 +24,7 @@ import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUPERSCRI 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.documents.DocumentElementMakers.*; @@ -79,7 +80,7 @@ public class DocumentConverterTests { deepEquals(new InternalResult<>( list(Html.element("p")), - list("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)")))); + set("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)")))); } @Test @@ -106,7 +107,7 @@ public class DocumentConverterTests { deepEquals(new InternalResult<>( list(Html.text("Hello")), - list("Unrecognised run style: Tips Run (Style ID: TipsRun)")))); + set("Unrecognised run style: Tips Run (Style ID: TipsRun)")))); } @Test diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index bc997a2..3319ebb 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -13,7 +13,9 @@ 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; public class MammothTests { @Test @@ -189,6 +191,15 @@ public class MammothTests { } @Test + public void canDisableDefaultStyleMap() throws IOException { + assertThat( + convertToHtml("simple-list.docx", mammoth -> mammoth.disableDefaultStyleMap()), + isResult( + equalTo("<p>Apple</p><p>Banana</p>"), + list("Unrecognised paragraph style: List Paragraph (Style ID: ListParagraph)"))); + } + + @Test public void canExtractRawText() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx")), diff --git a/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java new file mode 100644 index 0000000..a5b9585 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java @@ -0,0 +1,21 @@ +package org.zwobble.mammoth.tests; + +import com.google.common.collect.ImmutableSet; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.zwobble.mammoth.Result; + +import static org.hamcrest.Matchers.hasProperty; +import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; + +public class ResultMatchers { + 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))); + } +} 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 049db81..3141620 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -23,12 +23,12 @@ import java.util.function.Function; import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.*; import static org.zwobble.mammoth.internal.documents.NoteReference.endnoteReference; 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.isResult; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.NUMBERING; @@ -475,7 +475,7 @@ public class BodyXmlTests { assertThat( result, - hasProperty("warnings", deepEquals(list("Image of type image/x-emf is unlikely to display in web browsers")))); + hasWarnings(list("Image of type image/x-emf is unlikely to display in web browsers"))); } @Test @@ -594,7 +594,7 @@ public class BodyXmlTests { private static DocumentElement readSuccess(Maker<BodyXmlReader> reader, XmlElement element) { InternalResult<DocumentElement> result = read(reader, element); - assertThat(result.getWarnings(), deepEquals(list())); + assertThat(result.getWarnings(), emptyIterable()); return result.getValue(); } @@ -652,12 +652,6 @@ public class BodyXmlTests { return hasProperty("numbering", deepEquals(expected)); } - private <T> Matcher<InternalResult<? extends T>> isResult(Matcher<T> valueMatcher, List<String> warnings) { - return Matchers.allOf( - hasProperty("value", valueMatcher), - hasProperty("warnings", deepEquals(warnings))); - } - private Run run(DocumentElement... children) { return make(a(RUN, with(CHILDREN, asList(children)))); } |
