From 0bcff043394cd4a7b14f93fd0750b640b0517ef6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 11 Apr 2024 09:51:21 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20merger=20for=20free=20games?= =?utf8?q?=20from=20the=20Epic=20games=20store?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../filters/webpages/epicgames/EpicGamesMerger.kt | 18 +++++ .../webpages/epicgames/EpicGamesMergerTest.kt | 87 ++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMerger.kt create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMergerTest.kt 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 index 0000000..80ab923 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMerger.kt @@ -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 index 0000000..49ade1f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/EpicGamesMergerTest.kt @@ -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)) -- 2.7.4