diff --git a/core/src/test/java/com/google/googlejavaformat/java/DiagnosticTest.java b/core/src/test/java/com/google/googlejavaformat/java/DiagnosticTest.java index e05a37264..5f8c9cd26 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/DiagnosticTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/DiagnosticTest.java @@ -17,7 +17,6 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.common.base.Joiner; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -55,19 +54,19 @@ public void restoreLocale() throws Exception { @Test public void parseError() throws Exception { String input = - Joiner.on('\n') - .join( - "public class InvalidSyntax {", - " private static NumPrinter {", - " public static void print(int n) {", - " System.out.printf(\"%d%n\", n);", - " }", - " }", - "", - " public static void main(String[] args) {", - " NumPrinter.print(args.length);", - " }", - "}"); + """ + public class InvalidSyntax { + private static NumPrinter { + public static void print(int n) { + System.out.printf("%d%n", n); + } + } + + public static void main(String[] args) { + NumPrinter.print(args.length); + } + }\ + """; StringWriter stdout = new StringWriter(); StringWriter stderr = new StringWriter(); @@ -150,7 +149,11 @@ public void oneFileParseErrorReplace() throws Exception { @Test public void parseError2() throws FormatterException, IOException, UsageException { - String input = "class Foo { void f() {\n g() } }"; + String input = + """ + class Foo { void f() { + g() } } + """; Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("A.java"); @@ -169,7 +172,11 @@ public void parseError2() throws FormatterException, IOException, UsageException @Test public void parseErrorStdin() throws FormatterException, IOException, UsageException { - String input = "class Foo { void f() {\n g() } }"; + String input = + """ + class Foo { void f() { + g() } } + """; InputStream inStream = new ByteArrayInputStream(input.getBytes(UTF_8)); StringWriter out = new StringWriter(); @@ -184,7 +191,11 @@ public void parseErrorStdin() throws FormatterException, IOException, UsageExcep @Test public void lexError2() throws FormatterException, IOException, UsageException { - String input = "class Foo { void f() {\n g('foo'); } }"; + String input = + """ + class Foo { void f() { + g('foo'); } } + """; Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("A.java"); @@ -203,7 +214,11 @@ public void lexError2() throws FormatterException, IOException, UsageException { @Test public void lexErrorStdin() throws FormatterException, IOException, UsageException { - String input = "class Foo { void f() {\n g('foo'); } }"; + String input = + """ + class Foo { void f() { + g('foo'); } } + """; InputStream inStream = new ByteArrayInputStream(input.getBytes(UTF_8)); StringWriter out = new StringWriter(); StringWriter err = new StringWriter(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index 3835673d9..9ba460428 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -17,9 +17,8 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; -import com.google.common.base.Joiner; import com.google.common.io.CharStreams; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; import java.io.ByteArrayInputStream; @@ -49,17 +48,16 @@ public void testFormatAosp() throws Exception { "class A{void b(){while(true){weCanBeCertainThatThisWillEndUpGettingWrapped(" + "because, it, is, just, so, very, very, very, very, looong);}}}"; String expectedOutput = - Joiner.on("\n") - .join( - "class A {", - " void b() {", - " while (true) {", - " weCanBeCertainThatThisWillEndUpGettingWrapped(", - " because, it, is, just, so, very, very, very, very, looong);", - " }", - " }", - "}", - ""); + """ + class A { + void b() { + while (true) { + weCanBeCertainThatThisWillEndUpGettingWrapped( + because, it, is, just, so, very, very, very, very, looong); + } + } + } + """; Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("A.java"); @@ -82,7 +80,7 @@ public void testFormatNonJavaFiles() throws Exception { // should succeed because non-Java files are skipped assertThat(main.format("foo.go")).isEqualTo(0); - assertThat(err.toString()).contains("Skipping non-Java file: " + "foo.go"); + assertThat(err.toString()).contains("Skipping non-Java file: foo.go"); // format still fails on missing files assertThat(main.format("Foo.java")).isEqualTo(1); @@ -91,8 +89,20 @@ public void testFormatNonJavaFiles() throws Exception { @Test public void testFormatStdinStdoutWithDashFlag() throws Exception { - String input = "class Foo{\n" + "void f\n" + "() {\n" + "}\n" + "}\n"; - String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n"; + String input = + """ + class Foo{ + void f + () { + } + } + """; + String expectedOutput = + """ + class Foo { + void f() {} + } + """; InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8)); StringWriter out = new StringWriter(); @@ -110,8 +120,20 @@ public void testFormatStdinStdoutWithDashFlag() throws Exception { @Test public void testFormatLengthUpToEOF() throws Exception { - String input = "class Foo{\n" + "void f\n" + "() {\n" + "}\n" + "}\n\n\n\n\n\n"; - String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n"; + String input = + """ + class Foo{ + void f + () { + } + }\n\n\n\n\n + """; + String expectedOutput = + """ + class Foo { + void f() {} + } + """; Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("Foo.java"); @@ -164,49 +186,132 @@ public void testFormatOffsetOutOfRange() throws Exception { @Test public void blankInClassBody() throws FormatterException { - String input = "package test;\nclass T {\n\n}\n"; + String input = + """ + package test; + class T { + + } + """; String output = new Formatter().formatSource(input); - String expect = "package test;\n\nclass T {}\n"; + String expect = + """ + package test; + + class T {} + """; assertThat(output).isEqualTo(expect); } @Test public void blankInClassBodyNoTrailing() throws FormatterException { - String input = "package test;\nclass T {\n\n}"; + String input = + """ + package test; + class T { + + }\ + """; String output = new Formatter().formatSource(input); - String expect = "package test;\n\nclass T {}\n"; + String expect = + """ + package test; + + class T {} + """; assertThat(output).isEqualTo(expect); } @Test public void docCommentTrailingBlank() throws FormatterException { - String input = "class T {\n/** asd */\n\nint x;\n}"; + String input = + """ + class T { + /** asd */ + + int x; + }\ + """; String output = new Formatter().formatSource(input); - String expect = "class T {\n /** asd */\n int x;\n}\n"; + String expect = + """ + class T { + /** asd */ + int x; + } + """; assertThat(output).isEqualTo(expect); } @Test public void blockCommentInteriorTrailingBlank() throws FormatterException { - String input = "class T {\n/*\n* asd \n* fgh\n*/ \n\nint x;\n}"; + String input = + """ + class T { + /* + * asd + * fgh + */ + + int x; + }\ + """; String output = new Formatter().formatSource(input); - String expect = "class T {\n /*\n * asd\n * fgh\n */\n\n int x;\n}\n"; + String expect = + """ + class T { + /* + * asd + * fgh + */ + + int x; + } + """; assertThat(output).isEqualTo(expect); } @Test public void blockCommentTrailingBlank() throws FormatterException { - String input = "class T {\n/* asd */ \n\nint x;\n}"; + String input = + """ + class T { + /* asd */ + + int x; + }\ + """; String output = new Formatter().formatSource(input); - String expect = "class T {\n /* asd */\n\n int x;\n}\n"; + String expect = + """ + class T { + /* asd */ + + int x; + } + """; assertThat(output).isEqualTo(expect); } @Test public void lineCommentTrailingBlank() throws FormatterException { - String input = "class T {\n// asd \n\nint x;\n}"; + String input = + """ + class T { + // asd + + int x; + }\ + """; String output = new Formatter().formatSource(input); - String expect = "class T {\n // asd\n\n int x;\n}\n"; + String expect = + """ + class T { + // asd + + int x; + } + """; assertThat(output).isEqualTo(expect); } @@ -215,15 +320,36 @@ public void lineCommentTrailingThinSpace() throws FormatterException { // The Unicode thin space is matched by CharMatcher.whitespace() but not trim(). String input = "class T {\n // asd\u2009\n}\n"; String output = new Formatter().formatSource(input); - String expect = "class T {\n // asd\n}\n"; + String expect = + """ + class T { + // asd + } + """; assertThat(output).isEqualTo(expect); } @Test public void noBlankAfterLineCommentWithInteriorBlankLine() throws FormatterException { - String input = "class T {\n// asd \n\n// dsa \nint x;\n}"; + String input = + """ + class T { + // asd + + // dsa + int x; + }\ + """; String output = new Formatter().formatSource(input); - String expect = "class T {\n // asd\n\n // dsa\n int x;\n}\n"; + String expect = + """ + class T { + // asd + + // dsa + int x; + } + """; assertThat(output).isEqualTo(expect); } @@ -231,7 +357,12 @@ public void noBlankAfterLineCommentWithInteriorBlankLine() throws FormatterExcep public void badConstructor() throws FormatterException { String input = "class X { Y() {} }"; String output = new Formatter().formatSource(input); - String expect = "class X {\n Y() {}\n}\n"; + String expect = + """ + class X { + Y() {} + } + """; assertThat(output).isEqualTo(expect); } @@ -239,32 +370,37 @@ public void badConstructor() throws FormatterException { 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"; + String expect = + """ + class X { + void Y() {} + } + """; assertThat(output).isEqualTo(expect); } private static final String UNORDERED_IMPORTS = - Joiner.on('\n') - .join( - "import com.google.common.base.Preconditions;", - "", - "import static org.junit.Assert.fail;", - "import static com.google.truth.Truth.assertThat;", - "", - "import org.junit.runners.JUnit4;", - "import org.junit.runner.RunWith;", - "", - "import java.util.List;", - "", - "import javax.annotation.Nullable;"); + """ + import com.google.common.base.Preconditions; + + import static org.junit.Assert.fail; + import static com.google.truth.Truth.assertThat; + + import org.junit.runners.JUnit4; + import org.junit.runner.RunWith; + + import java.util.List; + + import javax.annotation.Nullable; + """; @Test public void importsNotReorderedByDefault() throws FormatterException { String input = - "package com.google.example;\n" + UNORDERED_IMPORTS + "\npublic class ExampleTest {}\n"; + "package com.google.example;\n" + UNORDERED_IMPORTS + "public class ExampleTest {}\n"; String output = new Formatter().formatSource(input); String expect = - "package com.google.example;\n\n" + UNORDERED_IMPORTS + "\n\npublic class ExampleTest {}\n"; + "package com.google.example;\n\n" + UNORDERED_IMPORTS + "\npublic class ExampleTest {}\n"; assertThat(output).isEqualTo(expect); } @@ -273,17 +409,23 @@ public void importsFixedIfRequested() throws FormatterException { String input = "package com.google.example;\n" + UNORDERED_IMPORTS - + "\npublic class ExampleTest {\n" - + " @Nullable List xs;\n" - + "}\n"; + + """ + public class ExampleTest { + @Nullable List xs; + } + """; String output = new Formatter().formatSourceAndFixImports(input); String expect = - "package com.google.example;\n\n" - + "import java.util.List;\n" - + "import javax.annotation.Nullable;\n\n" - + "public class ExampleTest {\n" - + " @Nullable List xs;\n" - + "}\n"; + """ + package com.google.example; + + import java.util.List; + import javax.annotation.Nullable; + + public class ExampleTest { + @Nullable List xs; + } + """; assertThat(output).isEqualTo(expect); } @@ -339,7 +481,7 @@ private void importOrdering(String sortArg, String outputResourceName) private String getResource(String resourceName) throws IOException { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(resourceName)) { - assertWithMessage("Missing resource: " + resourceName).that(stream).isNotNull(); + assertWithMessage("Missing resource: %s", resourceName).that(stream).isNotNull(); return CharStreams.toString(new InputStreamReader(stream, UTF_8)); } } @@ -354,13 +496,25 @@ public void testTrailingCommentWithoutTerminalNewline() throws Exception { @Test public void testEmptyArray() throws Exception { assertThat(new Formatter().formatSource("class T { int x[] = {,}; }")) - .isEqualTo("class T {\n int x[] = {,};\n}\n"); + .isEqualTo( + """ + class T { + int x[] = {,}; + } + """); } @Test public void stringEscapeLength() throws Exception { assertThat(new Formatter().formatSource("class T {{ f(\"\\\"\"); }}")) - .isEqualTo("class T {\n {\n f(\"\\\"\");\n }\n}\n"); + .isEqualTo( + """ + class T { + { + f(\"\\\"\"); + } + } + """); } @Test @@ -368,21 +522,22 @@ public void wrapLineComment() throws Exception { assertThat( new Formatter() .formatSource( - "class T {\n" - + " public static void main(String[] args) { // one long incredibly" - + " unbroken sentence moving from topic to topic so that no-one had a" - + " chance to interrupt;\n" - + " }\n" - + "}\n")) +""" +class T { + public static void main(String[] args) { // one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt; + } +} +""")) .isEqualTo( - "class T {\n" - + " public static void main(\n" - + " String[]\n" - + " args) { // one long incredibly unbroken sentence moving" - + " from topic to topic so that no-one\n" - + " // had a chance to interrupt;\n" - + " }\n" - + "}\n"); +""" +class T { + public static void main( + String[] + args) { // one long incredibly unbroken sentence moving from topic to topic so that no-one + // had a chance to interrupt; + } +} +"""); } @Test @@ -390,21 +545,21 @@ public void onlyWrapLineCommentOnWhitespace() throws Exception { assertThat( new Formatter() .formatSource( - "class T {\n" - + " public static void main(String[] args) { // one_long_incredibly" - + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" - + "_chance_to_interrupt;\n" - + " }\n" - + "}\n")) +""" +class T { + public static void main(String[] args) { // one_long_incredibly_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a_chance_to_interrupt; + } +} +""")) .isEqualTo( - "class T {\n" - + " public static void main(\n" - + " String[]\n" - + " args) { // one_long_incredibly" - + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" - + "_chance_to_interrupt;\n" - + " }\n" - + "}\n"); +""" +class T { + public static void main( + String[] + args) { // one_long_incredibly_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a_chance_to_interrupt; + } +} +"""); } @Test @@ -412,53 +567,55 @@ public void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Excepti assertThat( new Formatter() .formatSource( - "class T {\n" - + " public static void main(String[] args) { //one_long_incredibly" - + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" - + "_chance_to_interrupt;\n" - + " }\n" - + "}\n")) +""" +class T { + public static void main(String[] args) { //one_long_incredibly_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a_chance_to_interrupt; + } +} +""")) .isEqualTo( - "class T {\n" - + " public static void main(\n" - + " String[]\n" - + " args) { // one_long_incredibly" - + "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a" - + "_chance_to_interrupt;\n" - + " }\n" - + "}\n"); +""" +class T { + public static void main( + String[] + args) { // one_long_incredibly_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a_chance_to_interrupt; + } +} +"""); } @Test public void throwsFormatterException() throws Exception { - try { - new Formatter().formatSourceAndFixImports("package foo; public class {"); - fail(); - } catch (FormatterException expected) { - } + assertThrows( + FormatterException.class, + () -> new Formatter().formatSourceAndFixImports("package foo; public class {")); } @Test public void blankLinesImportComment() throws FormatterException { String withBlank = - "package p;\n" - + "\n" - + "/** test */\n" - + "\n" - + "import a.A;\n" - + "\n" - + "class T {\n" - + " A a;\n" - + "}\n"; + """ + package p; + + /** test */ + + import a.A; + + class T { + A a; + } + """; String withoutBlank = - "package p;\n" - + "\n" - + "/** test */\n" - + "import a.A;\n" - + "\n" - + "class T {\n" - + " A a;\n" - + "}\n"; + """ + package p; + + /** test */ + import a.A; + + class T { + A a; + } + """; // Formatting deletes the blank line between the "javadoc" and the first import. assertThat(new Formatter().formatSource(withBlank)).isEqualTo(withoutBlank); @@ -478,17 +635,17 @@ public void dontWrapMoeLineComments() throws Exception { assertThat( new Formatter() .formatSource( - "class T {\n" - + " // MOE: one long incredibly" - + " unbroken sentence moving from topic to topic so that no-one had a" - + " chance to interrupt;\n" - + "}\n")) +""" +class T { + // MOE: one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt; +} +""")) .isEqualTo( - "class T {\n" - + " // MOE: one long incredibly" - + " unbroken sentence moving from topic to topic so that no-one had a" - + " chance to interrupt;\n" - + "}\n"); +""" +class T { + // MOE: one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt; +} +"""); } @Test @@ -503,11 +660,13 @@ public void removeTrailingTabsInComments() throws Exception { + " }\n" + "}\n")) .isEqualTo( - "class Foo {\n" - + " void f() {\n" - + " int x = 0; // comment\n" - + " return;\n" - + " }\n" - + "}\n"); + """ + class Foo { + void f() { + int x = 0; // comment + return; + } + } + """); } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java index a7da2c1dd..f386efd9e 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -17,9 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.common.base.Joiner; import com.google.common.io.ByteStreams; -import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -32,156 +30,179 @@ public final class JavadocFormattingTest { @Test public void notJavadoc() { - String[] input = { - "/**/", // - "class Test {}", - }; - String[] expected = { - "/**/", // - "class Test {}", - }; + String input = + """ + /**/ + class Test {}\ + """; + String expected = + """ + /**/ + class Test {} + """; doFormatTest(input, expected); } @Test public void empty() { - String[] input = { - "/***/", // - "class Test {}", - }; - String[] expected = { - "/***/", "class Test {}", - }; + String input = + """ + /***/ + class Test {}\ + """; + String expected = + """ + /***/ + class Test {} + """; doFormatTest(input, expected); } @Test public void emptyMultipleLines() { - String[] input = { - "/**", // - " */", - "class Test {}", - }; - String[] expected = { - "/** */", "class Test {}", - }; + String input = + """ + /** + */ + class Test {}\ + """; + String expected = + """ + /** */ + class Test {} + """; doFormatTest(input, expected); } @Test public void simple() { - String[] input = { - "/** */", // - "class Test {}", - }; - String[] expected = { - "/** */", "class Test {}", - }; + String input = + """ + /** */ + class Test {}\ + """; + String expected = + """ + /** */ + class Test {} + """; doFormatTest(input, expected); } @Test public void commentMostlyUntouched() { // This test isn't necessarily what we'd want to do, but it's what we do now, and it's OK-ish. - String[] input = { - "/**", - " * Foo.", - " *", - " * bar", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Foo.", - " * ", - " * bar", - " */", - "class Test {}", - }; + @SuppressWarnings("MisleadingEscapedSpace") // TODO(b/496180372): remove + String input = + """ + /** + * Foo. + * + * bar + */ + class Test {}\ + """; + String expected = + """ + /** + * Foo. + * + * bar + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void moeComments() { - String[] input = { - "/**", - " * Deatomizes the given user.", - " * ", - " * See deatomizer-v5 for the design doc.", - " * ", - " * To reatomize, call {@link reatomize}.", - " *", - " * ", - " *

This method is used in the Google teleporter.", - " *", - " *

Yes, we have a teleporter.", - " * ", - " *", - " * @param user the person to teleport.", - " * ", - " * Users must sign deatomize-waiver ahead of time.", - " * ", - " * ", - " * @deprecated Sometimes turns the user into a goat.", - " * ", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Deatomizes the given user.", - " * ", - " * See deatomizer-v5 for the design doc.", - " * ", - " * To reatomize, call {@link reatomize}.", - " *", - " * ", - " *

This method is used in the Google teleporter.", - " *", - " *

Yes, we have a teleporter.", - " * ", - " *", - " * @param user the person to teleport.", - " * ", - " * Users must sign deatomize-waiver ahead of time.", - " * ", - " * ", - " * @deprecated Sometimes turns the user into a goat.", - " * ", - " */", - "class Test {}", - }; + // We replace moe by MOE to avoid triggering actual MOE rewriting. + String input = + """ + /** + * Deatomizes the given user. + * + * See deatomizer-v5 for the design doc. + * + * To reatomize, call {@link reatomize}. + * + * + *

This method is used in the Google teleporter. + * + *

Yes, we have a teleporter. + * + * + * @param user the person to teleport. + * + * Users must sign deatomize-waiver ahead of time. + * + * + * @deprecated Sometimes turns the user into a goat. + * + */ + class Test {}\ + """ + .replace("moe", "MOE"); + String expected = + """ + /** + * Deatomizes the given user. + * + * See deatomizer-v5 for the design doc. + * + * To reatomize, call {@link reatomize}. + * + * + *

This method is used in the Google teleporter. + * + *

Yes, we have a teleporter. + * + * + * @param user the person to teleport. + * + * Users must sign deatomize-waiver ahead of time. + * + * + * @deprecated Sometimes turns the user into a goat. + * + */ + class Test {} + """ + .replace("moe", "MOE"); doFormatTest(input, expected); } @Test public void moeCommentBeginOnlyInMiddleOfDoc() { // We don't really care what happens here so long as we don't explode. - String[] input = { - "/**", // - " * Foo.", - " * ", - " * Bar.", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Foo.", - " * ", - " * Bar.", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + * Bar. + */ + class Test {}\ + """ + .replace("moe", "MOE"); + String expected = + """ + /** + * Foo. + * + * Bar. + */ + class Test {} + """ + .replace("moe", "MOE"); doFormatTest(input, expected); } @@ -189,70 +210,79 @@ public void moeCommentBeginOnlyInMiddleOfDoc() { public void moeCommentBeginOnlyAtEndOfDoc() { // We don't really care what happens here so long as we don't explode. // TODO(cpovirk): OK, maybe try to leave it in.... - String[] input = { - "/**", // - " * Foo.", - " * ", - " */", - "class Test {}", - }; - String[] expected = { - "/** Foo. */", // - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + */ + class Test {}\ + """ + .replace("moe", "MOE"); + String expected = + """ + /** Foo. */ + class Test {} + """; doFormatTest(input, expected); } @Test public void moeCommentEndOnly() { // We don't really care what happens here so long as we don't explode. - String[] input = { - "/**", // - " * Foo.", - " * ", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Foo.", - " * ", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + */ + class Test {}\ + """ + .replace("moe", "MOE"); + String expected = + """ + /** + * Foo. + * + */ + class Test {} + """ + .replace("moe", "MOE"); doFormatTest(input, expected); } @Test public void tableMostlyUntouched() { - String[] input = { - "/**", - " * Foo.", - " *", - " * ", - "*", - " * ", - " * ", - " *
ab
A", - " * B", - " *
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Foo.", - " *", - " * ", - " * ", - " * ", - " * ", - " *
ab
A", - " * B", - " *
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + * + * + * + * + *
ab
A + * B + *
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Foo. + * + * + * + * + * + *
ab
A + * B + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @@ -268,206 +298,215 @@ public void preMostlyUntouched() { * the change at all. (We've also slightly complicated NEWLINE_PATTERN and writeNewline to * accommodate it.) */ - String[] input = { - "/**", // - " * Example:", - " *", - " *

",
-      "*    1 2
3 ", - " *4 5 6", - "7 8", - " *
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Example:", - " *", - " *
",
-      " *    1 2
3", - " * 4 5 6", - " * 7 8", - " *
", - " */", - "class Test {}", - }; + @SuppressWarnings("MisleadingEscapedSpace") // TODO(b/496180372): remove + String input = + """ + /** + * Example: + * + *
+        *    1 2
3 \s + *4 5 6 + 7 8 + *
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Example: + * + *
+         *    1 2
3 + * 4 5 6 + * 7 8 + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void preCodeExample() { // We should figure out whether we want a newline or blank line before
 or not.
-    String[] input = {
-      "/**",
-      " * Example:",
-      " *",
-      " * 
   {@code",
-      " *",
-      " *   Abc.def(foo, 7, true); // blah}
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Example:", - " *", - " *
{@code",
-      " * Abc.def(foo, 7, true); // blah",
-      " * }
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Example: + * + *
   {@code
+         *
+         *   Abc.def(foo, 7, true); // blah}
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Example: + * + *
{@code
+         * Abc.def(foo, 7, true); // blah
+         * }
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void preNotWrapped() { - String[] input = { - "/**", - " * Example:", - " *", - " *
",
-      " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 "
-          + "456789012 45678901",
-      " * 
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Example:", - " *", - " *
",
-      " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 "
-          + "456789012 45678901",
-      " * 
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Example: + * + *
+         * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 45678901
+         * 
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Example: + * + *
+         * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 45678901
+         * 
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void javaCodeInPre() { - String[] input = { - "/**", - " * Example:", - " *", - " *
",
-      " * aaaaa    |   a  |   +",
-      " * \"bbbb    |   b  |  \"",
-      " *
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * Example:", - " *", - " *
",
-      " * aaaaa    |   a  |   +",
-      " * \"bbbb    |   b  |  \"",
-      " * 
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Example: + * + *
+         * aaaaa    |   a  |   +
+         * "bbbb    |   b  |  "
+         *
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Example: + * + *
+         * aaaaa    |   a  |   +
+         * "bbbb    |   b  |  "
+         * 
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void joinLines() { - String[] input = { - "/**", // - " * foo", - " * bar", - " * baz", - " */", - "class Test {}", - }; - String[] expected = { - "/** foo bar baz */", // - "class Test {}", - }; + String input = + """ + /** + * foo + * bar + * baz + */ + class Test {}\ + """; + String expected = + """ + /** foo bar baz */ + class Test {} + """; doFormatTest(input, expected); } @Test public void oneLinerIs100() { - String[] input = { - "/**", - " * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 " - + "567890123 567", - " */", - "class Test {}", - }; - String[] expected = { - "/** 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 " - + "567890123 567 */", - "class Test {}", - }; + String input = + """ + /** + * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567 + */ + class Test {}\ + """; + String expected = + """ + /** 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567 */ + class Test {} + """; doFormatTest(input, expected); } @Test public void oneLinerWouldBe101() { - String[] input = { - "/**", - " * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 " - + "567890123 5678", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 " - + "567890123 5678", - " */", - "class Test {}", - }; + String input = + """ + /** + * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 5678 + */ + class Test {}\ + """; + String expected = + """ + /** + * 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 5678 + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void multilineWrap() { - String[] input = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012 45678901", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012", - " * 45678901", - " */", - "class Test {}", - }; + String input = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 45678901 + */ + class Test {}\ + """; + String expected = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 + * 45678901 + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void tooLong() { - String[] input = { - "/**", - " * abc", - " *", - " *

789012345678901234567890123456789012345678901234567890123456789012345678901234567" - + "8901234567890123456", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * abc", - " *", - " *

789012345678901234567890123456789012345678901234567890123456789012345678901234567" - + "8901234567890123456", - " */", - "class Test {}", - }; + String input = + """ + /** + * abc + * + *

7890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456 + */ + class Test {}\ + """; + String expected = + """ + /** + * abc + * + *

7890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456 + */ + class Test {} + """; doFormatTest(input, expected); } @@ -477,21 +516,21 @@ public void joinedTokens() { * Originally, 4, , and 8901 are separate tokens. Test that we join them (and thus don't * split them across lines). */ - String[] input = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012 48901", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012", - " * 48901", - " */", - "class Test {}", - }; + String input = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 48901 + */ + class Test {}\ + """; + String expected = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 + * 48901 + */ + class Test {} + """; doFormatTest(input, expected); } @@ -501,362 +540,375 @@ public void joinedAtSign() { * The last 456789012 would fit on the first line with the others. But putting it there would * mean the next line would start with @5678901, which would then be interpreted as a tag. */ - String[] input = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012 @5678901", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012", - " * 456789012 @5678901", - " */", - "class Test {}", - }; + String input = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 @5678901 + */ + class Test {}\ + """; + String expected = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 + * 456789012 @5678901 + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void joinedMultipleAtSign() { // This is the same as above except that it tests multiple consecutive @... tokens. - String[] input = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "@56789012 @5678901", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012", - " * 456789012 @56789012 @5678901", - " */", - "class Test {}", - }; + String input = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 @56789012 @5678901 + */ + class Test {}\ + """; + String expected = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 + * 456789012 @56789012 @5678901 + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void noAsterisk() { - String[] input = { - "/**", // - " abc

def", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * abc", - " *", - " *

def", - " */", - "class Test {}", - }; + String input = + """ + /** + abc

def + */ + class Test {}\ + """; + String expected = + """ + /** + * abc + * + *

def + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void significantAsterisks() { - String[] input = { - "/** *", // - " * *", - " */", - "class Test {}", - }; - String[] expected = { - "/** * * */", // - "class Test {}", - }; + String input = + """ + /** * + * * + */ + class Test {}\ + """; + String expected = + """ + /** * * */ + class Test {} + """; doFormatTest(input, expected); } @Test public void links() { - String[] input = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012 4567 foo.", - " *", - " *

789012 456789012 456789012 456789012 456789012 456789012 456789012 456789 " - + "", - " * foo.", - " *", - " *

789012 456789012 456789012 456789012 456789012 456789012 456789012 4567890 " - + "", - " * foo.", - " *", - " *

", - " * foo.", - " *", - " *

foo ", - " * bar.", - " *", - " *

foo-", - " * bar.", - " *", - " *

foo", - " * bar.", - " *", - " *

foo bar.", - " */", - "class Test {}", - }; - String[] expected = { - "/**", - " * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 " - + "456789012 4567 foo.", - " *", - " *

789012 456789012 456789012 456789012 456789012 456789012 456789012 456789 " - + "foo.", - " *", - " *

789012 456789012 456789012 456789012 456789012 456789012 456789012 4567890 " - + "", - " * foo.", - " *", - " *

foo.", - " *", - " *

foo bar.", - " *", - " *

foo-bar.", - " *", - /* - * In this next case, we've removed a space from the output. Fortunately, the depot doesn't - * appear to contain any occurrences of this pattern. And if it does, the better fix is to - * insert a space before rather than after. - */ - " *

foobar.", - " *", - " *

foo bar.", - " */", - "class Test {}", - }; + String input = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 4567 foo. + * + *

789012 456789012 456789012 456789012 456789012 456789012 456789012 456789 + * foo. + * + *

789012 456789012 456789012 456789012 456789012 456789012 456789012 4567890 + * foo. + * + *

+ * foo. + * + *

foo + * bar. + * + *

foo- + * bar. + * + *

foo + * bar. + * + *

foo bar. + */ + class Test {}\ + """; + String expected = + """ + /** + * 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 456789012 4567 foo. + * + *

789012 456789012 456789012 456789012 456789012 456789012 456789012 456789 foo. + * + *

789012 456789012 456789012 456789012 456789012 456789012 456789012 4567890 + * foo. + * + *

foo. + * + *

foo bar. + * + *

foo-bar. + * + *

foobar. + * + *

foo bar. + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void heading() { - String[] input = { - "/**", // - " * abc

def

ghi", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * abc", - " *", - " *

def

", - " *", - " * ghi", - " */", - "class Test {}", - }; + String input = + """ + /** + * abc

def

ghi + */ + class Test {}\ + """; + String expected = + """ + /** + * abc + * + *

def

+ * + * ghi + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void blockquote() { - String[] input = { - "/**", // - " * abc

def

ghi", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * abc", - " *", - " *
", - " *", - " *

def", - " *", - " *

", - " *", - " * ghi", - " */", - "class Test {}", - }; + String input = + """ + /** + * abc

def

ghi + */ + class Test {}\ + """; + String expected = + """ + /** + * abc + * + *
+ * + *

def + * + *

+ * + * ghi + */ + class Test {} + """; doFormatTest(input, expected); } @Test public void lists() { - String[] input = { - "/**", // - "* hi", - "*", - "*
    ", - "*
  • ", - "*
      ", - "*
    • a
    • ", - "*
    ", - "*
  • ", - "*
", - "*/", - "class Test {}", - }; - String[] expected = { - "/**", // - " * hi", - " *", - " *
    ", - " *
  • ", - " *
      ", - " *
    • a", - " *
    ", - " *
", - " */", - "class Test {}", - }; + String input = + """ + /** + * hi + * + *
    + *
  • + *
      + *
    • a
    • + *
    + *
  • + *
+ */ + class Test {}\ + """; + String expected = + """ + /** + * hi + * + *
    + *
  • + *
      + *
    • a + *
    + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void lists2() { - String[] input = { - "/**", // - " * Foo.", - " *", - " *
  • 1
    • 1a
    • 1b
    more 1

    still more 1

  • 2
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Foo.", - " *", - " *
    ", - " *
  • 1", - " *
      ", - " *
    • 1a", - " *
    • 1b", - " *
    ", - " * more 1", - " *

    still more 1", - " *

  • 2", - " *
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + *
  • 1
    • 1a
    • 1b
    more 1

    still more 1

  • 2
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Foo. + * + *
    + *
  • 1 + *
      + *
    • 1a + *
    • 1b + *
    + * more 1 + *

    still more 1 + *

  • 2 + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void closeInnerListStillNewline() { - String[] input = { - "/**", // - " * Foo.", - " *", - " *
    • a
    b
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Foo.", - " *", - " *
    ", - " *
  • ", - " *
      ", - " *
    • a", - " *
    ", - " * b", - " *
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + *
    • a
    b
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Foo. + * + *
    + *
  • + *
      + *
    • a + *
    + * b + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void listItemWrap() { - String[] input = { - "/**", // - " * Foo.", - " *", - " *
  • 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890" - + " 234567890 234567890
", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Foo.", - " *", - " *
    ", - " *
  • 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890" - + " 234567890", - " * 234567890", - " *
", - " */", - "class Test {}", - }; + String input = + """ + /** + * Foo. + * + *
  • 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890
+ */ + class Test {}\ + """; + String expected = + """ + /** + * Foo. + * + *
    + *
  • 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890 234567890 + * 234567890 + *
+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void unclosedList() { - String[] input = { - "/**", // - " * Foo.", - " *", - " *