mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-15 12:10:21 +00:00
[AB-76] replaced usage templates for commands with a generated usage string, removed possibility to define the usage directly on the help
added Google guava dependency minor fixes in documentation fixed versions in main pom
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import dev.sheldan.abstracto.core.command.Command;
|
||||
import dev.sheldan.abstracto.core.command.condition.CommandCondition;
|
||||
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
|
||||
import dev.sheldan.abstracto.core.command.condition.ConditionalCommand;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer;
|
||||
@@ -27,6 +29,8 @@ import java.util.List;
|
||||
public class CommandServiceBean implements CommandService {
|
||||
|
||||
public static final String NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE = "no_feature_command_found_exception";
|
||||
private static final String[] MANDATORY_ENCLOSING = {"<", ">"};
|
||||
private static final String[] OPTIONAL_ENCLOSING = {"[", "]"};
|
||||
|
||||
@Autowired
|
||||
private ModuleManagementService moduleManagementService;
|
||||
@@ -91,6 +95,26 @@ public class CommandServiceBean implements CommandService {
|
||||
commandForServer.setRestricted(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateUsage(Command command) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
CommandConfiguration commandConfig = command.getConfiguration();
|
||||
builder.append(commandConfig.getName());
|
||||
if(!commandConfig.getParameters().isEmpty()) {
|
||||
builder.append(" ");
|
||||
}
|
||||
commandConfig.getParameters().forEach(parameter -> {
|
||||
String[] enclosing = parameter.isOptional() ? OPTIONAL_ENCLOSING : MANDATORY_ENCLOSING;
|
||||
builder.append(enclosing[0]);
|
||||
builder.append(parameter.getName());
|
||||
builder.append(enclosing[1]);
|
||||
if(!parameter.equals(Iterables.getLast(commandConfig.getParameters()))) {
|
||||
builder.append(" ");
|
||||
}
|
||||
});
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unRestrictCommand(ACommand aCommand, AServer server) {
|
||||
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, server);
|
||||
|
||||
@@ -94,6 +94,7 @@ public class Help implements Command {
|
||||
model.setAllowedRoles(roleService.getRolesFromGuild(aCommandInAServer.getAllowedRoles()));
|
||||
model.setRestricted(true);
|
||||
}
|
||||
model.setUsage(commandService.generateUsage(command));
|
||||
model.setCommand(command.getConfiguration());
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_command_details_response", model);
|
||||
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package dev.sheldan.abstracto.core.command.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.Command;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandServiceBeanTest {
|
||||
|
||||
private static final String COMMAND_NAME = "test";
|
||||
private static final String PARAMETER_1_NAME = "param1";
|
||||
private static final String PARAMETER_2_NAME = "param2";
|
||||
|
||||
@InjectMocks
|
||||
private CommandServiceBean commandServiceBean;
|
||||
|
||||
@Test
|
||||
public void testUsageWithoutParameters() {
|
||||
executeTest("test", commandWithConfig(getNoParameters()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsageWithOptionalParameter() {
|
||||
executeTest("test [param1]", commandWithConfig(getOptionalParameterConfig()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsageWithMandatoryParameter() {
|
||||
executeTest("test <param1>", commandWithConfig(getMandatoryParameterConfig()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsageWithMixedParameters() {
|
||||
executeTest("test <param1> [param2]", commandWithConfig(getMixedParameterConfig()));
|
||||
}
|
||||
|
||||
private void executeTest(String expectedUsage, Command commandToExecute) {
|
||||
String generatedUsage = commandServiceBean.generateUsage(commandToExecute);
|
||||
Assert.assertEquals(expectedUsage, generatedUsage);
|
||||
}
|
||||
|
||||
private Command commandWithConfig(CommandConfiguration commandConfiguration) {
|
||||
Command command = Mockito.mock(Command.class);
|
||||
when(command.getConfiguration()).thenReturn(commandConfiguration);
|
||||
return command;
|
||||
}
|
||||
|
||||
private CommandConfiguration getNoParameters() {
|
||||
return CommandConfiguration
|
||||
.builder()
|
||||
.name(COMMAND_NAME)
|
||||
.build();
|
||||
}
|
||||
|
||||
private CommandConfiguration getOptionalParameterConfig() {
|
||||
return CommandConfiguration
|
||||
.builder()
|
||||
.name(COMMAND_NAME)
|
||||
.parameters(Arrays.asList(getOptionalParameter(true)))
|
||||
.build();
|
||||
}
|
||||
|
||||
private CommandConfiguration getMandatoryParameterConfig() {
|
||||
return CommandConfiguration
|
||||
.builder()
|
||||
.name(COMMAND_NAME)
|
||||
.parameters(Arrays.asList(getOptionalParameter(false)))
|
||||
.build();
|
||||
}
|
||||
|
||||
private CommandConfiguration getMixedParameterConfig() {
|
||||
Parameter param1 = Parameter.builder().name(PARAMETER_1_NAME).optional(false).build();
|
||||
Parameter param2 = Parameter.builder().name(PARAMETER_2_NAME).optional(true).build();
|
||||
return CommandConfiguration
|
||||
.builder()
|
||||
.name(COMMAND_NAME)
|
||||
.parameters(Arrays.asList(param1, param2))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Parameter getOptionalParameter(boolean b) {
|
||||
return Parameter.builder().name(PARAMETER_1_NAME).optional(b).build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user