Create test for HelpSyntaxHelperTest

(cherry picked from commit 9a6e96d)
This commit is contained in:
ljacqu 2015-11-21 11:16:31 +01:00
parent e65319d42c
commit 115680a363
5 changed files with 164 additions and 25 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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)
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, " "));

View File

@ -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<String> elements, List<String> otherElements, String separator) {
// Combine the lists
List<String> 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.
*

View File

@ -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>"));
}
@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>"));
}
@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>"));
}
@Test
public void shouldHighlightCommandWithNoParams() {
// given
CommandDescription description = getDescription();
description.setArguments(new ArrayList<CommandArgumentDescription>());
// 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 + " <test>" + 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));
}
}