🐛 Fix comic elements being reordered randomly
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 24 Oct 2021 09:52:16 +0000 (11:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 24 Oct 2021 09:52:16 +0000 (11:52 +0200)
For some reason I decided to merge the list of existing comics and the list of
new comics into a Set, thus completely getting rid of their order.

src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java
src/test/kotlin/net/pterodactylus/rhynodge/triggers/NewComicTriggerTest.kt [new file with mode: 0644]

index 416b9e2..16560e2 100644 (file)
@@ -17,7 +17,9 @@
 
 package net.pterodactylus.rhynodge.triggers;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import net.pterodactylus.rhynodge.State;
@@ -45,11 +47,12 @@ public class NewComicTrigger implements Trigger {
                ComicState previousComicState = (ComicState) previousState;
                ComicState currentComicState = (ComicState) currentState;
 
-               Set<Comic> allComics = new HashSet<>(previousComicState.comics());
+               List<Comic> allComics = new ArrayList<>(previousComicState.comics());
                Set<Comic> newComics = new HashSet<>();
 
                for (Comic comic : currentComicState) {
-                       if (allComics.add(comic)) {
+                       if (!allComics.contains(comic)) {
+                               allComics.add(comic);
                                newComics.add(comic);
                                triggered = true;
                        }
diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/triggers/NewComicTriggerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/triggers/NewComicTriggerTest.kt
new file mode 100644 (file)
index 0000000..967a890
--- /dev/null
@@ -0,0 +1,40 @@
+package net.pterodactylus.rhynodge.triggers
+
+import net.pterodactylus.rhynodge.states.ComicState
+import net.pterodactylus.rhynodge.states.ComicState.Comic
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+class NewComicTriggerTest {
+
+       private val newComicTrigger = NewComicTrigger()
+
+       @Test
+       fun `comic trigger recognizes there are no new comic`() {
+               val oldComicState = ComicState(generateListOfComics())
+               val newComicState = ComicState(generateListOfComics())
+               newComicTrigger.mergeStates(oldComicState, newComicState)
+               assertThat(newComicTrigger.triggers(), equalTo(false))
+       }
+
+       @Test
+       fun `comic trigger recognizes new comics`() {
+               val oldComicState = ComicState(generateListOfComics())
+               val newComicState = ComicState(generateListOfComics().plusElement(Comic("new 1")))
+               newComicTrigger.mergeStates(oldComicState, newComicState)
+               assertThat(newComicTrigger.triggers(), equalTo(true))
+       }
+
+       @Test
+       fun `comic trigger does not reorder comics`() {
+               val oldComicState = ComicState(generateListOfComics())
+               val newComicState = ComicState(generateListOfComics().plusElement(Comic("new 1")))
+               val mergedComicState = newComicTrigger.mergeStates(oldComicState, newComicState) as ComicState
+               assertThat(mergedComicState.comics(), contains(*generateListOfComics().plusElement(Comic("new 1")).toTypedArray()))
+       }
+
+}
+
+private fun generateListOfComics(): List<Comic> = (1..40).map { Comic("comic $it") }