From 56fd654eed226bcc4c788d8b34d9e100e333ea20 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sat, 17 Sep 2011 21:10:39 -0700 Subject: [PATCH] Fixed bug with unclosed quotes --- .../util/commands/CommandContext.java | 2 +- .../util/commands/CommandContextTest.java | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java b/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java index d4b50bf44..dfc99f3c0 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java @@ -74,7 +74,7 @@ public class CommandContext { build.append(' ').append(arg2); } } - args = removePortionOfArray(args, i, endIndex, build.toString()); + if (endIndex < args.length) args = removePortionOfArray(args, i, endIndex, build.toString()); } } diff --git a/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java b/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java index 2dc4e19f7..226d82fea 100644 --- a/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java +++ b/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java @@ -11,7 +11,6 @@ import java.util.HashSet; public class CommandContextTest { final String firstCmdString = "herpderp -opw testers \"mani world\" 'another thing' because something"; CommandContext firstCommand; - CommandContext secondCommand; @Before public void setUpTest(){ @@ -19,7 +18,7 @@ public class CommandContextTest { firstCommand = new CommandContext(firstCmdString, new HashSet(Arrays.asList('o', 'w'))); } catch (CommandException e) { e.printStackTrace(); - fail(); + fail("Unexpected exception when creating CommandContext"); } } @@ -29,4 +28,48 @@ public class CommandContextTest { new CommandContext(failingCommand, new HashSet(Arrays.asList('o', 'w'))); } + @Test + public void testBasicArgs() { + String command = firstCommand.getCommand(); + String argOne = firstCommand.getString(0); + String joinedArg = firstCommand.getJoinedStrings(0); + assertEquals(command, "herpderp"); + assertEquals(argOne, "another thing"); + assertEquals(joinedArg, "another thing because something"); + } + + @Test + public void testFlags() { + assertTrue(firstCommand.hasFlag('p')); + assertTrue(firstCommand.hasFlag('o')); + assertTrue(firstCommand.hasFlag('w')); + assertEquals(firstCommand.getFlag('o'), "testers"); + assertEquals(firstCommand.getFlag('w'), "mani world"); + assertNull(firstCommand.getFlag('u')); + } + + @Test + public void testOnlyQuotedString() { + String cmd = "r \"hello goodbye have fun\""; + String cmd2 = "r 'hellogeedby' nnnnnee"; + try { + CommandContext context = new CommandContext(cmd); + CommandContext context2 = new CommandContext(cmd2); + } catch (CommandException e) { + e.printStackTrace(); + fail("Error creating CommandContext"); + } + } + + @Test + public void testHagingQuoted() { + String cmd = "r \"hello goodbye have fun"; + try { + CommandContext context = new CommandContext(cmd); + } catch (CommandException e) { + e.printStackTrace(); + fail("Error creating CommandContext"); + } + } + }