Search in sources :

Example 1 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testSimpleChildCommandIsThrownOnErrorWhenSelected.

@Test
public void testSimpleChildCommandIsThrownOnErrorWhenSelected() 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();
    }).childArgumentParseExceptionFallback(false).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "parent");
    try {
        execute.process(mock(CommandSource.class), "parent child");
    } catch (ArgumentParseException ex) {
    // ignored - we check this with the booleans
    }
    assertFalse(childExecuted.get());
    assertFalse(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) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) SimpleDispatcher(org.spongepowered.api.command.dispatcher.SimpleDispatcher) Test(org.junit.Test)

Example 2 with SimpleDispatcher

use of org.spongepowered.api.command.dispatcher.SimpleDispatcher in project SpongeAPI by SpongePowered.

the class ChildCommandsTest method testEmptyChildrenWorks.

@Test
public void testEmptyChildrenWorks() throws CommandException {
    final AtomicBoolean parent = new AtomicBoolean();
    final CommandSpec spec = CommandSpec.builder().children(ImmutableMap.<List<String>, CommandSpec>of()).executor((s, c) -> {
        parent.set(true);
        return CommandResult.success();
    }).build();
    final SimpleDispatcher execute = new SimpleDispatcher();
    execute.register(spec, "emptyparent");
    execute.process(mock(CommandSource.class), "emptyparent");
    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 3 with SimpleDispatcher

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

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

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

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