From 211b567f44437d45eacc31fbeca5d5aee99f1d3b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 24 Oct 2021 11:52:16 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20comic=20elements=20being=20?= =?utf8?q?reordered=20randomly?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- .../rhynodge/triggers/NewComicTrigger.java | 7 ++-- .../rhynodge/triggers/NewComicTriggerTest.kt | 40 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/triggers/NewComicTriggerTest.kt diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java index 416b9e2..16560e2 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java @@ -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 allComics = new HashSet<>(previousComicState.comics()); + List allComics = new ArrayList<>(previousComicState.comics()); Set 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 index 0000000..967a890 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/triggers/NewComicTriggerTest.kt @@ -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 = (1..40).map { Comic("comic $it") } -- 2.7.4