Search in sources :

Example 21 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testSimpleChildCommandIsSuppressedOnError.

@Test
public void testSimpleChildCommandIsSuppressedOnError() throws CommandException {
    final AtomicBoolean parentExecuted = new AtomicBoolean();
    final AtomicBoolean childExecuted = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().arguments(GenericArguments.literal(Text.of("test"), "test")).executor((src, args) -> {
        childExecuted.set(true);
        return CommandResult.builder().successCount(1).build();
    }).build())).arguments(GenericArguments.literal(Text.of("t"), "child")).executor((src, args) -> {
        parentExecuted.set(true);
        return CommandResult.success();
    }).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "parent");
    execute.process(mock(CommandSource.class), "parent child");
    assertFalse(childExecuted.get());
    assertTrue(parentExecuted.get());
}
Also used : SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Game(org.spongepowered.api.Game) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 22 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testErrorOnNonExistentChildWithNoOtherParameters.

@Test
public void testErrorOnNonExistentChildWithNoOtherParameters() throws CommandException {
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().executor((src, args) -> CommandResult.builder().successCount(1).build()).build())).childArgumentParseExceptionFallback(false).executor((src, args) -> CommandResult.success()).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "parent");
    try {
        execute.process(mock(CommandSource.class), "parent wrong");
    } catch (ArgumentParseException ex) {
        assertEquals("Input command wrong was not a valid subcommand!\nwrong\n^", ex.getMessage());
    }
}
Also used : SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Game(org.spongepowered.api.Game) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 23 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testEmptyChildrenWorksWithArgument.

@Test
public void testEmptyChildrenWorksWithArgument() throws CommandException {
    final AtomicBoolean parent = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("a")))).children(ImmutableMap.<List<String>, CommandSpec>of()).executor((s, c) -> {
        parent.set(true);
        return CommandResult.success();
    }).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "emptyparentwith");
    execute.process(mock(CommandSource.class), "emptyparentwith child");
    assertTrue(parent.get());
}
Also used : SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) ImmutableMap(com.google.common.collect.ImmutableMap) Game(org.spongepowered.api.Game) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 24 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class CommandFlagsTest method testFlaggedCommand.

@Test
public void testFlaggedCommand() throws CommandException {
    CommandSpec command = CommandSpec.builder().arguments(flags().flag("a").valueFlag(integer(t("quot")), "q").buildWith(string(t("key")))).executor((src, args) -> {
        assertEquals(true, args.getOne("a").get());
        assertEquals(42, args.getOne("quot").get());
        assertEquals("something", args.getOne("key").get());
        return CommandResult.builder().successCount(3).build();
    }).build();
    process(command, "-a -q 42 something");
    process(command, "-aq 42 something");
    process(command, "-a something -q 42");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) InputTokenizer(org.spongepowered.api.command.args.parsing.InputTokenizer) CommandSource(org.spongepowered.api.command.CommandSource) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) GenericArguments.none(org.spongepowered.api.command.args.GenericArguments.none) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Mockito(org.mockito.Mockito) GenericArguments.integer(org.spongepowered.api.command.args.GenericArguments.integer) SpongeApiTranslationHelper.t(org.spongepowered.api.util.SpongeApiTranslationHelper.t) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) GenericArguments.flags(org.spongepowered.api.command.args.GenericArguments.flags) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) TestPlainTextSerializer(org.spongepowered.api.text.TestPlainTextSerializer) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) Test(org.junit.Test)

Example 25 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandElementExecutor method parse.

@Override
public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException {
    if (this.fallbackExecutor != null && !args.hasNext()) {
        if (this.fallbackElements != null) {
            // there might be optionals to take account of that would parse this successfully.
            this.fallbackElements.parse(source, args, context);
        }
        // execute the fallback regardless in this scenario.
        return;
    }
    Object state = args.getState();
    final String key = args.next();
    Optional<CommandMapping> optionalCommandMapping = this.dispatcher.get(key, source);
    if (optionalCommandMapping.isPresent()) {
        final CommandMapping mapping = optionalCommandMapping.get();
        try {
            if ((mapping.getCallable() instanceof CommandSpec)) {
                CommandSpec spec = ((CommandSpec) mapping.getCallable());
                spec.populateContext(source, args, context);
            } else {
                if (args.hasNext()) {
                    args.next();
                }
                context.putArg(getUntranslatedKey() + "_args", args.getRaw().substring(args.getRawPosition()));
                while (args.hasNext()) {
                    args.next();
                }
            }
            // Success, add to context now so that we don't execute the wrong executor in the first place.
            context.putArg(getUntranslatedKey(), mapping);
        } catch (ArgumentParseException ex) {
            // If we get here, fallback to the elements, if they exist.
            args.setState(state);
            if (this.fallbackOnFail && this.fallbackElements != null) {
                this.fallbackElements.parse(source, args, context);
                return;
            }
            // Get the usage
            args.next();
            if (ex instanceof ArgumentParseException.WithUsage) {
                // This indicates a previous child failed, so we just prepend our child
                throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", ((ArgumentParseException.WithUsage) ex).getUsage()));
            }
            throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", mapping.getCallable().getUsage(source)));
        }
    } else {
        // Not a child, so let's continue with the fallback.
        if (this.fallbackExecutor != null && this.fallbackElements != null) {
            args.setState(state);
            this.fallbackElements.parse(source, args, context);
        } else {
            // so specifying it implicitly means we have a child command to execute.
            throw args.createError(t("Input command %s was not a valid subcommand!", key));
        }
    }
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandMapping(org.spongepowered.api.command.CommandMapping)

Aggregations

CommandSpec (org.spongepowered.api.command.spec.CommandSpec)28 Text (org.spongepowered.api.text.Text)18 CommandContext (org.spongepowered.api.command.args.CommandContext)14 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)14 List (java.util.List)12 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)12 GenericArguments (org.spongepowered.api.command.args.GenericArguments)12 ImmutableList (com.google.common.collect.ImmutableList)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CommandResult (org.spongepowered.api.command.CommandResult)10 Lists (com.google.common.collect.Lists)9 Sponge (org.spongepowered.api.Sponge)9 CommandException (org.spongepowered.api.command.CommandException)9 Test (org.junit.Test)8 CommandSource (org.spongepowered.api.command.CommandSource)8 Player (org.spongepowered.api.entity.living.player.Player)8 SimpleDispatcher (org.spongepowered.api.command.dispatcher.SimpleDispatcher)7 TextColors (org.spongepowered.api.text.format.TextColors)7 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6