Formatter { public void formatSource(CharSource input, CharSink output) throws FormatterException, IOException { output.write(formatSource(input.read())); } Formatter();  Formatter(JavaFormatterOptions options); void formatSource(CharSource input, CharSink output); String formatSource(String input); String formatSourceAndFixImports(String input); String formatSource(String input, Collection<Range<Integer>> characterRanges); ImmutableList<Replacement> getFormatReplacements(
      String input, Collection<Range<Integer>> characterRanges); static RangeSet<Integer> lineRangesToCharRanges(
      String input, RangeSet<Integer> lineRanges); static final int MAX_LINE_LENGTH; }
@Test public void blankInClassBody() throws FormatterException { String input = "package test;\nclass T {\n\n}\n"; String output = new Formatter().formatSource(input); String expect = "package test;\n\nclass T {}\n"; assertThat(output).isEqualTo(expect); }
@Test public void blankInClassBodyNoTrailing() throws FormatterException { String input = "package test;\nclass T {\n\n}"; String output = new Formatter().formatSource(input); String expect = "package test;\n\nclass T {}\n"; assertThat(output).isEqualTo(expect); }
@Test public void docCommentTrailingBlank() throws FormatterException { String input = "class T {\n\n\nint x;\n}"; String output = new Formatter().formatSource(input); String expect = "class T {\n \n int x;\n}\n"; assertThat(output).isEqualTo(expect); }
@Test public void blockCommentInteriorTrailingBlank() throws FormatterException { String input = "class T {\n \n\nint x;\n}"; String output = new Formatter().formatSource(input); String expect = "class T {\n \n\n int x;\n}\n"; assertThat(output).isEqualTo(expect); }
@Test public void blockCommentTrailingBlank() throws FormatterException { String input = "class T {\n \n\nint x;\n}"; String output = new Formatter().formatSource(input); String expect = "class T {\n \n\n int x;\n}\n"; assertThat(output).isEqualTo(expect); }
@Test public void lineCommentTrailingBlank() throws FormatterException { String input = "class T {\n String output = new Formatter().formatSource(input); String expect = "class T {\n assertThat(output).isEqualTo(expect); }
@Test public void lineCommentTrailingThinSpace() throws FormatterException { String input = "class T {\n String output = new Formatter().formatSource(input); String expect = "class T {\n assertThat(output).isEqualTo(expect); }
@Test public void noBlankAfterLineCommentWithInteriorBlankLine() throws FormatterException { String input = "class T {\n String output = new Formatter().formatSource(input); String expect = "class T {\n assertThat(output).isEqualTo(expect); }
@Test public void badConstructor() throws FormatterException { String input = "class X { Y() {} }"; String output = new Formatter().formatSource(input); String expect = "class X {\n Y() {}\n}\n"; assertThat(output).isEqualTo(expect); }
@Test public void voidMethod() throws FormatterException { String input = "class X { void Y() {} }"; String output = new Formatter().formatSource(input); String expect = "class X {\n void Y() {}\n}\n"; assertThat(output).isEqualTo(expect); }
@Test public void importsNotReorderedByDefault() throws FormatterException { String input = "package com.google.example;\n" + UNORDERED_IMPORTS + "\npublic class ExampleTest {}\n"; String output = new Formatter().formatSource(input); String expect = "package com.google.example;\n\n" + UNORDERED_IMPORTS + "\n\npublic class ExampleTest {}\n"; assertThat(output).isEqualTo(expect); }
@Test public void testTrailingCommentWithoutTerminalNewline() throws Exception { assertThat(new Formatter().formatSource("")) .isEqualTo("\n"); }
@Test public void testEmptyArray() throws Exception { assertThat(new Formatter().formatSource("class T { int x[] = {,}; }")) .isEqualTo("class T {\n int x[] = {,};\n}\n"); }
@Test public void stringEscapeLength() throws Exception { assertThat(new Formatter().formatSource("class T {{ f(\"\\\"\"); }}")) .isEqualTo("class T {\n {\n f(\"\\\"\");\n }\n}\n"); }
@Test public void wrapLineComment() throws Exception { assertThat( new Formatter() .formatSource( "class T {\n" + " public static void main(String[] args) { + " unbroken sentence moving from topic to topic so that no-one had a" + " chance to interrupt;\n" + " }\n" + "}\n")) .isEqualTo( "class T {\n" + " public static void main(\n" + " String[]\n" + " args) { + " from topic to topic so that no-one\n" + " + " }\n" + "}\n"); }
@Test public void onlyWrapLineCommentOnWhitespace() throws Exception { assertThat( new Formatter() .formatSource( "class T {\n" + " public static void main(String[] args) { + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" + "_chance_to_interrupt;\n" + " }\n" + "}\n")) .isEqualTo( "class T {\n" + " public static void main(\n" + " String[]\n" + " args) { + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" + "_chance_to_interrupt;\n" + " }\n" + "}\n"); }
@Test public void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Exception { assertThat( new Formatter() .formatSource( "class T {\n" + " public static void main(String[] args) { + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" + "_chance_to_interrupt;\n" + " }\n" + "}\n")) .isEqualTo( "class T {\n" + " public static void main(\n" + " String[]\n" + " args) { + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" + "_chance_to_interrupt;\n" + " }\n" + "}\n"); }
@Test public void dontWrapMoeLineComments() throws Exception { assertThat( new Formatter() .formatSource( "class T {\n" + " + " unbroken sentence moving from topic to topic so that no-one had a" + " chance to interrupt;\n" + "}\n")) .isEqualTo( "class T {\n" + " + " unbroken sentence moving from topic to topic so that no-one had a" + " chance to interrupt;\n" + "}\n"); }
@Test public void removeTrailingTabsInComments() throws Exception { assertThat( new Formatter() .formatSource( "class Foo {\n" + " void f() {\n" + " int x = 0; + " return;\n" + " }\n" + "}\n")) .isEqualTo( "class Foo {\n" + " void f() {\n" + " int x = 0; + " return;\n" + " }\n" + "}\n"); }