From 33392b02fba732c94779e4375b2940fb7fa8c92c Mon Sep 17 00:00:00 2001 From: tueem Date: Fri, 29 Nov 2024 21:41:46 +0100 Subject: [PATCH] add InteractionCheck test --- .../marinara/test/InteractionCheckTest.java | 29 ++++++++++++++ .../tomatentum/marinara/test/TestButton.java | 2 + .../marinara/test/TestInteractionCheck.java | 38 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java create mode 100644 wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java new file mode 100644 index 0000000..95773f3 --- /dev/null +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java @@ -0,0 +1,29 @@ +package net.tomatentum.marinara.test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import net.tomatentum.marinara.Marinara; +import net.tomatentum.marinara.test.mocks.ButtonInteractionMock; +import net.tomatentum.marinara.test.mocks.DiscordApiMock; +import net.tomatentum.marinara.wrapper.LibraryWrapper; +import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper; + +@TestInstance(Lifecycle.PER_CLASS) +public class InteractionCheckTest { + + @Test + public void testInteractionCheck() { + LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); + Marinara marinara = Marinara.load(wrapper); + marinara.getCheckRegistry().addCheck(new TestInteractionCheck()); + marinara.getRegistry().addInteractions(new TestButton()); + wrapper.handleInteraction(new ButtonInteractionMock()); + assertTrue(TestInteractionCheck.preExecuted); + assertTrue(TestInteractionCheck.postExecuted); + } + +} diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java index c72b8f9..eb9c9c8 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java @@ -10,12 +10,14 @@ import org.javacord.api.interaction.ButtonInteraction; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.annotation.Button; +import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck; public class TestButton implements InteractionHandler { public static boolean didRun = false; @Button("test") + @TestCheck public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) { assertNotNull(interaction); assertNotNull(channel); diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java new file mode 100644 index 0000000..b52efea --- /dev/null +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java @@ -0,0 +1,38 @@ +package net.tomatentum.marinara.test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import net.tomatentum.marinara.checks.InteractionCheck; + +public class TestInteractionCheck implements InteractionCheck { + + public static boolean preExecuted = false; + public static boolean postExecuted = false; + + @Target({ElementType.METHOD}) + @Retention(RetentionPolicy.RUNTIME) + public static @interface TestCheck { + } + + @Override + public boolean preExec(Object context, TestCheck annotation) { + assertNotNull(annotation); + assertNotNull(context); + preExecuted = true; + return true; + } + + @Override + public boolean postExec(Object context, TestCheck annotation) { + assertNotNull(annotation); + assertNotNull(context); + postExecuted = true; + return true; + } + +}