From 7bb7911cc698902dfdf3392498951c059cdf9611 Mon Sep 17 00:00:00 2001 From: Gregory Mitchell Date: Tue, 26 Nov 2024 02:46:17 +0000 Subject: [PATCH] Create Unit Tests for `CommandUtil#hasRole` and `CommandUtil.EmbedReply#build` --- .../io/codemc/bot/utils/TestCommandUtil.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/io/codemc/bot/utils/TestCommandUtil.java diff --git a/src/test/java/io/codemc/bot/utils/TestCommandUtil.java b/src/test/java/io/codemc/bot/utils/TestCommandUtil.java new file mode 100644 index 0000000..f5c7626 --- /dev/null +++ b/src/test/java/io/codemc/bot/utils/TestCommandUtil.java @@ -0,0 +1,58 @@ +package io.codemc.bot.utils; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import io.codemc.bot.MockCodeMCBot; +import io.codemc.bot.MockJDA; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageEmbed; + +public class TestCommandUtil { + + private static final long AUTHOR_ROLE = MockCodeMCBot.INSTANCE.getConfigHandler().getLong("author_role"); + private static final long ADMIN_ROLE = 405917902865170453L; + private static final long MAINTAINER_ROLE = 659568973079379971L; + + @Test + @DisplayName("Test CommandUtil#hasRole") + public void testHasRole() { + Member m1 = MockJDA.mockMember("gmitch215"); + + assertFalse(CommandUtil.hasRole(m1, List.of(AUTHOR_ROLE))); + + MockJDA.GUILD.addRoleToMember(m1, MockJDA.AUTHOR); + + assertTrue(CommandUtil.hasRole(m1, List.of(AUTHOR_ROLE))); + + Member m2 = MockJDA.mockMember("sgdc3"); + + assertFalse(CommandUtil.hasRole(m2, List.of(ADMIN_ROLE))); + assertFalse(CommandUtil.hasRole(m2, List.of(MAINTAINER_ROLE))); + + MockJDA.GUILD.addRoleToMember(m2, MockJDA.ADMINISTRATOR); + MockJDA.GUILD.addRoleToMember(m2, MockJDA.MAINTAINER); + + assertTrue(CommandUtil.hasRole(m2, List.of(ADMIN_ROLE, MAINTAINER_ROLE))); + } + + @Test + @DisplayName("Test CommandUtil.EmbedReply#build") + public void testEmbedReply() { + CommandUtil.EmbedReply r1 = CommandUtil.EmbedReply.empty(); + MessageEmbed m1 = r1.success("Success!").build(); + + MockJDA.assertEmbed(m1, CommandUtil.embedSuccess("Success!"), true); + + CommandUtil.EmbedReply r2 = CommandUtil.EmbedReply.empty(); + MessageEmbed m2 = r2.error("Error!").build(); + + MockJDA.assertEmbed(m2, CommandUtil.embedError("Error!"), true); + } + +}