#293 Translatable help - show translated description of child commands

- Show translated command descriptions when available
- Fix bug where localized command is registered on the parent each time

Thanks to @Maxetto
This commit is contained in:
ljacqu
2016-10-18 19:47:57 +02:00
parent 3dab5cd70c
commit 1d11824367
9 changed files with 112 additions and 85 deletions
@@ -19,14 +19,14 @@ public class CommandUtilsTest {
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(ExecutableCommand.class)
.build();
.register();
CommandDescription command = CommandDescription.builder()
.parent(base)
.labels("help", "h", "?")
.description("Child")
.detailedDescription("Test child command.")
.executableCommand(ExecutableCommand.class)
.build();
.register();
// when
String commandPath = CommandUtils.constructCommandPath(command);
@@ -42,7 +42,7 @@ public class CommandUtilsTest {
@Test
public void shouldComputeMinAndMaxOnEmptyCommand() {
// given
CommandDescription command = getBuilderForArgsTest().build();
CommandDescription command = getBuilderForArgsTest().register();
// when / then
checkArgumentCount(command, 0, 0);
@@ -54,7 +54,7 @@ public class CommandUtilsTest {
CommandDescription command = getBuilderForArgsTest()
.withArgument("Test", "Arg description", false)
.withArgument("Test22", "Arg description 2", false)
.build();
.register();
// when / then
checkArgumentCount(command, 2, 2);
@@ -67,7 +67,7 @@ public class CommandUtilsTest {
.withArgument("arg1", "Arg description", false)
.withArgument("arg2", "Arg description 2", true)
.withArgument("arg3", "Arg description 3", true)
.build();
.register();
// when / then
checkArgumentCount(command, 1, 3);
@@ -42,7 +42,7 @@ public final class TestCommandsUtil {
newArgument("player", true));
// Register /email helptest -- use only to test for help command arguments special case
CommandDescription.builder().parent(emailBase).labels("helptest").executableCommand(HelpCommand.class)
.description("test").detailedDescription("Test.").withArgument("Query", "", false).build();
.description("test").detailedDescription("Test.").withArgument("Query", "", false).register();
// Register /unregister <player>, alias: /unreg
CommandDescription unregisterBase = createCommand(AdminPermission.UNREGISTER, null,
@@ -101,7 +101,7 @@ public final class TestCommandsUtil {
}
}
return command.build();
return command.register();
}
/** Shortcut command to initialize a new argument description. */
@@ -16,6 +16,7 @@ import java.util.Set;
import java.util.function.Function;
import static fr.xephi.authme.TestHelper.getJarFile;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.sameInstance;
@@ -48,7 +49,7 @@ public class HelpMessagesServiceTest {
@Test
public void shouldReturnLocalizedCommand() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "authme", "register");
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "register");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
@@ -68,7 +69,7 @@ public class HelpMessagesServiceTest {
@Test
public void shouldReturnLocalizedCommandWithDefaults() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "authme", "login");
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "login");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
@@ -84,7 +85,7 @@ public class HelpMessagesServiceTest {
@Test
public void shouldReturnSameCommandForNoLocalization() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "email");
CommandDescription command = getCommandWithLabel(COMMANDS, "email");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
@@ -96,7 +97,7 @@ public class HelpMessagesServiceTest {
@Test
public void shouldKeepChildrenInLocalCommand() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "authme");
CommandDescription command = getCommandWithLabel(COMMANDS, "authme");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
@@ -114,4 +115,28 @@ public class HelpMessagesServiceTest {
assertThat(helpMessagesService.getMessage(HelpMessage.RESULT), equalTo("res."));
assertThat(helpMessagesService.getMessage(HelpSection.ARGUMENTS), equalTo("arg."));
}
@Test
public void shouldGetLocalCommandDescription() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "register");
// when
String description = helpMessagesService.getDescription(command);
// then
assertThat(description, equalTo("Registration"));
}
@Test
public void shouldFallbackToDescriptionOnCommandObject() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "unregister");
// when
String description = helpMessagesService.getDescription(command);
// then
assertThat(description, equalTo(command.getDescription()));
}
}
@@ -251,6 +251,10 @@ public class HelpProviderTest {
// given
CommandDescription command = getCommandWithLabel(commands, "authme");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
given(helpMessagesService.getDescription(getCommandWithLabel(commands, "authme", "login")))
.willReturn("Command for login [localized]");
given(helpMessagesService.getDescription(getCommandWithLabel(commands, "authme", "register")))
.willReturn("Registration command [localized]");
// when
helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
@@ -258,9 +262,9 @@ public class HelpProviderTest {
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(1), containsString("Children:"));
assertThat(lines.get(2), containsString("/authme login: login cmd"));
assertThat(lines.get(3), containsString("/authme register: register cmd"));
assertThat(lines.get(1), equalTo("Children:"));
assertThat(lines.get(2), equalTo(" /authme login: Command for login [localized]"));
assertThat(lines.get(3), equalTo(" /authme register: Registration command [localized]"));
}
@Test