GrEclipseFormatterStepImpl { public String format(String raw) throws Exception { IDocument doc = new Document(raw); GroovyErrorListener errorListener = new GroovyErrorListener(); TextSelection selectAll = new TextSelection(doc, 0, doc.getLength()); GroovyFormatter codeFormatter = new DefaultGroovyFormatter(selectAll, doc, preferencesStore, false); TextEdit edit = codeFormatter.format(); if (!ignoreFormatterProblems && errorListener.errorsDetected()) { throw new IllegalArgumentException(errorListener.toString()); } edit.apply(doc); return doc.get(); } GrEclipseFormatterStepImpl(final Properties properties); String format(String raw); static final String IGNORE_FORMATTER_PROBLEMS; }
@Test public void defaultFormat() throws Throwable { String output = format(TEST_DATA.input("nominal.test"), config -> {}); assertEquals("Unexpected default formatting.", TEST_DATA.expected("nominal.test"), output); }
@Test public void validConfiguration() throws Throwable { String output = format(TEST_DATA.input("nominal.test"), config -> { config.put(GROOVY_FORMATTER_REMOVE_UNNECESSARY_SEMICOLONS, "true"); }); assertEquals("Unexpected formatting for custom configuration.", TEST_DATA.expected("nominal.test").replace(";", ""), output); }
@Test public void invalidConfiguration() throws Throwable { String output = format(TEST_DATA.input("nominal.test"), config -> { config.put(GROOVY_FORMATTER_INDENTATION, JavaCore.SPACE); config.put(GROOVY_FORMATTER_INDENTATION_SIZE, "noInteger"); }); assertEquals("Groovy formatter does not replace invalid preferences by their defaults.", TEST_DATA.expected("nominal.test").replace("\t", " "), output); }
@Test(expected = IllegalArgumentException.class) public void parserException() throws Throwable { format(PARSER_EXCEPTION, config -> {}); }
@Test(expected = IllegalArgumentException.class) public void scannerException() throws Throwable { format(SCANNER_EXCEPTION, config -> {}); }
@Test public void boundedWildCards() throws Throwable { String output = format(BOUNDED_WILDCARDS_UNFORMATTED, config -> {}); assertEquals("Unexpected formatting after bounded wildcards.", BOUNDED_WILDCARDS_FORMATTED, output); }
@Test public void ignoreCompilerProblems() throws Throwable { Consumer<Properties> ignoreCompilerProblems = config -> { config.setProperty(IGNORE_FORMATTER_PROBLEMS, "true"); }; format(PARSER_EXCEPTION, ignoreCompilerProblems); format(SCANNER_EXCEPTION, ignoreCompilerProblems); }