-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add requirements provider
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.goulash.core | ||
|
||
import com.goulash.core.domain.Actor | ||
|
||
/** | ||
* Stores provided requirements to be accessed by individual [Actor]s | ||
* to satisfy their activity requirements. | ||
*/ | ||
object RequirementsProvider { | ||
private val providedRequirements: MutableMap<Actor, MutableList<String>> = mutableMapOf() | ||
|
||
fun satisfy(actor: Actor, requirement: String) { | ||
val provided = providedRequirements.getOrDefault(actor, null) | ||
if (provided == null) { | ||
providedRequirements[actor] = mutableListOf(requirement) | ||
} else { | ||
provided.add(requirement) | ||
} | ||
} | ||
|
||
fun providesAll(actor: Actor): List<String> { | ||
return providedRequirements.getOrElse(actor) { listOf() } | ||
} | ||
|
||
fun isProvided(actor: Actor, requirement: String): Boolean { | ||
val provided = providedRequirements.getOrElse(actor) { mutableListOf() } | ||
if (provided.isEmpty()) return false | ||
return provided.contains(requirement) | ||
} | ||
|
||
fun consume(actor: Actor, requirement: String): Boolean { | ||
val provided = providedRequirements.getOrElse(actor) { mutableListOf() } | ||
if (provided.isEmpty()) return false | ||
return provided.remove(requirement) | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/test/kotlin/com/goulash/core/RequirementsProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.goulash.core | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.contains | ||
import assertk.assertions.containsAll | ||
import assertk.assertions.hasSize | ||
import assertk.assertions.isEmpty | ||
import assertk.assertions.isFalse | ||
import assertk.assertions.isTrue | ||
import com.goulash.core.domain.Actor | ||
import io.mockk.mockk | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class RequirementsProviderTest { | ||
@Test | ||
fun `should add multiple requirements`() { | ||
val actor: Actor = mockk() | ||
RequirementsProvider.satisfy(actor, "food") | ||
RequirementsProvider.satisfy(actor, "food") | ||
|
||
assertThat(RequirementsProvider.providesAll(actor)).hasSize(2) | ||
assertThat(RequirementsProvider.providesAll(actor)).containsAll("food", "food") | ||
} | ||
|
||
@Test | ||
fun `should consume multiple requirements`() { | ||
val actor: Actor = mockk() | ||
RequirementsProvider.satisfy(actor, "food") | ||
RequirementsProvider.satisfy(actor, "food") | ||
|
||
assertThat(RequirementsProvider.consume(actor, "food")).isTrue() | ||
assertThat(RequirementsProvider.isProvided(actor, "food")).isTrue() | ||
assertThat(RequirementsProvider.consume(actor, "food")).isTrue() | ||
assertThat(RequirementsProvider.isProvided(actor, "food")).isFalse() | ||
} | ||
|
||
@Test | ||
fun `should consume requirement`() { | ||
val actor: Actor = mockk() | ||
RequirementsProvider.satisfy(actor, "food") | ||
|
||
assertThat(RequirementsProvider.consume(actor, "food")).isTrue() | ||
assertThat(RequirementsProvider.isProvided(actor, "food")).isFalse() | ||
} | ||
|
||
@Test | ||
fun `should check if requirement is provided`() { | ||
val actor: Actor = mockk() | ||
RequirementsProvider.satisfy(actor, "food") | ||
|
||
assertThat(RequirementsProvider.isProvided(actor, "food")).isTrue() | ||
} | ||
|
||
@Test | ||
fun `should return empty list`() { | ||
assertThat(RequirementsProvider.providesAll(mockk())).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `should add requirement to the provided map`() { | ||
val actor: Actor = mockk() | ||
RequirementsProvider.satisfy(actor, "food") | ||
assertThat(RequirementsProvider.providesAll(actor)).contains("food") | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters