From 86ef8e2773272e07052e45f3406152492a8dc5bb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Dec 2021 13:24:28 +0100 Subject: [PATCH] Add some more Tests Add CommandPart.checkGuard --- .../src/de/steamwar/command/CommandPart.java | 77 ++++++++++++++----- .../steamwar/command/CommandRegistering.java | 2 + .../command/SimpleCommandPartTest.java | 37 ++++++++- 3 files changed, 92 insertions(+), 24 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java index 27762e4..2c3d394 100644 --- a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java +++ b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java @@ -39,11 +39,11 @@ public class CommandPart { private GuardChecker guard; private Class varArgType; private String optional; - private boolean help; + private GuardCheckType guardCheckType; private CommandPart next = null; - CommandPart(TypeMapper typeMapper, GuardChecker guard, Class varArgType, String optional, boolean help) { + public CommandPart(TypeMapper typeMapper, GuardChecker guard, Class varArgType, String optional, GuardCheckType guardCheckType) { this.typeMapper = typeMapper; this.guard = guard; if (optional != null && varArgType != null) { @@ -51,17 +51,17 @@ public class CommandPart { } this.varArgType = varArgType; this.optional = optional; - this.help = help; + this.guardCheckType = guardCheckType; } - void setNext(CommandPart next) { + public void setNext(CommandPart next) { if (varArgType != null) { throw new IllegalArgumentException("There can't be a next part if this is a vararg part!"); } this.next = next; } - void generateArgumentArray(List current, CommandSender commandSender, String[] args, int startIndex) { + public void generateArgumentArray(List current, CommandSender commandSender, String[] args, int startIndex) { if (startIndex >= args.length) { return; } @@ -69,7 +69,7 @@ public class CommandPart { if (varArgType != null) { Object array = Array.newInstance(varArgType, args.length - startIndex); for (int i = startIndex; i < args.length; i++) { - CheckArgumentResult validArgument = checkArgument(help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, commandSender, args, i); + CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i); if (!validArgument.success) { throw new CommandParseException(); } @@ -79,7 +79,7 @@ public class CommandPart { return; } - CheckArgumentResult validArgument = checkArgument(help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, commandSender, args, startIndex); + CheckArgumentResult validArgument = checkArgument(null, commandSender, args, startIndex); if (!validArgument.success && optional == null) { throw new CommandParseException(); } @@ -96,7 +96,40 @@ public class CommandPart { } } - void generateTabComplete(List current, CommandSender commandSender, String[] args, int startIndex) { + public boolean guardCheck(CommandSender commandSender, String[] args, int startIndex) { + if (varArgType != null) { + for (int i = startIndex; i < args.length; i++) { + GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, i); + if (guardResult == GuardResult.DENIED) { + throw new CommandNoHelpException(); + } + if (guardResult == GuardResult.DENIED_WITH_HELP) { + return false; + } + } + return true; + } + + GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, startIndex); + if (guardResult == GuardResult.DENIED) { + if (optional != null && next != null) { + return next.guardCheck(commandSender, args, startIndex); + } + throw new CommandNoHelpException(); + } + if (guardResult == GuardResult.DENIED_WITH_HELP) { + if (optional != null && next != null) { + return next.guardCheck(commandSender, args, startIndex); + } + return false; + } + if (next != null) { + return next.guardCheck(commandSender, args, startIndex + 1); + } + return true; + } + + public void generateTabComplete(List current, CommandSender commandSender, String[] args, int startIndex) { if (args.length - 1 > startIndex) { CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex); if (checkArgumentResult.success && next != null) { @@ -120,21 +153,25 @@ public class CommandPart { if (value == null) { return new CheckArgumentResult(false, null); } - if (guard != null) { - GuardResult guardResult = guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]); - switch (guardResult) { - case ALLOWED: - return new CheckArgumentResult(true, value); - case DENIED: - throw new CommandNoHelpException(); - case DENIED_WITH_HELP: - default: - return new CheckArgumentResult(false, null); - } + GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, index); + switch (guardResult) { + case ALLOWED: + return new CheckArgumentResult(true, value); + case DENIED: + throw new CommandNoHelpException(); + case DENIED_WITH_HELP: + default: + return new CheckArgumentResult(false, null); } - return new CheckArgumentResult(true, value); } catch (Exception e) { return new CheckArgumentResult(false, null); } } + + private GuardResult checkGuard(GuardCheckType guardCheckType, CommandSender commandSender, String[] args, int index) { + if (guard != null && guardCheckType != null) { + return guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]); + } + return GuardResult.ALLOWED; + } } diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandRegistering.java b/SpigotCore_Main/src/de/steamwar/command/CommandRegistering.java index 8cbf59f..143ea2a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/CommandRegistering.java +++ b/SpigotCore_Main/src/de/steamwar/command/CommandRegistering.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; @@ -27,6 +28,7 @@ import org.bukkit.command.SimpleCommandMap; import java.lang.reflect.Field; import java.util.Map; +@UtilityClass class CommandRegistering { private static final CommandMap commandMap; diff --git a/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java index 1d7b99f..d734d71 100644 --- a/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java +++ b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java @@ -36,13 +36,17 @@ public class SimpleCommandPartTest { private CommandPart intCommandPart; private CommandPart chainedCommandPart; + private CommandPart simpleGuardPart; + @Before public void setUp() throws Exception { - stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false); - intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true); + stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND); + intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND); - chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false); - chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true)); + chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND); + chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND)); + + simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND); } @Test @@ -118,4 +122,29 @@ public class SimpleCommandPartTest { System.out.println(tabCompletes); assertThat(tabCompletes.size(), is(0)); } + + @Test + public void testGuardCommandExecute() { + List argumentArray = new ArrayList<>(); + simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0); + assertThat(argumentArray.size(), is(1)); + } + + @Test(expected = CommandNoHelpException.class) + public void testGuardGuardCheck() { + simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"hello"}, 0); + } + + @Test + public void testGuardCommandExecuteValid() { + List argumentArray = new ArrayList<>(); + simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0); + assertThat(argumentArray.size(), is(1)); + } + + @Test + public void testGuardGuardCheckValid() { + boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0); + assertThat(guardResult, is(true)); + } }