public CommandArgs parse(String arguments) throws CommandException { CommandContext context = new CommandContext(CommandContext.split("_ " + arguments), Collections.<Character>emptySet(), false, null, false); List<String> args = Lists.newArrayList(); for (int i = 0; i < context.argsLength(); i++) { args.add(context.getString(i)); } if (isUsingHangingArguments()) { if (arguments.isEmpty() || arguments.endsWith(" ")) { args.add(""); } } return new CommandArgs(args); } }
@Test(expected = CommandException.class) public void testInvalidFlags() throws CommandException { final String failingCommand = "herpderp -opw testers"; new CommandContext(failingCommand, new HashSet<>(Arrays.asList('o', 'w'))); }
@Test public void testOnlyQuotedString() { String cmd = "r \"hello goodbye have fun\""; String cmd2 = "r 'hellogeedby' nnnnnee"; try { new CommandContext(cmd); new CommandContext(cmd2); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
@Before public void setUpTest() { try { firstCommand = new CommandContext(firstCmdString, new HashSet<>(Arrays.asList('o', 'w'))); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Unexpected exception when creating CommandContext"); } }
@Test public void testUnmatchedQuote() { String cmd = "r \"hello goodbye have fun"; try { new CommandContext(cmd); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
@Test public void testMultipleSpaces() { String cmd = "r hi self"; try { new CommandContext(cmd); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
@Test public void testFlagsAnywhere() { try { CommandContext context = new CommandContext("r hello -f"); assertTrue(context.hasFlag('f')); CommandContext context2 = new CommandContext("r hello -f world"); assertTrue(context2.hasFlag('f')); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
@Test public void testSlice() { try { CommandContext context = new CommandContext("foo bar baz"); assertArrayEquals(new String[] { "foo", "bar", "baz" }, context.getSlice(0)); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
@Test public void testEmptyQuote() { try { CommandContext context = new CommandContext("region flag xmas blocked-cmds \"\""); assertEquals(context.argsLength(), 3); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } } }
CommandContext context = new CommandContext(newArgs, valueFlags);
@Test public void testExactJoinedStrings() { try { CommandContext context = new CommandContext("r -f \"hello world\" foo bar"); assertTrue(context.hasFlag('f')); assertEquals("\"hello world\" foo bar", context.getJoinedStrings(0)); assertEquals("foo bar", context.getJoinedStrings(1)); CommandContext context2 = new CommandContext("pm name \"hello world\" foo bar"); assertEquals("\"hello world\" foo bar", context2.getJoinedStrings(1)); } catch (CommandException e) { log.log(Level.WARNING, "Error", e); fail("Error creating CommandContext"); } }
CommandContext context = new CommandContext(split, getValueFlags(), false, locals);
CommandContext context = new CommandContext(newArgs, valueFlags, completing);