Rewrite “heldentage” test
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 26 Aug 2015 19:16:31 +0000 (21:16 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 26 Aug 2015 19:16:31 +0000 (21:16 +0200)
src/test/java/net/pterodactylus/rhynodge/filters/comics/ComicMatchers.java [new file with mode: 0644]
src/test/java/net/pterodactylus/rhynodge/filters/comics/HeldentageFilterTest.java

diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/comics/ComicMatchers.java b/src/test/java/net/pterodactylus/rhynodge/filters/comics/ComicMatchers.java
new file mode 100644 (file)
index 0000000..581c6ae
--- /dev/null
@@ -0,0 +1,65 @@
+package net.pterodactylus.rhynodge.filters.comics;
+
+import java.util.Objects;
+
+import net.pterodactylus.rhynodge.states.ComicState.Comic;
+import net.pterodactylus.rhynodge.states.ComicState.Strip;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+/**
+ * Matchers for comics.
+ *
+ * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
+ */
+public class ComicMatchers {
+
+       public static Matcher<? super Comic> isComic(String title, Matcher<Iterable<? extends Strip>> stripsMatcher) {
+               return new TypeSafeDiagnosingMatcher<Comic>() {
+                       @Override
+                       protected boolean matchesSafely(Comic comic, Description mismatchDescription) {
+                               if (!Objects.equals(comic.title(), title)) {
+                                       mismatchDescription.appendText("title is ").appendValue(comic.title());
+                                       return false;
+                               }
+                               if (!stripsMatcher.matches(comic.strips())) {
+                                       stripsMatcher.describeMismatch(comic.strips(), mismatchDescription);
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is comic with title ").appendValue(title);
+                               description.appendText(" and strips ").appendValueList("(", ", ", ")", stripsMatcher);
+                       }
+               };
+       }
+
+       public static Matcher<? super Strip> isStrip(String url, String comment) {
+               return new TypeSafeDiagnosingMatcher<Strip>() {
+                       @Override
+                       protected boolean matchesSafely(Strip strip, Description mismatchDescription) {
+                               if (!Objects.equals(strip.imageUrl(), url)) {
+                                       mismatchDescription.appendText("image URL is ").appendValue(strip.imageUrl());
+                                       return false;
+                               }
+                               if (!Objects.equals(strip.comment(), comment)) {
+                                       mismatchDescription.appendText("comment is ").appendValue(strip.comment());
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is strip from ").appendValue(url);
+                               description.appendText(" with comment ").appendValue(comment);
+                       }
+               };
+       }
+
+}
index e308809..6d14b81 100644 (file)
@@ -1,14 +1,14 @@
 package net.pterodactylus.rhynodge.filters.comics;
 
-import static com.google.common.base.Optional.absent;
-import static java.util.Arrays.asList;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.jsoup.Jsoup.parse;
+import static org.hamcrest.Matchers.contains;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
+
+import net.pterodactylus.rhynodge.filters.ComicSiteFilterTest;
+import net.pterodactylus.rhynodge.filters.ResourceLoader;
+import net.pterodactylus.rhynodge.states.ComicState;
+import net.pterodactylus.rhynodge.states.HtmlState;
 
 import org.jsoup.nodes.Document;
 import org.junit.Test;
@@ -18,34 +18,24 @@ import org.junit.Test;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class HeldentageFilterTest {
+public class HeldentageFilterTest extends ComicSiteFilterTest {
 
        private final HeldentageFilter heldentageFilter = new HeldentageFilter();
-       private final Document document;
+       private final HtmlState htmlState;
 
        public HeldentageFilterTest() throws IOException {
-               document = loadDocument("heldentage.html", "http://www.der-flix.de/");
-       }
-
-       private Document loadDocument(String resourceName, String baseUri) throws IOException {
-               InputStream inputStream = getClass().getResourceAsStream(resourceName);
-               Document document = parse(inputStream, "UTF-8", baseUri);
-               return document;
-       }
-
-       @Test
-       public void comicDoesNotHaveATitle() {
-               assertThat(heldentageFilter.extractTitle(document), is(absent()));
-       }
-
-       @Test
-       public void comicUrlCanBeFound() {
-               assertThat(heldentageFilter.extractImageUrls(document), is(asList("/images/heldentage/Tag_916.jpg")));
+               Document document = ResourceLoader.loadDocument(HeldentageFilter.class, "heldentage.html", "http://www.der-flix.de/");
+               htmlState = new HtmlState("http://www.der-flix.de/", document);
        }
 
        @Test
-       public void comicDoesNotHaveImageComments() {
-               assertThat(heldentageFilter.extractImageComments(document), is(Collections.<String>emptyList()));
+       public void comicIsParsedCorrectly() {
+               ComicState comicState = (ComicState) heldentageFilter.filter(htmlState);
+               assertThat(comicState.comics(), contains(
+                               ComicMatchers.isComic("", contains(
+                                               ComicMatchers.isStrip("http://www.der-flix.de/images/heldentage/Tag_916.jpg", "")
+                               ))
+               ));
        }
 
 }