Search in sources :

Example 6 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher 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)

Example 7 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher 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 8 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher 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 9 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher 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)

Aggregations

Test (org.junit.Test)9 SimpleDispatcher (org.spongepowered.api.command.dispatcher.SimpleDispatcher)9 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Mockito.mock (org.mockito.Mockito.mock)8 CommandContext (org.spongepowered.api.command.args.CommandContext)8 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)8 TestHooks (org.spongepowered.api.util.test.TestHooks)8 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableMap (com.google.common.collect.ImmutableMap)7 Lists (com.google.common.collect.Lists)7 List (java.util.List)7 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertFalse (org.junit.Assert.assertFalse)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Before (org.junit.Before)7 Mockito.when (org.mockito.Mockito.when)7 Game (org.spongepowered.api.Game)7 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)7 GenericArguments (org.spongepowered.api.command.args.GenericArguments)7