EclipseCdtFormatterStepImpl { public String format(String raw) throws Exception { TextEdit edit = codeFormatter.format(CodeFormatter.K_UNKNOWN, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER); if (edit == null) { throw new IllegalArgumentException("Invalid C/C++ syntax for formatting."); } else { IDocument doc = new Document(raw); edit.apply(doc); return doc.get(); } } EclipseCdtFormatterStepImpl(Properties settings); String format(String raw);  }
@Test public void defaultFormat() throws Throwable { String output = format(CPP_UNFORMATTED, config -> {}); assertEquals("Unexpected formatting with default preferences.", CPP_FORMATTED, output); }
@Test public void invalidFormat() throws Throwable { String output = format(CPP_FORMATTED.replace("int main() {", "int main() "), config -> {}); assertTrue("Incomplete CPP not formatted on best effort basis.", output.contains("int main()" + LINE_DELIMITER)); }
@Test public void invalidCharater() throws Throwable { String output = format(CPP_FORMATTED.replace("int main() {", "int main()" + ILLEGAL_CHAR + " {"), config -> {}); assertTrue("Invalid charater not formatted on best effort basis.", output.contains("int main()" + LINE_DELIMITER)); }
@Test public void invalidConfiguration() throws Throwable { String output = format(CPP_FORMATTED, config -> { config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "noInteger"); }); assertEquals("Invalid indentation configuration not replaced by default value (4 spaces)", CPP_FORMATTED.replace("\t", " "), output); }
@Test public void htmlCommentFormat() throws Throwable { String output = format(DOXYGEN_HTML + CPP_FORMATTED, config -> {}); assertEquals("HTML comments not ignored by formatter.", DOXYGEN_HTML + CPP_FORMATTED, output); }
@Test public void regionWarning() throws Throwable { String output = format(FUNCT_PTR_UNFORMATTED, config -> {}); assertEquals("Code not formatted at all due to regional error.", FUNCT_PTR_FORMATTED, output); }