From a72936f8df714ba842395b247789fd89aae352b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 26 Aug 2015 21:16:31 +0200 Subject: [PATCH] =?utf8?q?Rewrite=20=E2=80=9Cheldentage=E2=80=9D=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../rhynodge/filters/comics/ComicMatchers.java | 65 ++++++++++++++++++++++ .../filters/comics/HeldentageFilterTest.java | 44 ++++++--------- 2 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 src/test/java/net/pterodactylus/rhynodge/filters/comics/ComicMatchers.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 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", "") + )) + )); } } -- 2.7.4