🚧 Add merger for free games from the Epic games store
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2024 07:51:21 +0000 (09:51 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2024 07:51:21 +0000 (09:51 +0200)
src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMerger.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMergerTest.kt [new file with mode: 0644]

diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMerger.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMerger.kt
new file mode 100644 (file)
index 0000000..80ab923
--- /dev/null
@@ -0,0 +1,18 @@
+package net.pterodactylus.rhynodge.filters.webpages.epicgames
+
+import net.pterodactylus.rhynodge.Merger
+import net.pterodactylus.rhynodge.State
+
+class EpicGamesMerger : Merger {
+
+       override fun mergeStates(previousState: State, currentState: State): State {
+               previousState as? FreeGamesState ?: throw IllegalArgumentException("previousState is not a FreeGamesState")
+               currentState as? FreeGamesState ?: throw IllegalArgumentException("currentState is not a FreeGamesState")
+
+               val oldGames = previousState.games
+               val newGames = currentState.games
+
+               return FreeGamesState(newGames, (newGames - oldGames).isNotEmpty())
+       }
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMergerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMergerTest.kt
new file mode 100644 (file)
index 0000000..49ade1f
--- /dev/null
@@ -0,0 +1,87 @@
+package net.pterodactylus.rhynodge.filters.webpages.epicgames
+
+import net.pterodactylus.rhynodge.Merger
+import net.pterodactylus.rhynodge.states.StateManagerTest.TestState
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.empty
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.instanceOf
+import org.junit.Assert.assertThrows
+import org.junit.Test
+import java.time.Instant
+
+class EpicGamesMergerTest {
+
+       @Test
+       fun `merger implements Merger interface`() {
+               assertThat(merger, instanceOf(Merger::class.java))
+       }
+
+       @Test
+       fun `merger throws exception if previous state is not a free games state`() {
+               assertThrows(IllegalArgumentException::class.java) { merger.mergeStates(TestState(), emptyState) }
+       }
+
+       @Test
+       fun `merger throws exception if current state is not a free games state`() {
+               assertThrows(IllegalArgumentException::class.java) { merger.mergeStates(emptyState, TestState()) }
+       }
+
+       @Test
+       fun `merger given two empty free game states returns an empty state`() {
+               val mergedState = merger.mergeStates(emptyState, emptyState) as FreeGamesState
+               assertThat(mergedState.games, empty())
+       }
+
+       @Test
+       fun `merging two empty states results in not-triggered state`() {
+               val mergedState = merger.mergeStates(emptyState, emptyState) as FreeGamesState
+               assertThat(mergedState.triggered(), equalTo(false))
+       }
+
+       @Test
+       fun `merging state without games into state with games returns state without games`() {
+               val mergedState = merger.mergeStates(stateWithOneGame, emptyState) as FreeGamesState
+               assertThat(mergedState.games, empty())
+       }
+
+       @Test
+       fun `merging state without games into state with games returns not-triggered state`() {
+               val mergedState = merger.mergeStates(stateWithOneGame, emptyState) as FreeGamesState
+               assertThat(mergedState.triggered(), equalTo(false))
+       }
+
+       @Test
+       fun `merging state with games into state without games returns state with games`() {
+               val mergedState = merger.mergeStates(emptyState, stateWithOneGame) as FreeGamesState
+               assertThat(mergedState.games, contains(game1))
+       }
+
+       @Test
+       fun `merging state with games into state without games returns a triggered state`() {
+               val mergedState = merger.mergeStates(emptyState, stateWithOneGame) as FreeGamesState
+               assertThat(mergedState.triggered(), equalTo(true))
+       }
+
+       @Test
+       fun `merge state with more games into state with less games returns a triggered state`() {
+               val mergedState = merger.mergeStates(stateWithOneGame, stateWithTwoGames) as FreeGamesState
+               assertThat(mergedState.triggered(), equalTo(true))
+       }
+
+       @Test
+       fun `merge state with less games into state with more games does not return a triggered state`() {
+               val mergedState = merger.mergeStates(stateWithTwoGames, stateWithOneGame) as FreeGamesState
+               assertThat(mergedState.triggered(), equalTo(false))
+       }
+
+       private val merger = EpicGamesMerger()
+
+}
+
+private val emptyState = FreeGamesState(emptySet())
+private val game1 = FreeGame("1", "i", Instant.now(), Instant.now())
+private val game2 = FreeGame("2", "i", Instant.now(), Instant.now())
+private val stateWithOneGame = FreeGamesState(setOf(game1))
+private val stateWithTwoGames = FreeGamesState(setOf(game1, game2))