#305 Adjust behavior of label handling; cleanup

- Fix bugs in behavior (wrong labels being shown for help)
- Change order of labels and arguments in FoundCommandResult constructors
- Move FoundResultStatus enum to its own class
- Create test class for HelpProvider
This commit is contained in:
ljacqu
2015-12-18 22:19:26 +01:00
parent d871939793
commit 01a294a334
9 changed files with 231 additions and 51 deletions
@@ -53,7 +53,7 @@ public class CommandHandlerTest {
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("login", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.SUCCESS));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getArguments(), contains("test1"));
assertThat(result.getDifference(), equalTo(0.0));
}
@@ -68,7 +68,7 @@ public class CommandHandlerTest {
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.SUCCESS));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getArguments(), contains("arg1", "arg2"));
assertThat(result.getDifference(), equalTo(0.0));
}
@@ -83,7 +83,21 @@ public class CommandHandlerTest {
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
public void shouldRejectCommandWithTooFewArguments() {
// given
List<String> parts = Arrays.asList("authme", "Reg");
// when
FoundCommandResult result = handler.mapPartsToCommand(parts);
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
}
@@ -97,7 +111,7 @@ public class CommandHandlerTest {
// then
assertThat(result.getCommandDescription(), equalTo(getChildWithLabel("register", "authme")));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.UNKNOWN_LABEL));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() < 0.75, equalTo(true));
}
@@ -112,7 +126,7 @@ public class CommandHandlerTest {
// then
assertThat(result.getCommandDescription(), not(nullValue()));
assertThat(result.getResultStatus(), equalTo(FoundCommandResult.ResultStatus.UNKNOWN_LABEL));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() > 0.75, equalTo(true));
}
@@ -8,6 +8,7 @@ import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test for {@link CommandUtils}.
@@ -37,4 +38,92 @@ public class CommandUtilsTest {
// then
assertThat(str, equalTo(""));
}
@Test
public void shouldPrintLabels() {
// given
List<String> labels = Arrays.asList("authme", "help", "reload");
// when
String result = CommandUtils.labelsToString(labels);
// then
assertThat(result, equalTo("authme help reload"));
}
@Test
public void shouldReturnCommandPath() {
// given
CommandDescription base = CommandDescription.builder()
.labels("authme", "auth")
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(mock(ExecutableCommand.class))
.build();
CommandDescription command = CommandDescription.builder()
.parent(base)
.labels("help", "h", "?")
.description("Child")
.detailedDescription("Test child command.")
.executableCommand(mock(ExecutableCommand.class))
.build();
// when
String commandPath = CommandUtils.constructCommandPath(command);
// then
assertThat(commandPath, equalTo("/authme help"));
}
// ------
// min / max arguments
// ------
@Test
public void shouldComputeMinAndMaxOnEmptyCommand() {
// given
CommandDescription command = getBuilderForArgsTest().build();
// when / then
checkArgumentCount(command, 0, 0);
}
@Test
public void shouldComputeMinAndMaxOnCommandWithMandatoryArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("Test", "Arg description", false)
.withArgument("Test22", "Arg description 2", false)
.build();
// when / then
checkArgumentCount(command, 2, 2);
}
@Test
public void shouldComputeMinAndMaxOnCommandIncludingOptionalArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("arg1", "Arg description", false)
.withArgument("arg2", "Arg description 2", true)
.withArgument("arg3", "Arg description 3", true)
.build();
// when / then
checkArgumentCount(command, 1, 3);
}
private static void checkArgumentCount(CommandDescription command, int expectedMin, int expectedMax) {
assertThat(CommandUtils.getMinNumberOfArguments(command), equalTo(expectedMin));
assertThat(CommandUtils.getMaxNumberOfArguments(command), equalTo(expectedMax));
}
private static CommandDescription.CommandBuilder getBuilderForArgsTest() {
return CommandDescription.builder()
.labels("authme", "auth")
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(mock(ExecutableCommand.class));
}
}
@@ -0,0 +1,65 @@
package fr.xephi.authme.command.help;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.ExecutableCommand;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test for {@link HelpProvider}.
*/
public class HelpProviderTest {
private static CommandDescription parent;
private static CommandDescription child;
@BeforeClass
public static void setUpCommands() {
parent = CommandDescription.builder()
.executableCommand(mock(ExecutableCommand.class))
.labels("base", "b")
.description("Parent")
.detailedDescription("Test base command.")
.build();
child = CommandDescription.builder()
.executableCommand(mock(ExecutableCommand.class))
.parent(parent)
.labels("child", "c")
.description("Child")
.detailedDescription("Child test command.")
.withArgument("Argument", "The argument", false)
.build();
}
@Test
public void shouldRetainCorrectLabels() {
// given
List<String> labels = Arrays.asList("b", "child");
// when
List<String> result = HelpProvider.filterCorrectLabels(child, labels);
// then
assertThat(result, contains("b", "child"));
}
@Test
public void shouldReplaceIncorrectLabels() {
// given
List<String> labels = Arrays.asList("base", "wrong");
// when
List<String> result = HelpProvider.filterCorrectLabels(child, labels);
// then
assertThat(result, contains("base", "child"));
}
}