diff options
author | bush-did-711 <39170869+bush-did-711@users.noreply.github.com> | 2022-03-30 23:03:09 -1000 |
---|---|---|
committer | bush-did-711 <39170869+bush-did-711@users.noreply.github.com> | 2022-03-30 23:03:09 -1000 |
commit | 0f5fdefed68d4d54f08d8e9fc9dc3a0e098f93a1 (patch) | |
tree | 5f773a9927046a0a0c41c4fb01bcda48dc3cbf6b /src/test | |
parent | 1246f0f644eb1fca30f92c601941937506ca2beb (diff) | |
download | eventbus-kotlin-0f5fdefed68d4d54f08d8e9fc9dc3a0e098f93a1.tar.gz eventbus-kotlin-0f5fdefed68d4d54f08d8e9fc9dc3a0e098f93a1.tar.bz2 eventbus-kotlin-0f5fdefed68d4d54f08d8e9fc9dc3a0e098f93a1.zip |
started adding tests, more refactoring (not done)
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/kotlin/Main.kt | 66 | ||||
-rw-r--r-- | src/test/kotlin/Test.kt | 151 |
2 files changed, 151 insertions, 66 deletions
diff --git a/src/test/kotlin/Main.kt b/src/test/kotlin/Main.kt deleted file mode 100644 index 7000c2d..0000000 --- a/src/test/kotlin/Main.kt +++ /dev/null @@ -1,66 +0,0 @@ -import me.bush.illnamethislater.* -import org.apache.logging.log4j.Level -import org.apache.logging.log4j.LogManager -import org.apache.logging.log4j.core.config.Configurator - -/** - * @author bush - * @since 1.0.0 - */ -fun main() { - Configurator.setRootLevel(Level.INFO) - - EventBus().run { - - subscribe(Subscriber()) - - post(External()) - -// val key = register(listener<Int> { -// println(it) -// }) -// -// val topLevelListenerKey = register(topLevelListener()) -// -// unsubscribe(key) -// -// unsubscribe(topLevelListenerKey) -// -// debugInfo() - } - -} - -fun topLevelListener() = listener<Int> { - println("topLevelListener(): $it") -} - -class Subscriber { - - val listener0 = listener<External>(500) { - println("listener 0") - println(it.canceled) - it.canceled = true - } - - val listener1 = listener<External>(250, receiveCancelled = true) { - println("listener 1") - println(it.canceled) - it.canceled = false - } - - val listener2 get() = listener<External> { - println("listener 2") - println(it.canceled) - it.canceled = true - } - - fun listener3() = listener<External>(-250) { - println("listener 3") - println(it.canceled) - } -} - -class External { - var canceled = false -} diff --git a/src/test/kotlin/Test.kt b/src/test/kotlin/Test.kt new file mode 100644 index 0000000..e7fb4cc --- /dev/null +++ b/src/test/kotlin/Test.kt @@ -0,0 +1,151 @@ +import me.bush.illnamethislater.Event +import me.bush.illnamethislater.EventBus +import me.bush.illnamethislater.listener +import org.apache.logging.log4j.Level +import org.apache.logging.log4j.LogManager +import org.apache.logging.log4j.core.config.Configurator +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.TestInstance.Lifecycle +import kotlin.random.Random + +/** + * I don't know how to do these.... + * + * @author bush + * @since 1.0.0 + */ +@TestInstance(Lifecycle.PER_CLASS) +class Test { + lateinit var eventBus: EventBus + private val logger = LogManager.getLogger() + + @BeforeAll + fun setup() { + Configurator.setRootLevel(Level.ALL) + + logger.info("Initializing") + eventBus = EventBus() + + logger.info("Logging empty debug info") + eventBus.debugInfo() + + logger.info("Subscribing") + eventBus.subscribe(this) + + logger.info("Testing Events") + } + + // Tests autoboxing and internal data structure against primitives. + @Test + fun primitiveListenerTest() { + val random = Random.nextInt() + eventBus.post(random) + Assertions.assertEquals(random, primitiveTestValue) + } + + var primitiveTestValue = 0 + + val primitiveListener = listener<Int> { + primitiveTestValue = it + } + + // Tests unsubscribing of "free" listeners which don't belong to a subscriber. todo allow keys to be resubscribed and test top level listeners + @Test + fun freeListenerTest() { + // Register "free" listener, and keep the returned key + val key = eventBus.register(listener<String> { + freeListenerTestValue = it + }) + val valueOne = "i love bush's eventbus <3" + val valueTwo = "sdklasdjsakdsadlksadlksdl" + // Will change the value + eventBus.post(valueOne) + Assertions.assertEquals(valueOne, freeListenerTestValue) + // Remove the listener + eventBus.unsubscribe(key) + // No effect + eventBus.post(valueTwo) + // Value will not change + Assertions.assertEquals(valueOne, freeListenerTestValue) + } + + var freeListenerTestValue: String? = null + + // Tests priority and receiveCancelled functionality. + @Test + fun myEventListenerTest() { + val event = MyEvent() + eventBus.post(event) + Assertions.assertEquals(event.lastListener, "myEventListener3") + } + + // First to be called; highest priority. + val myEventListener0 = listener<MyEvent>(priority = 10) { + Assertions.assertEquals(it.lastListener, "") + it.lastListener = "myEventListener0" + it.cancel() + } + + // Will not be called; second-highest priority, no receiveCancelled. + val myEventListener1 + get() = listener<MyEvent>(priority = 0) { + Assertions.assertTrue(false) + } + + // Second to be called; has receiveCancelled and can un-cancel the event. + fun myEventListener2() = listener<MyEvent>(priority = Int.MIN_VALUE + 100, receiveCancelled = true) { + Assertions.assertEquals(it.lastListener, "myEventListener0") + it.lastListener = "myEventListener2" + it.cancelled = false + } + + // Last to be called; does not have receiveCancelled, but the last listener un-cancelled the event. + fun myEventListener3() = listener<MyEvent>(priority = Int.MIN_VALUE) { + Assertions.assertEquals(it.lastListener, "myEventListener2") + it.lastListener = "myEventListener3" + } + + // Tests external event cancel state functionality. + @Test + fun externalEventListenerTest() { + + } + + // Tests parallel invocation functionality. todo how will this work with cancellability + @Test + fun parallelListenerTest() { + + } + + // Tests reflection against singleton object classes. + @Test + fun objectSubscriberTest() { + + } + + // todo what else? +} + +// todo test changing cancellability +class MyEvent : Event() { + override val cancellable = true + + var lastListener = "" +} + +class ExternalEvent0 { + var canceled = false +} + +class ExternalEvent1 { + var cancelled = false +} + +// Should give us a warning about duplicates +class ExternalEvent2 { + var canceled = false + var cancelled = false +} |