-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Emitter class implementation and tests
- Loading branch information
Showing
5 changed files
with
122 additions
and
3 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
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,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() | ||
} | ||
} |
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,8 @@ | ||
package com.featurevisor.types | ||
|
||
enum class EventName { | ||
READY, | ||
REFRESH, | ||
UPDATE, | ||
ACTIVATION, | ||
} |
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,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() | ||
} | ||
} | ||
} |