Skip to content

Commit

Permalink
Add Emitter class implementation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Oct 10, 2023
1 parent 0ae992b commit 95a465f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ We are breaking down the various parts that we need to migrate to Swift in the s
| | SDK's `bucket.ts` ➡️ `Bucket.kt` ||
| | SDK's `conditions.ts` ➡️ `Conditions.kt` ||
| | SDK's `datafileReader.ts` ➡️ `DatafileReader.kt` ||
| | SDK's `emitter.ts` ➡️ `Emitter.kt` | 🟠 |
| | SDK's `feature.ts` ➡️ `Feature.kt` | |
| | SDK's `emitter.ts` ➡️ `Emitter.kt` | |
| | SDK's `feature.ts` ➡️ `Feature.kt` | 🟠 |
| | SDK's `instance.ts` ➡️ `Instance.kt` | |
| | SDK's `logger.ts` ➡️ `Logger.kt` | |
| | SDK's `segments.ts` ➡️ `segments.kt` | |
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies {
// Use the JUnit 5 integration.
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
// Uncomment when needed
// testImplementation("io.mockk:mockk:1.13.8")
testImplementation("io.mockk:mockk:1.13.8")
testImplementation("io.kotest:kotest-assertions-core:5.7.2")

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/com/featurevisor/sdk/Emitter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.featurevisor.sdk

import com.featurevisor.types.EventName

class Emitter {
private val listeners = mutableMapOf<EventName, () -> Unit>()

fun addListener(event: EventName, listener: () -> Unit) {
listeners.putIfAbsent(event, listener)
}

fun removeListener(event: EventName) {
listeners.remove(event)
}

fun removeAllListeners() {
listeners.clear()
}

operator fun invoke(event: EventName) {
listeners.getOrDefault(event, null)?.invoke()
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/com/featurevisor/types/EventName.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.featurevisor.types

enum class EventName {
READY,
REFRESH,
UPDATE,
ACTIVATION,
}
88 changes: 88 additions & 0 deletions src/test/kotlin/com/featurevisor/sdk/EmitterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.featurevisor.sdk

import com.featurevisor.types.EventName.ACTIVATION
import com.featurevisor.types.EventName.READY
import com.featurevisor.types.EventName.REFRESH
import com.featurevisor.types.EventName.UPDATE
import com.featurevisor.types.EventName.values
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test

class EmitterTest {

private val readyCallback: () -> Unit = mockk {
every { this@mockk() } answers { nothing }
}
private val refreshCallback: () -> Unit = mockk {
every { this@mockk() } answers { nothing }
}
private val updateCallback: () -> Unit = mockk {
every { this@mockk() } answers { nothing }
}
private val activationCallback: () -> Unit = mockk {
every { this@mockk() } answers { nothing }
}

private val systemUnderTest = Emitter()

@Test
fun `add listeners and confirm they are invoked`() {
systemUnderTest.addListener(READY, readyCallback)
systemUnderTest.addListener(REFRESH, refreshCallback)
systemUnderTest.addListener(UPDATE, updateCallback)
systemUnderTest.addListener(ACTIVATION, activationCallback)

values().forEach {
systemUnderTest.invoke(it)
}

verify(exactly = 1) {
readyCallback()
refreshCallback()
updateCallback()
activationCallback()
}
}

@Test
fun `removed listener is no longer invoked`() {
systemUnderTest.addListener(READY, readyCallback)
systemUnderTest.addListener(REFRESH, refreshCallback)
systemUnderTest.addListener(UPDATE, updateCallback)

systemUnderTest.removeListener(REFRESH)
values().forEach {
systemUnderTest.invoke(it)
}

verify(exactly = 1) {
readyCallback()
updateCallback()
}
verify(exactly = 0) {
refreshCallback()
}
}

@Test
fun `removeAllListeners() works correctly`() {
systemUnderTest.addListener(READY, readyCallback)
systemUnderTest.addListener(REFRESH, refreshCallback)
systemUnderTest.addListener(UPDATE, updateCallback)
systemUnderTest.addListener(ACTIVATION, activationCallback)

systemUnderTest.removeAllListeners()
values().forEach {
systemUnderTest.invoke(it)
}

verify(exactly = 0) {
readyCallback()
refreshCallback()
updateCallback()
activationCallback()
}
}
}

0 comments on commit 95a465f

Please sign in to comment.