✨ Update Sinfest filter to current HTML
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / comics / SinfestComicFilterTest.java
1 package net.pterodactylus.rhynodge.filters.comics;
2
3 import java.io.IOException;
4
5 import net.pterodactylus.rhynodge.Filter;
6 import net.pterodactylus.rhynodge.State;
7 import net.pterodactylus.rhynodge.filters.ResourceLoader;
8 import net.pterodactylus.rhynodge.states.ComicState;
9 import net.pterodactylus.rhynodge.states.ComicState.Comic;
10 import net.pterodactylus.rhynodge.states.ComicState.Strip;
11 import net.pterodactylus.rhynodge.states.HtmlState;
12
13 import org.hamcrest.Description;
14 import org.hamcrest.Matcher;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.hamcrest.TypeSafeDiagnosingMatcher;
18 import org.jsoup.nodes.Document;
19 import org.junit.Test;
20
21
22 /**
23  * Unit test for {@link SinfestComicFilter}.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class SinfestComicFilterTest {
28
29         private final Filter sinfestFilter = new SinfestComicFilter();
30         private final HtmlState htmlState;
31
32         public SinfestComicFilterTest() throws IOException {
33                 Document document = ResourceLoader.loadDocument(SinfestComicFilter.class, "sinfest.html", "https://sinfest.xyz/");
34                 htmlState = new HtmlState("https://sinfest.xyz/", document);
35         }
36
37         @Test
38         public void canParseComicsFromHtml() {
39                 State state = sinfestFilter.filter(htmlState);
40                 MatcherAssert.assertThat(state, Matchers.instanceOf(ComicState.class));
41         }
42
43         @Test
44         public void imageUrlsAreParsedCorrectly() {
45                 ComicState comicState = (ComicState) sinfestFilter.filter(htmlState);
46                 MatcherAssert.assertThat(comicState.comics(), Matchers.contains(matchesComic("October 24, 2021: Unperson 33", "https://sinfest.xyz/btphp/comics/2021-10-24.jpg", "")));
47         }
48
49         private Matcher<Comic> matchesComic(String title, String url, String comment) {
50                 return new TypeSafeDiagnosingMatcher<Comic>() {
51                         @Override
52                         protected boolean matchesSafely(Comic comic, Description mismatchDescription) {
53                                 if (!comic.title().equals(title)) {
54                                         mismatchDescription.appendText("comic is named ").appendValue(comic.title());
55                                         return false;
56                                 }
57                                 if (comic.strips().size() != 1) {
58                                         mismatchDescription.appendText("comic has ").appendValue(comic.strips().size()).appendText(" strips");
59                                         return false;
60                                 }
61                                 Strip strip = comic.strips().get(0);
62                                 if (!strip.imageUrl().equals(url)) {
63                                         mismatchDescription.appendText("image url is ").appendValue(strip.imageUrl());
64                                         return false;
65                                 }
66                                 if (!strip.comment().equals(comment)) {
67                                         mismatchDescription.appendText("comment is ").appendValue(strip.comment());
68                                         return false;
69                                 }
70                                 return true;
71                         }
72
73                         @Override
74                         public void describeTo(Description description) {
75                                 description.appendText("comic named ").appendValue(title);
76                                 description.appendText(" at ").appendValue(url);
77                                 description.appendText(" with comment ").appendValue(comment);
78                         }
79                 };
80         }
81
82 }