From: David ‘Bombe’ Roden Date: Sun, 20 Sep 2020 16:37:10 +0000 (+0200) Subject: 🎨 Replace Business Cat filter and test with Kotlin versions X-Git-Tag: v2~80 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f26feab47280a5b64429e40376c7e0d0766d2d25;p=rhynodge.git 🎨 Replace Business Cat filter and test with Kotlin versions --- 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) } + +}