SyntaxTools { public static String handleShortSyntax(String line, List<String> options) { final String prepend = "concourse."; if(line.equalsIgnoreCase("time") || line.equalsIgnoreCase("date")) { return line + " \"now\""; } else if(!line.contains("(")) { if(line.startsWith(prepend)) { boolean hasArgs = line.split("\\s+").length > 1; if(!hasArgs) { line += "()"; } return line; } else { String[] query = line.split("\\s+"); String cmd = query[0]; if(cmd.contains("_")) { String replacement = CaseFormat.LOWER_UNDERSCORE .to(CaseFormat.LOWER_CAMEL, cmd); line = line.replaceFirst(cmd, replacement); } String expanded = prepend + line.trim(); Pattern pattern = Pattern.compile(expanded.split("\\s|\\(")[0]); for (String option : options) { if(pattern.matcher(option).matches()) { boolean hasArgs = expanded.split("\\s+").length > 1; if(!hasArgs) { expanded += "()"; } return expanded; } } } } else { Set<String> shortInvokedMethods = parseShortInvokedMethods(line); for (String method : shortInvokedMethods) { if(options.contains(prepend + method)) { line = line.replaceAll("(?<!\\_)" + method + "\\(", prepend + method + "\\("); } } } return line; } private  SyntaxTools(); static String handleMissingArgCommas(String line,
            List<String> methods); static String handleShortSyntax(String line, List<String> options);  }
@Test public void testHandleShortSyntaxTimeWithNoArg() { Assert.assertEquals("time \"now\"", SyntaxTools.handleShortSyntax( "time", Lists.newArrayList(ConcourseShell.getAccessibleApiMethods()))); }
@Test public void testHandleShortSyntaxDateWithNoArg() { Assert.assertEquals("date \"now\"", SyntaxTools.handleShortSyntax( "date", Lists.newArrayList(ConcourseShell.getAccessibleApiMethods()))); }
@Test public void testHandleShortSyntaxTimeWithArg() { Assert.assertEquals("concourse.time \"yesterday\"", SyntaxTools.handleShortSyntax("time \"yesterday\"", Lists.newArrayList( ConcourseShell.getAccessibleApiMethods()))); }
@Test public void testHandleShortSyntaxDateWithArg() { Assert.assertEquals("date \"yesterday\"", SyntaxTools.handleShortSyntax( "date \"yesterday\"", Lists.newArrayList(ConcourseShell.getAccessibleApiMethods()))); }
@Test public void testHandleShortSyntaxSingleMethodNoParens() { String line = "add \"foo\", \"bar\", 1"; String expected = "concourse.add \"foo\", \"bar\", 1"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void testHandleShortSyntaxSingleMethodWithParens() { String line = "add(\"foo\", \"bar\", 1)"; String expected = "concourse.add(\"foo\", \"bar\", 1)"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void testHandleShortSyntaxNestedMethods() { String line = "get(describe(1), 1)"; String expected = "concourse.get(concourse.describe(1), 1)"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void testHandleShortSyntaxNestedMethodsSomeBlacklisted() { String line = "get(describe(1), 1, time(\"yesterday\"))"; String expected = "concourse.get(concourse.describe(1), 1, time(\"yesterday\"))"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void testHandleShortSyntaxNestedNestedMethods() { String line = "get(describe(1), find(\"name\", eq, \"foo\"))"; String expected = "concourse.get(concourse.describe(1), concourse.find(\"name\", eq, \"foo\"))"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void testHandleShortSyntaxMethodWithNoArgs() { String line = "getServerVersion"; String expected = "concourse.getServerVersion()"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }
@Test public void reproGH_127() { String line = "find_or_add('name', 'concourse'); callA(record); find_or_add('name', 'concourse'); callB(record); add('name', 'jeff', 2);"; String expected = "find_or_add('name', 'concourse'); callA(record); find_or_add('name', 'concourse'); callB(record); concourse.add('name', 'jeff', 2);"; Assert.assertEquals(expected, SyntaxTools.handleShortSyntax(line, options())); }