diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/TestJava.java | 80 | ||||
-rw-r--r-- | src/test/kotlin/TestKotlin.kt (renamed from src/test/kotlin/Test.kt) | 50 |
2 files changed, 118 insertions, 12 deletions
diff --git a/src/test/java/TestJava.java b/src/test/java/TestJava.java new file mode 100644 index 0000000..c6c77a8 --- /dev/null +++ b/src/test/java/TestJava.java @@ -0,0 +1,80 @@ +import me.bush.illnamethislater.Listener; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +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.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import me.bush.illnamethislater.EventBus; +import org.apache.logging.log4j.LogManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import static me.bush.illnamethislater.ListenerKt.listener; + +/** + * I was getting noclassdeffound when trying to load this Java + * class in the other test and I don't care enough to fix it. + * + * @author bush + * @since 1.0.0 + */ +@TestInstance(Lifecycle.PER_CLASS) +public class TestJava { + private static boolean thisShouldChange; + private boolean thisShouldChangeToo; + private EventBus eventBus; + private final Logger logger = LogManager.getLogger(); + + @BeforeAll + public void setup() { + Configurator.setRootLevel(Level.ALL); + + // Test that init works + logger.info("Initializing"); + eventBus = new EventBus(); + + // Ensure no npe + logger.info("Logging empty debug info"); + eventBus.debugInfo(); + + // Test that basic subscribing reflection works + logger.info("Subscribing"); + eventBus.subscribe(this); + + logger.info("Testing Events"); + } + + @AfterAll + public void unsubscribe() { + logger.info("Unsubscribing"); + eventBus.unsubscribe(this); + eventBus.debugInfo(); + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void javaSubscriberTest() { + eventBus.subscribe(this); + eventBus.post(new MyEvent()); + Assertions.assertTrue(TestJava.thisShouldChange); + Assertions.assertTrue(this.thisShouldChangeToo); + // TODO: 4/2/2022 fix calling from java + } + + public Listener listener = listener(MyEvent.class, 200, event -> { + Assertions.assertEquals(event.getSomeString(), "donda"); + event.setSomeString("donda 2"); + this.thisShouldChangeToo = true; + }); + + public static Listener someStaticListener() { + return listener(MyEvent.class, 100, event -> { + Assertions.assertEquals(event.getSomeString(), "donda 2"); + thisShouldChange = true; + }); + } +} diff --git a/src/test/kotlin/Test.kt b/src/test/kotlin/TestKotlin.kt index 2bdb445..2ae6313 100644 --- a/src/test/kotlin/Test.kt +++ b/src/test/kotlin/TestKotlin.kt @@ -4,6 +4,7 @@ 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.AfterAll import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test @@ -44,6 +45,13 @@ class Test { logger.info("Testing Events") } + @AfterAll + fun unsubscribe() { + logger.info("Unsubscribing") + eventBus.unsubscribe(this) + eventBus.debugInfo() + } + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -138,13 +146,26 @@ class Test { // Tests external event cancel state functionality. @Test fun externalEventListenerTest() { - + var unchanged = "this will not change" + // Cancels the event + eventBus.register(listener<ExternalEvent>(200) { + it.canceled = true + }) + // This shouldn't be called + eventBus.register(listener<ExternalEvent>(100) { + unchanged = "changed" + }) + eventBus.post(ExternalEvent()) + Assertions.assertEquals(unchanged, "this will not change") + // Tests that duplicates are detected, and that both + // "canceled" and "cancelled" are detected as valid fields + eventBus.register(listener<ExternalDuplicates> {}) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Tests parallel invocation functionality. todo how will this work with cancellability + // Tests parallel invocation functionality. @Test fun parallelListenerTest() { @@ -156,34 +177,39 @@ class Test { // Tests reflection against singleton object classes. @Test fun objectSubscriberTest() { - + eventBus.subscribe(ObjectSubscriber) + eventBus.post(Unit) + Assertions.assertTrue(ObjectSubscriber.willChange) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // todo what else? + // todo test thread safety // todo ensure boolean functions return proper value (subscribe, unsubscribe, etc) } -// todo test changing cancellability +object ObjectSubscriber { + var willChange = false + + fun listener() = listener<Unit> { + willChange = true + } +} + class MyEvent : Event() { override val cancellable = true var lastListener = "" + var someString = "donda" } -class ExternalEvent0 { +class ExternalEvent { var canceled = false } -class ExternalEvent1 { - var cancelled = false -} - // Should give us a warning about duplicates -class ExternalEvent2 { +class ExternalDuplicates { var canceled = false var cancelled = false } |