diff --git a/src/main/java/fr/xephi/authme/command/CommandDescription.java b/src/main/java/fr/xephi/authme/command/CommandDescription.java index 51ce67b0b..4feec54d3 100644 --- a/src/main/java/fr/xephi/authme/command/CommandDescription.java +++ b/src/main/java/fr/xephi/authme/command/CommandDescription.java @@ -681,6 +681,7 @@ public class CommandDescription { * * @param maximumArguments True if there is an argument maximum, based on the number of registered arguments. */ + // TODO ljacqu 20151121: Rename the setter public void setMaximumArguments(boolean maximumArguments) { this.noArgumentMaximum = !maximumArguments; } diff --git a/src/main/java/fr/xephi/authme/command/CommandManager.java b/src/main/java/fr/xephi/authme/command/CommandManager.java index 162d61d46..be5b28bf8 100644 --- a/src/main/java/fr/xephi/authme/command/CommandManager.java +++ b/src/main/java/fr/xephi/authme/command/CommandManager.java @@ -54,6 +54,7 @@ public class CommandManager { /** * Register all commands. */ + // TODO ljacqu 20151121: Create a builder class for CommandDescription @SuppressWarnings({ "serial" }) public void registerCommands() { // Register the base AuthMe Reloaded command diff --git a/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java b/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java index 55f2581ac..5f564749b 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java @@ -9,7 +9,11 @@ import fr.xephi.authme.util.ListUtils; /** */ -public class HelpSyntaxHelper { +public final class HelpSyntaxHelper { + + private HelpSyntaxHelper() { + // Helper class + } /** * Get the proper syntax for a command. @@ -19,8 +23,8 @@ public class HelpSyntaxHelper { * @param alternativeLabel The alternative label to use for this command syntax. * @param highlight True to highlight the important parts of this command. * - - * @return The command with proper syntax. */ + * @return The command with proper syntax. + */ @SuppressWarnings("StringConcatenationInsideStringBufferAppend") public static String getCommandSyntax(CommandDescription commandDescription, CommandParts commandReference, String alternativeLabel, boolean highlight) { // Create a string builder to build the command @@ -35,9 +39,9 @@ public class HelpSyntaxHelper { String commandLabel = helpCommandReference.get(helpCommandReference.getCount() - 1); // Check whether the alternative label should be used - if(alternativeLabel != null) - if(alternativeLabel.trim().length() > 0) - commandLabel = alternativeLabel; + if (alternativeLabel != null && alternativeLabel.trim().length() > 0) { + commandLabel = alternativeLabel; + } // Show the important bit of the command, highlight this part if required sb.append(ListUtils.implode(parentCommand, (highlight ? ChatColor.YELLOW + "" + ChatColor.BOLD : "") + commandLabel, " ")); diff --git a/src/main/java/fr/xephi/authme/util/ListUtils.java b/src/main/java/fr/xephi/authme/util/ListUtils.java index 05ede1ba1..69ed092ef 100644 --- a/src/main/java/fr/xephi/authme/util/ListUtils.java +++ b/src/main/java/fr/xephi/authme/util/ListUtils.java @@ -37,25 +37,6 @@ public class ListUtils { return sb.toString(); } - /** - * Implode two lists of elements into a single string, with a specified separator. - * - * @param elements The first list of elements to implode. - * @param otherElements The second list of elements to implode. - * @param separator The separator to use. - * - - * @return The result string. */ - public static String implode(List elements, List otherElements, String separator) { - // Combine the lists - List combined = new ArrayList<>(); - combined.addAll(elements); - combined.addAll(otherElements); - - // Implode and return the result - return implode(combined, separator); - } - /** * Implode two elements into a single string, with a specified separator. * diff --git a/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java b/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java new file mode 100644 index 000000000..3fba70254 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java @@ -0,0 +1,152 @@ +package fr.xephi.authme.command.help; + +import fr.xephi.authme.command.CommandArgumentDescription; +import fr.xephi.authme.command.CommandDescription; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.executable.authme.AuthMeCommand; +import fr.xephi.authme.command.executable.authme.RegisterCommand; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +import static java.util.Collections.singletonList; +import static org.bukkit.ChatColor.BOLD; +import static org.bukkit.ChatColor.ITALIC; +import static org.bukkit.ChatColor.WHITE; +import static org.bukkit.ChatColor.YELLOW; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Test for {@link HelpSyntaxHelper}. + */ +public class HelpSyntaxHelperTest { + + @Test + public void shouldFormatSimpleCommand() { + // given + CommandDescription description = getDescription(); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), "", false); + + // then + assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]")); + } + + @Test + public void shouldFormatSimpleCommandWithOptionalParam() { + // given + CommandDescription description = getDescription(); + description.setArguments(singletonList( + new CommandArgumentDescription("test", "", false))); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), null, false); + + // then + assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " ")); + } + + @Test + public void shouldFormatCommandWithMultipleParams() { + // given + CommandDescription description = getDescription(); + description.setArguments(Arrays.asList( + new CommandArgumentDescription("name", "", true), + new CommandArgumentDescription("test", "", false))); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), "", false); + + // then + assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " ")); + } + + @Test + public void shouldHighlightCommandWithMultipleParams() { + // given + CommandDescription description = getDescription(); + description.setArguments(Arrays.asList( + new CommandArgumentDescription("name", "", true), + new CommandArgumentDescription("test", "", false))); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), "", true); + + // then + assertThat(result, equalTo(WHITE + "/authme " + + YELLOW + BOLD + "register" + + YELLOW + ITALIC + " [name]" + ITALIC + " ")); + } + + @Test + public void shouldHighlightCommandWithNoParams() { + // given + CommandDescription description = getDescription(); + description.setArguments(new ArrayList()); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), null, true); + + // then + assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW)); + } + + @Test + public void shouldFormatSimpleCommandWithAlternativeLabel() { + // given + CommandDescription description = getDescription(); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), "alt", false); + + // then + assertThat(result, equalTo(WHITE + "/authme alt" + ITALIC + " [name]")); + } + + @Test + public void shouldHighlightCommandWithAltLabelAndUnlimitedArguments() { + // given + CommandDescription description = getDescription(); + description.setArguments(Arrays.asList( + new CommandArgumentDescription("name", "", true), + new CommandArgumentDescription("test", "", false))); + description.setMaximumArguments(false); + + // when + String result = HelpSyntaxHelper.getCommandSyntax( + description, new CommandParts(), "test", true); + + // then + assertThat(result, equalTo(WHITE + "/authme " + + YELLOW + BOLD + "test" + + YELLOW + ITALIC + " [name]" + ITALIC + " " + ITALIC + " ...")); + } + + + private static CommandDescription getDescription() { + CommandDescription base = new CommandDescription(new AuthMeCommand(), + singletonList("authme"), + "Base command", + "AuthMe base command", + null); + CommandArgumentDescription userArg = new CommandArgumentDescription( + "name", "", true); + + return new CommandDescription( + new RegisterCommand(), + Arrays.asList("register", "r"), + "Register a player", + "Register the specified player with the specified password.", + base, + singletonList(userArg)); + } +}