@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()); }
@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()); }
@Test public void testExecutorRequired() { this.expected.expect(NullPointerException.class); this.expected.expectMessage("An executor is required"); CommandSpec.builder() .build(); } }
@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()); }
@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"); }
@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()); } }
@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()); }
@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()); }
@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()); } }
@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()); }
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; }
@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"); }
@Override public CommandSpec buildSpec() { return CommandSpec.builder() .executor(this) .permission(PomData.ARTIFACT_ID + ".command.skininfo.base") .build(); }
@Override public CommandSpec buildSpec(Settings settings) { return CommandSpec.builder() .executor(this) .arguments( onlyOne( player(of("account")) ) ) .build(); } }
@Override public CommandSpec buildSpec(Settings settings) { return CommandSpec.builder() .executor(this) .arguments(onlyOne( user(of("user"))) ) .build(); } }
@Override public CommandSpec buildSpec() { return CommandSpec.builder() .executor(this) .arguments( string(of("skin")), flags().flag("keep").buildWith(GenericArguments.none())) .permission(PomData.ARTIFACT_ID + ".command.setskin.base") .build(); } }
public static void setup(Object plugin) { Sponge.getCommandManager().register(plugin, CommandSpec.builder().arguments(GenericArguments.playerOrSource(Text.of("sniper")), GenericArguments.integer(Text.of("center"))) .executor(new VoxelCenterCommand()).permission(VoxelSniperConfiguration.PERMISSION_SNIPER) .description(Text.of("VoxelSniper Center point selection")).build(), "vc"); }
public static void setup(Object plugin) { Sponge.getCommandManager().register(plugin, CommandSpec.builder() .arguments(GenericArguments.playerOrSource(Text.of("sniper")), GenericArguments.optional(GenericArguments.integer(Text.of("count")))) .executor(new VoxelUndoCommand()).permission(VoxelSniperConfiguration.PERMISSION_SNIPER) .description(Text.of("VoxelSniper undo")).build(), "u"); }
public static void setup(Object plugin) { Sponge.getCommandManager().register(plugin, CommandSpec.builder() .arguments(GenericArguments.playerOrSource(Text.of("sniper")), GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("args")))) .executor(new VoxelSniperCommand()).permission(VoxelSniperConfiguration.PERMISSION_SNIPER) .description(Text.of("VoxelSniper material list selection")).build(), "vs"); }