2011-09-19 06:24:21 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-09-15 00:58:22 +00:00
|
|
|
package com.sk89q.minecraft.util.commands;
|
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
public class CommandContextTest {
|
2011-09-19 07:08:10 +00:00
|
|
|
private static final String firstCmdString = "herpderp -opw testers \"mani world\" 'another thing' because something";
|
2011-09-15 00:58:22 +00:00
|
|
|
CommandContext firstCommand;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUpTest(){
|
|
|
|
try {
|
|
|
|
firstCommand = new CommandContext(firstCmdString, new HashSet<Character>(Arrays.asList('o', 'w')));
|
|
|
|
} catch (CommandException e) {
|
|
|
|
e.printStackTrace();
|
2011-09-18 04:10:39 +00:00
|
|
|
fail("Unexpected exception when creating CommandContext");
|
2011-09-15 00:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = CommandException.class)
|
|
|
|
public void testInvalidFlags() throws CommandException {
|
2011-09-19 07:08:10 +00:00
|
|
|
final String failingCommand = "herpderp -opw testers";
|
2011-09-15 00:58:22 +00:00
|
|
|
new CommandContext(failingCommand, new HashSet<Character>(Arrays.asList('o', 'w')));
|
|
|
|
}
|
|
|
|
|
2011-09-18 04:10:39 +00:00
|
|
|
@Test
|
|
|
|
public void testBasicArgs() {
|
|
|
|
String command = firstCommand.getCommand();
|
|
|
|
String argOne = firstCommand.getString(0);
|
|
|
|
String joinedArg = firstCommand.getJoinedStrings(0);
|
2011-09-19 21:01:57 +00:00
|
|
|
assertEquals("herpderp", command);
|
|
|
|
assertEquals("another thing", argOne);
|
2011-09-20 00:35:15 +00:00
|
|
|
assertEquals("'another thing' because something", joinedArg);
|
2011-09-18 04:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testFlags() {
|
|
|
|
assertTrue(firstCommand.hasFlag('p'));
|
|
|
|
assertTrue(firstCommand.hasFlag('o'));
|
|
|
|
assertTrue(firstCommand.hasFlag('w'));
|
2011-09-19 21:01:57 +00:00
|
|
|
assertEquals("testers", firstCommand.getFlag('o'));
|
|
|
|
assertEquals("mani world", firstCommand.getFlag('w'));
|
2011-09-20 00:35:15 +00:00
|
|
|
assertFalse(firstCommand.hasFlag('u'));
|
2011-09-18 04:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOnlyQuotedString() {
|
|
|
|
String cmd = "r \"hello goodbye have fun\"";
|
|
|
|
String cmd2 = "r 'hellogeedby' nnnnnee";
|
|
|
|
try {
|
2011-09-19 21:01:57 +00:00
|
|
|
new CommandContext(cmd);
|
|
|
|
new CommandContext(cmd2);
|
2011-09-18 04:10:39 +00:00
|
|
|
} catch (CommandException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2011-09-20 01:41:07 +00:00
|
|
|
public void testUnmatchedQuote() {
|
2011-09-18 04:10:39 +00:00
|
|
|
String cmd = "r \"hello goodbye have fun";
|
|
|
|
try {
|
2011-09-19 21:01:57 +00:00
|
|
|
new CommandContext(cmd);
|
2011-09-18 04:10:39 +00:00
|
|
|
} catch (CommandException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-19 21:01:57 +00:00
|
|
|
@Test
|
|
|
|
public void testMultipleSpaces() {
|
|
|
|
String cmd = "r hi self";
|
|
|
|
try {
|
|
|
|
new CommandContext(cmd);
|
|
|
|
} catch (CommandException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2011-09-20 00:35:15 +00:00
|
|
|
public void testFlagsAnywhere() {
|
2011-09-19 21:01:57 +00:00
|
|
|
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) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
2011-09-20 00:35:15 +00:00
|
|
|
|
|
|
|
@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) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
2011-09-20 01:41:07 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSlice() {
|
|
|
|
try {
|
|
|
|
CommandContext context = new CommandContext("foo bar baz");
|
|
|
|
assertArrayEquals(new String[] { "foo", "bar", "baz" }, context.getSlice(0));
|
|
|
|
|
|
|
|
} catch (CommandException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Error creating CommandContext");
|
|
|
|
}
|
|
|
|
}
|
2011-09-15 00:58:22 +00:00
|
|
|
}
|