From: David ‘Bombe’ Roden Date: Wed, 26 Aug 2015 19:16:31 +0000 (+0200) Subject: Rewrite “heldentage” test X-Git-Tag: v2~156 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a72936f8df714ba842395b247789fd89aae352b4;p=rhynodge.git Rewrite “heldentage” test --- 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 index 0000000..581c6ae --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/comics/ComicMatchers.java @@ -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 David Roden + */ +public class ComicMatchers { + + public static Matcher isComic(String title, Matcher> stripsMatcher) { + return new TypeSafeDiagnosingMatcher() { + @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 isStrip(String url, String comment) { + return new TypeSafeDiagnosingMatcher() { + @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); + } + }; + } + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/comics/HeldentageFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/comics/HeldentageFilterTest.java index e308809..6d14b81 100644 --- a/src/test/java/net/pterodactylus/rhynodge/filters/comics/HeldentageFilterTest.java +++ b/src/test/java/net/pterodactylus/rhynodge/filters/comics/HeldentageFilterTest.java @@ -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 David ‘Bombe’ Roden */ -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.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", "") + )) + )); } }