#963 Create tool task to generate plugin.yml

- Create task that generates commands/permissions section of plugin.yml
- Change CommandInitializer to return a List instead of Set (preserve insertion order)
- Merge CommandSyntaxHelper into CommandUtils
This commit is contained in:
ljacqu
2016-10-23 12:17:37 +02:00
parent 1867617dbb
commit d09964f1cb
18 changed files with 527 additions and 337 deletions
@@ -18,6 +18,7 @@ import java.util.regex.Pattern;
import static fr.xephi.authme.permission.DefaultPermission.OP_ONLY;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -32,7 +33,7 @@ public class CommandInitializerTest {
*/
private static int MAX_ALLOWED_DEPTH = 1;
private static Set<CommandDescription> commands;
private static Collection<CommandDescription> commands;
@BeforeClass
public static void initializeCommandCollection() {
@@ -46,7 +47,7 @@ public class CommandInitializerTest {
// It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes"
assertThat(commands.size(), equalTo(8));
assertThat(commands, hasSize(8));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
@@ -39,7 +39,7 @@ import static org.mockito.Mockito.mock;
@RunWith(DelayedInjectionRunner.class)
public class CommandMapperTest {
private static Set<CommandDescription> commands;
private static List<CommandDescription> commands;
@InjectDelayed
private CommandMapper mapper;
@@ -1,8 +1,15 @@
package fr.xephi.authme.command;
import fr.xephi.authme.TestHelper;
import org.bukkit.ChatColor;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -11,6 +18,13 @@ import static org.junit.Assert.assertThat;
*/
public class CommandUtilsTest {
private static Collection<CommandDescription> commands;
@BeforeClass
public static void setUpTestCommands() {
commands = TestCommandsUtil.generateCommands();
}
@Test
public void shouldReturnCommandPath() {
// given
@@ -79,6 +93,46 @@ public class CommandUtilsTest {
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandUtils.class);
}
@Test
public void shouldFormatSimpleArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme");
List<String> labels = Collections.singletonList("authme");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW));
}
@Test
public void shouldFormatCommandWithMultipleArguments() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme", "register");
List<String> labels = Arrays.asList("authme", "reg");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW + " reg <password> <confirmation>"));
}
@Test
public void shouldFormatCommandWithOptionalArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "email");
List<String> labels = Collections.singletonList("email");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/email" + ChatColor.YELLOW + " [player]"));
}
private static void checkArgumentCount(CommandDescription command, int expectedMin, int expectedMax) {
assertThat(CommandUtils.getMinNumberOfArguments(command), equalTo(expectedMin));
@@ -1,5 +1,6 @@
package fr.xephi.authme.command;
import com.google.common.collect.ImmutableList;
import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode;
@@ -8,9 +9,7 @@ import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
@@ -27,7 +26,7 @@ public final class TestCommandsUtil {
*
* @return The generated commands
*/
public static Set<CommandDescription> generateCommands() {
public static List<CommandDescription> generateCommands() {
// Register /authme
CommandDescription authMeBase = createCommand(null, null, singletonList("authme"), ExecutableCommand.class);
// Register /authme login <password>
@@ -48,7 +47,7 @@ public final class TestCommandsUtil {
CommandDescription unregisterBase = createCommand(AdminPermission.UNREGISTER, null,
asList("unregister", "unreg"), TestUnregisterCommand.class, newArgument("player", false));
return newHashSet(authMeBase, emailBase, unregisterBase);
return ImmutableList.of(authMeBase, emailBase, unregisterBase);
}
/**
@@ -1,76 +0,0 @@
package fr.xephi.authme.command.help;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.TestCommandsUtil;
import org.bukkit.ChatColor;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link CommandSyntaxHelper}.
*/
public class CommandSyntaxHelperTest {
private static Set<CommandDescription> commands;
@BeforeClass
public static void setUpTestCommands() {
commands = TestCommandsUtil.generateCommands();
}
@Test
public void shouldFormatSimpleArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme");
List<String> labels = Collections.singletonList("authme");
// when
String result = CommandSyntaxHelper.getSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW));
}
@Test
public void shouldFormatCommandWithMultipleArguments() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme", "register");
List<String> labels = Arrays.asList("authme", "reg");
// when
String result = CommandSyntaxHelper.getSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW + " reg <password> <confirmation>"));
}
@Test
public void shouldFormatCommandWithOptionalArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "email");
List<String> labels = Collections.singletonList("email");
// when
String result = CommandSyntaxHelper.getSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/email" + ChatColor.YELLOW + " [player]"));
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandSyntaxHelper.class);
}
}
@@ -9,8 +9,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
@@ -78,7 +78,7 @@ public class HelpMessagesConsistencyTest {
* @return the CommandDescription object for the {@code /authme register} command.
*/
private static CommandDescription getAuthMeRegisterDescription() {
Set<CommandDescription> commands = new CommandInitializer().getCommands();
Collection<CommandDescription> commands = new CommandInitializer().getCommands();
List<CommandDescription> children = commands.stream()
.filter(command -> command.getLabels().contains("authme"))
@@ -12,7 +12,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.Set;
import java.util.Collection;
import java.util.function.Function;
import static fr.xephi.authme.TestHelper.getJarFile;
@@ -31,7 +31,7 @@ import static org.mockito.Matchers.any;
public class HelpMessagesServiceTest {
private static final String TEST_FILE = "/fr/xephi/authme/command/help/help_test.yml";
private static final Set<CommandDescription> COMMANDS = TestCommandsUtil.generateCommands();
private static final Collection<CommandDescription> COMMANDS = TestCommandsUtil.generateCommands();
@InjectDelayed
private HelpMessagesService helpMessagesService;
@@ -20,9 +20,9 @@ import org.mockito.Mock;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
@@ -53,7 +53,7 @@ import static org.mockito.Mockito.verify;
@RunWith(DelayedInjectionRunner.class)
public class HelpProviderTest {
private static Set<CommandDescription> commands;
private static Collection<CommandDescription> commands;
@InjectDelayed
private HelpProvider helpProvider;