Search in sources :

Example 16 with CommandSpec

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

the class CommandSpecTest method testNoArgsFunctional.

@Test
public void testNoArgsFunctional() throws CommandException, NoSuchFieldException, IllegalAccessException {
    CommandManager cm = mock(CommandManager.class);
    TestHooks.setInstance("commandManager", cm);
    CommandSpec cmd = CommandSpec.builder().executor((src, args) -> CommandResult.empty()).build();
    final SimpleDispatcher dispatcher = new SimpleDispatcher();
    dispatcher.register(cmd, "cmd");
    dispatcher.process(Mockito.mock(CommandSource.class), "cmd");
}
Also used : Mockito(org.mockito.Mockito) Rule(org.junit.Rule) CommandContext(org.spongepowered.api.command.args.CommandContext) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) TestHooks(org.spongepowered.api.util.test.TestHooks) Test(org.junit.Test) ExpectedException(org.junit.rules.ExpectedException) Mockito.mock(org.mockito.Mockito.mock) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 17 with CommandSpec

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

the class GenericArgumentsTest method parseForInput.

private static CommandContext parseForInput(String input, CommandElement element) throws ArgumentParseException {
    CommandSpec spec = CommandSpec.builder().arguments(element).executor(NULL_EXECUTOR).build();
    final CommandArgs args = new CommandArgs(input, spec.getInputTokenizer().tokenize(input, false));
    final CommandContext context = new CommandContext();
    spec.populateContext(MOCK_SOURCE, args, context);
    return context;
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec)

Example 18 with CommandSpec

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

the class ChildCommandsTest method testSimpleChildCommand.

@Test
public void testSimpleChildCommand() throws CommandException {
    final AtomicBoolean childExecuted = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.of(ImmutableList.of("child"), CommandSpec.builder().executor((src, args) -> {
        childExecuted.set(true);
        return CommandResult.builder().successCount(1).build();
    }).build())).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "parent");
    execute.process(mock(CommandSource.class), "parent child");
    assertTrue(childExecuted.get());
}
Also used : 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 19 with CommandSpec

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

the class ChildCommandsTest method testEmptyChildrenWorksWithOptionalArgument.

@Test
public void testEmptyChildrenWorksWithOptionalArgument() throws CommandException {
    final AtomicBoolean parent = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("b")))).children(ImmutableMap.<List<String>, CommandSpec>builder().put(Lists.newArrayList("aaa"), CommandSpec.builder().executor((s, c) -> CommandResult.empty()).build()).build()).executor((s, c) -> {
        parent.set(true);
        return CommandResult.success();
    }).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "emptyparentwithopt");
    execute.process(mock(CommandSource.class), "emptyparentwithopt");
    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 20 with CommandSpec

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

the class ChildCommandsTest method testErrorOnNonExistentChildWithNoExecutor.

@Test
public void testErrorOnNonExistentChildWithNoExecutor() 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).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)

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