LineParser { public ParsedLine parseLine(String text) { return parseLine(text, -1); }  LineParser input(String text); LineParser cursor(int cursor); LineParser parseBrackets(boolean doParse); LineParser operators(EnumSet<OperatorType> operators); ParsedLine parse(); List<ParsedLine> parseWithOperators(); ParsedLine parseLine(String text); ParsedLine parseLine(String text, int cursor); ParsedLine parseLine(String text, int cursor, boolean parseCurlyAndSquareBrackets); List<ParsedLine> parseLine(String text, int cursor, boolean parseCurlyAndSquareBrackets, Set<OperatorType> operators);  }
@Test public void testfindCurrentWordFromCursor() { LineParser lineParser = new LineParser(); assertEquals("", lineParser.parseLine(" ", 1).selectedWord().word()); assertEquals("foo", lineParser.parseLine("foo bar", 3).selectedWord().word()); assertEquals("bar", lineParser.parseLine("foo bar", 6).selectedWord().word()); assertEquals("foobar", lineParser.parseLine("foobar", 6).selectedWord().word()); assertEquals("fo", lineParser.parseLine("foobar", 2).selectedWordToCursor().word()); assertEquals("", lineParser.parseLine("ls ", 3).selectedWord().word()); assertEquals("foo", lineParser.parseLine("ls foo", 6).selectedWord().word()); assertEquals("foo", lineParser.parseLine("ls foo bar", 6).selectedWord().word()); assertEquals("bar", lineParser.parseLine("ls foo bar", 11).selectedWordToCursor().word()); assertEquals("ba", lineParser.parseLine("ls foo bar", 10).selectedWordToCursor().word()); assertEquals("b", lineParser.parseLine("ls foo bar", 9).selectedWordToCursor().word()); assertEquals("foo", lineParser.parseLine("ls foo ", 6).selectedWordToCursor().word()); assertEquals("o", lineParser.parseLine("ls o org/jboss/aeshell/Shell.class", 4).selectedWord().word()); assertEquals("", lineParser.parseLine("ls org/jboss/aeshell/Shell.class", 3).selectedWord().word()); }
@Test public void testFindCurrentWordWithEscapedSpaceToCursor() { LineParser lineParser = new LineParser(); assertEquals("foo bar", lineParser.parseLine("foo\\ bar", 8).selectedWordToCursor().word()); assertEquals("foo ba", lineParser.parseLine("foo\\ bar", 7).selectedWordToCursor().word()); assertEquals("foo bar", lineParser.parseLine("ls foo\\ bar", 12).selectedWordToCursor().word()); }
@Test public void testFindClosestWholeWordToCursor() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("ls foo bar", 6); assertEquals("foo", line.selectedWord().word()); assertFalse(line.isCursorAtEndOfSelectedWord()); assertEquals("", lineParser.parseLine(" ", 1).selectedWord().word()); line = lineParser.parseLine("foo bar", 1); assertEquals("foo", line.selectedWord().word()); assertFalse(line.isCursorAtEndOfSelectedWord()); line = lineParser.parseLine("foo bar", 3); assertEquals("foo", line.selectedWord().word()); assertTrue(line.isCursorAtEndOfSelectedWord()); assertEquals("foobar", lineParser.parseLine("foobar", 6).selectedWord().word()); assertEquals("foobar", lineParser.parseLine("foobar", 2).selectedWord().word()); assertEquals("", lineParser.parseLine("ls ", 3).selectedWord().word()); assertEquals("o", lineParser.parseLine("ls o org/jboss/aeshell/Shell.class", 4).selectedWord().word()); assertEquals("", lineParser.parseLine("ls org/jboss/aeshell/Shell.class", 3).selectedWord().word()); line = lineParser.parseLine("foo bar foo", 3); assertEquals("foo", line.selectedWord().word()); assertTrue(line.isCursorAtEndOfSelectedWord()); }
@Test public void testFindClosestWholeWordToCursorEscapedSpace() { LineParser lineParser = new LineParser(); assertEquals("foo bar", lineParser.parseLine("foo\\ bar", 7).selectedWord().word()); assertEquals("foo bar", lineParser.parseLine("ls foo\\ bar", 11).selectedWord().word()); }
@Test public void testOriginalInput() { LineParser lineParser = new LineParser(); String input = "echo foo -i bar"; ParsedLine line = lineParser.parseLine(input); assertEquals(input, line.line()); }
@Test public void testFindAllQuotedWords() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("foo bar \"baz 12345\" ", 19); assertEquals("foo", line.words().get(0).word()); assertEquals(0, line.words().get(0).lineIndex()); assertEquals("bar", line.words().get(1).word()); assertEquals(4, line.words().get(1).lineIndex()); assertEquals("baz 12345", line.words().get(2).word()); assertEquals(9, line.words().get(2).lineIndex()); assertEquals("", line.selectedWord().word()); assertEquals(0, line.wordCursor()); assertFalse(line.cursorAtEnd()); line = lineParser.parseLine("java -cp \"foo/bar\" \"Example\"", 32); assertEquals("foo/bar", line.words().get(2).word()); assertEquals("Example", line.words().get(3).word()); assertTrue(line.cursorAtEnd()); line = lineParser.parseLine("'foo/bar/' Example\\ 1"); assertEquals("foo/bar/", line.words().get(0).word()); assertEquals("Example 1", line.words().get(1).word()); line = lineParser.parseLine("man -f='foo bar/' Example\\ 1 foo"); assertEquals("man", line.words().get(0).word()); assertEquals("-f=foo bar/", line.words().get(1).word()); assertEquals("Example 1", line.words().get(2).word()); assertEquals("foo", line.words().get(3).word()); line = lineParser.parseLine("man -f='foo/bar/ Example\\ 1"); assertEquals(ParserStatus.UNCLOSED_QUOTE, line.status()); line = lineParser.parseLine("man -f='foo/bar/' Example\\ 1\""); assertEquals(ParserStatus.UNCLOSED_QUOTE, line.status()); line = lineParser.parseLine("-s \'redirectUris=[\"http: assertEquals("-s", line.words().get(0).word()); assertEquals("redirectUris=[\"http: line = lineParser.parseLine("\"baz\\ 12345\""); assertEquals(line.words().toString(), "baz\\ 12345", line.words().get(0).word()); line = lineParser.parseLine("\"\\\"String with double quotes\\\"\""); assertEquals(line.words().toString(), 1, line.words().size()); assertEquals(line.words().toString(), "\\\"String with double quotes\\\"", line.words().get(0).word()); line = lineParser.parseLine("\"\\\"String with double quotes\"\\\""); assertEquals(line.words().toString(), 2, line.words().size()); assertEquals(line.words().toString(), "\\\"String with double quotes", line.words().get(0).word()); assertEquals(line.words().toString(), "\"", line.words().get(1).word()); line = lineParser.parseLine("'\\'String with single quotes\\''"); assertEquals(line.words().toString(), 1, line.words().size()); assertEquals(line.words().toString(), "\\'String with single quotes\\'", line.words().get(0).word()); line = lineParser.parseLine("'\\'String with single quotes'\\'"); assertEquals(line.words().toString(), 2, line.words().size()); assertEquals(line.words().toString(), "\\'String with single quotes", line.words().get(0).word()); assertEquals(line.words().toString(), "'", line.words().get(1).word()); }
@Test public void testFindAllTernaryQuotedWords() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("\"\" \"\""); assertEquals(" ", line.words().get(0).word()); line = lineParser.parseLine("\"\" foo bar \"\""); assertEquals(" foo bar ", line.words().get(0).word()); line = lineParser.parseLine("\"\" \"foo bar\" \"\""); assertEquals(" \"foo bar\" ", line.words().get(0).word()); line = lineParser.parseLine("gah bah-bah \"\" \"foo bar\" \"\" boo"); assertEquals("gah", line.words().get(0).word()); assertEquals("bah-bah", line.words().get(1).word()); assertEquals(" \"foo bar\" ", line.words().get(2).word()); assertEquals("boo", line.words().get(3).word()); line = lineParser.parseLine(" \"\"/s-ramp/wsdl/Operation[xp2:matches(@name, 'submit.*')]\"\""); assertEquals("/s-ramp/wsdl/Operation[xp2:matches(@name, 'submit.*')]", line.words().get(0).word()); line = lineParser.parseLine(" \"\"/s-ramp/ext/${type} \\ \"\""); assertEquals("/s-ramp/ext/${type} \\ ", line.words().get(0).word()); line = lineParser.parseLine(" 'test=\"some thing\"' "); assertEquals("test=\"some thing\"", line.words().get(0).word()); }
@Test public void testParsedLineIterator() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("foo bar"); ParsedLineIterator iterator = line.iterator(); int counter = 0; while(iterator.hasNextWord()) { if(counter == 0) assertEquals("foo", iterator.pollWord()); else if(counter == 1) assertEquals("bar", iterator.pollWord()); counter++; } line = lineParser.parseLine(""); iterator = line.iterator(); assertFalse(iterator.hasNextWord()); assertNull(iterator.pollWord()); line = lineParser.parseLine("\\ foo ba bar"); iterator = line.iterator(); assertEquals(" foo", iterator.peekWord()); assertEquals(" foo", iterator.pollWord()); assertFalse(iterator.finished()); assertEquals('b', iterator.pollChar()); assertEquals('a', iterator.pollChar()); assertEquals(' ', iterator.pollChar()); assertFalse(iterator.finished()); assertEquals("bar", iterator.peekWord()); assertEquals("bar", iterator.pollWord()); assertTrue(iterator.finished()); line = lineParser.parseLine("\\ foo ba bar"); iterator = line.iterator(); assertEquals('\\', iterator.pollChar()); assertEquals(' ', iterator.pollChar()); assertEquals('f', iterator.pollChar()); assertEquals(" foo", iterator.pollWord()); assertEquals("ba", iterator.pollWord()); assertEquals('b', iterator.pollChar()); assertEquals('a', iterator.pollChar()); assertEquals('r', iterator.pollChar()); assertTrue(iterator.finished()); line = lineParser.parseLine("\\ foo ba bar"); iterator = line.iterator(); assertEquals(" foo", iterator.pollWord()); }
@Test public void testParsedLineIterator2() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("foo bar"); ParsedLineIterator iterator = line.iterator(); assertEquals("foo bar", iterator.stringFromCurrentPosition()); iterator.updateIteratorPosition(3); assertEquals(' ', iterator.pollChar()); assertEquals("bar", iterator.stringFromCurrentPosition()); assertEquals("bar", iterator.pollWord()); line = lineParser.parseLine("command --opt1={ myProp1=99, myProp2=100} --opt2"); iterator = line.iterator(); assertEquals("command", iterator.pollWord()); assertEquals('-', iterator.peekChar()); assertEquals("--opt1={", iterator.peekWord()); iterator.updateIteratorPosition(33); assertEquals("--opt2", iterator.peekWord()); assertEquals(' ', iterator.peekChar()); line = lineParser.parseLine("--headers={t=x; t=y}"); iterator = line.iterator(); assertEquals("--headers={t=x;", iterator.pollWord()); assertEquals('t', iterator.peekChar()); iterator.updateIteratorPosition(3); assertEquals('}', iterator.peekChar()); assertEquals("t=y}", iterator.pollWord()); assertFalse(iterator.hasNextWord()); assertNull("", iterator.pollWord()); line = lineParser.parseLine("--headers={t=x; t=y}"); iterator = line.iterator(); iterator.pollParsedWord(); iterator.updateIteratorPosition(4); assertFalse(iterator.hasNextChar()); assertEquals('\u0000', iterator.pollChar()); assertNull("", iterator.pollWord()); line = lineParser.parseLine("--headers={t=x; t=y}"); iterator = line.iterator(); iterator.pollParsedWord(); iterator.updateIteratorPosition(40); assertFalse(iterator.hasNextChar()); assertEquals('\u0000', iterator.pollChar()); assertNull("", iterator.pollWord()); line = lineParser.parseLine("--headers={t=x; t=y}"); iterator = line.iterator(); iterator.updateIteratorPosition(20); assertNull(iterator.peekWord()); }
@Test public void testCurlyBrackets() { LineParser lineParser = new LineParser(); ParsedLine line = lineParser.parseLine("foo bar {baz 12345} ", 19, true); assertEquals("foo", line.words().get(0).word()); assertEquals(0, line.words().get(0).lineIndex()); assertEquals("bar", line.words().get(1).word()); assertEquals(4, line.words().get(1).lineIndex()); assertEquals("{baz 12345}", line.words().get(2).word()); assertEquals(8, line.words().get(2).lineIndex()); assertEquals("{baz 12345}", line.selectedWord().word()); assertEquals(11, line.wordCursor()); line = lineParser.parseLine("cmd1 --option1=bar{x1; x2}", 19, true); assertEquals("cmd1", line.words().get(0).word()); assertEquals("--option1=bar{x1; x2}", line.words().get(1).word()); line = lineParser.parseLine("cmd1 --option1=bar{x1; x2}", 19, false); assertEquals("--option1=bar{x1;", line.words().get(1).word()); line = lineParser.parseLine("cmd1 --option1=bar{x1; x2 ", 19, true); assertEquals("cmd1", line.words().get(0).word()); assertEquals("--option1=bar{x1; x2 ", line.words().get(1).word()); assertEquals(ParsedWord.Status.OPEN_BRACKET, line.words().get(1).status()); }
@Test public void testParseEscapedCharacters() { assertEquals("mkdir", parseLine("mkdir He\\|lo").get(0).word()); assertEquals("Try to escape |", "He|lo", parseLine("mkdir He\\|lo").get(1).word()); assertEquals("Try to escape ;", "He;lo", parseLine("mkdir He\\;lo").get(1).word()); assertEquals("Try to escape \\","He\\lo", parseLine("mkdir He\\\\lo").get(1).word()); assertEquals("Try to escape normal char","He\\lo", parseLine("mkdir He\\lo").get(1).word()); assertEquals("Try to escape normal char","He\\-o", parseLine("mkdir He\\-o").get(1).word()); }