From f26feab47280a5b64429e40376c7e0d0766d2d25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 20 Sep 2020 18:37:10 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20Business=20Cat=20filter?= =?utf8?q?=20and=20test=20with=20Kotlin=20versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../filters/comics/BusinessCatComicFilter.java | 35 ------------------- .../filters/comics/BusinessCatComicFilter.kt | 18 ++++++++++ .../filters/comics/BusinessCatComicFilterTest.java | 40 ---------------------- .../filters/comics/BusinessCatComicFilterTest.kt | 28 +++++++++++++++ 4 files changed, 46 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.java create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.kt delete mode 100644 src/test/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.java create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.kt diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.java deleted file mode 100644 index 397c261..0000000 --- a/src/main/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.pterodactylus.rhynodge.filters.comics; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import net.pterodactylus.rhynodge.filters.ComicSiteFilter; - -import com.google.common.base.Optional; -import org.jsoup.nodes.Document; - -/** - * {@link ComicSiteFilter} implementation that can parse Business Cat comics. - * - * @author David ‘Bombe’ Roden - */ -public class BusinessCatComicFilter extends ComicSiteFilter { - - @Override - protected Optional extractTitle(Document document) { - String title = document.select(".comic-title h2").text(); - return title.isEmpty() ? Optional.absent() : Optional.of(title); - } - - @Override - protected List extractImageUrls(Document document) { - return Arrays.asList(document.select(".comic-image img").attr("src")); - } - - @Override - protected List extractImageComments(Document document) { - return Collections.emptyList(); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.kt new file mode 100644 index 0000000..54a94c5 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilter.kt @@ -0,0 +1,18 @@ +package net.pterodactylus.rhynodge.filters.comics + +import com.google.common.base.Optional +import net.pterodactylus.rhynodge.filters.ComicSiteFilter +import net.pterodactylus.rhynodge.utils.asOptional +import org.jsoup.nodes.Document + +class BusinessCatComicFilter : ComicSiteFilter() { + + override fun extractTitle(document: Document): Optional = + document.select(".comic-title h2").text().asOptional() + + override fun extractImageUrls(document: Document): List = + listOf(document.select(".comic-image img").attr("src")) + + override fun extractImageComments(document: Document): List = emptyList() + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.java deleted file mode 100644 index c61957a..0000000 --- a/src/test/java/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package net.pterodactylus.rhynodge.filters.comics; - -import java.io.IOException; - -import net.pterodactylus.rhynodge.filters.ResourceLoader; - -import com.google.common.base.Optional; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.jsoup.nodes.Document; -import org.junit.Test; - -/** - * Unit test for {@link BusinessCatComicFilter}. - */ -public class BusinessCatComicFilterTest { - - private final BusinessCatComicFilter filter = new BusinessCatComicFilter(); - private final Document document; - - public BusinessCatComicFilterTest() throws IOException { - document = ResourceLoader.loadDocument(getClass(), "business-cat.html", "https://www.businesscatcomic.com/"); - } - - @Test - public void imageTitleCanBeExtracted() { - MatcherAssert.assertThat(filter.extractTitle(document), Matchers.is(Optional.of("Full Circle"))); - } - - @Test - public void imageUrlsCanBeExtracted() { - MatcherAssert.assertThat(filter.extractImageUrls(document), Matchers.contains("https://www.businesscatcomic.com/wp-content/uploads/2019/11/2018-09-07-Full-Circle.png")); - } - - @Test - public void imageCommentsCanBeExtracted() { - MatcherAssert.assertThat(filter.extractImageComments(document), Matchers.empty()); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.kt new file mode 100644 index 0000000..df7bd8f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/BusinessCatComicFilterTest.kt @@ -0,0 +1,28 @@ +package net.pterodactylus.rhynodge.filters.comics + +import net.pterodactylus.rhynodge.filters.ResourceLoader +import net.pterodactylus.rhynodge.states.ComicState +import net.pterodactylus.rhynodge.states.HtmlState +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.junit.Test + +/** + * Unit test for [BusinessCatComicFilter]. + */ +class BusinessCatComicFilterTest { + + @Test + fun `comic is extracted correctly`() { + val comicState = filter.filter(htmlState) as ComicState + assertThat(comicState.comics(), contains( + ComicState.Comic("Full Circle").add(ComicState.Strip("https://www.businesscatcomic.com/wp-content/uploads/2019/11/2018-09-07-Full-Circle.png", "")) + )) + } + + private val filter = BusinessCatComicFilter() + private val htmlState: HtmlState = ResourceLoader + .loadDocument(javaClass, "business-cat.html", "https://www.businesscatcomic.com/") + .let { HtmlState("https://www.businesscatcomic.com/", it) } + +} -- 2.7.4