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.", - " *", - " *
| a | b |
| A", - " * | B", - " * |
| a | b |
| A", - " * | B", - " * |
| a | b |
| A + * | B + * |
| a | b |
| A + * | B + * |
", - "* 1 2", - " */", - "class Test {}", - }; - String[] expected = { - "/**", // - " * Example:", - " *", - " *
3 ", - " *4 5 6", - "7 8", - " *
", - " * 1 2", - " */", - "class Test {}", - }; + @SuppressWarnings("MisleadingEscapedSpace") // TODO(b/496180372): remove + String input = + """ + /** + * Example: + * + *
3", - " * 4 5 6", - " * 7 8", - " *
+ * 1 2+ */ + class Test {}\ + """; + String expected = + """ + /** + * Example: + * + *
3 \s + *4 5 6 + 7 8 + *
+ * 1 2+ */ + class Test {} + """; doFormatTest(input, expected); } @Test public void preCodeExample() { // We should figure out whether we want a newline or blank line before
3 + * 4 5 6 + * 7 8 + *
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 =
+ """
+ /**
+ * abcdef
ghi
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * abc
+ *
+ * def
+ *
+ * ghi
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void blockquote() {
- String[] input = {
- "/**", //
- " * abcdef
ghi",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * abc",
- " *",
- " * ",
- " *",
- " * def",
- " *",
- " *
",
- " *",
- " * ghi",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * abcdef
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 1still 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 1still 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.",
- " *",
- " * - 1",
- " * @return blah",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * Foo.",
- " *",
- " *
",
- " * - 1",
- " *",
- " * @return blah",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * Foo.
+ *
+ *
- 1
+ * @return blah
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * Foo.
+ *
+ *
+ * - 1
+ *
+ * @return blah
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void br() {
- 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 brSpaceBug() {
// TODO(b/28983091): Remove the space before
here.
- 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);
}
@@ -866,19 +918,22 @@ public void brAtSignBug() {
* This is a bug -- more of a "spec" bug than an implementation bug, and hard to fix.
* Fortunately, some very quick searching didn't turn up any instances in the Google codebase.
*/
- String[] input = {
- "/**", //
- " * abc
@foo ",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * abc
",
- " * @foo", // interpreted as a block tag now!
- " */",
- "class Test {}",
- };
+ @SuppressWarnings("MisleadingEscapedSpace") // TODO(b/496180372): remove
+ String input =
+ """
+ /**
+ * abc
@foo\s
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * abc
+ * @foo
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@@ -889,292 +944,314 @@ public void unicodeCharacterCountArguableBug() {
* perhaps for all characters to be treated based on their width in monospace fonts). But
* currently we just count chars.
*/
- String[] input = {
- "/**",
- " * 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 "
- + "456789π12 456789π",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**",
- " * 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12",
- " * 456789π12 456789π",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12 456789π12
+ * 456789π12 456789π
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void blankLinesAroundSnippetAndNoMangling() {
- String[] input = {
- "/**", //
- " * hello world",
- " * {@snippet :",
- " * public class Foo {",
- " * private String s;",
- " * }",
- " * }",
- " * hello again",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * hello world",
- " *",
- " * {@snippet :",
- " * public class Foo {",
- " * private String s;",
- " * }",
- " * }",
- " *",
- " * hello again",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * hello world
+ * {@snippet :
+ * public class Foo {
+ * private String s;
+ * }
+ * }
+ * hello again
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * hello world
+ *
+ * {@snippet :
+ * public class Foo {
+ * private String s;
+ * }
+ * }
+ *
+ * hello again
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void notASnippetUnlessOuterTag() {
- String[] input = {
- "/** I would like to tell you about the {@code {@snippet ...}} tag. */", "class Test {}",
- };
- String[] expected = {
- "/** I would like to tell you about the {@code {@snippet ...}} tag. */", "class Test {}",
- };
+ String input =
+ """
+ /** I would like to tell you about the {@code {@snippet ...}} tag. */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** I would like to tell you about the {@code {@snippet ...}} tag. */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void blankLineBeforeParams() {
- String[] input = {
- "/**", //
- " * hello world",
- " * @param this is a param",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * hello world",
- " *",
- " * @param this is a param",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * hello world
+ * @param this is a param
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * hello world
+ *
+ * @param this is a param
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void onlyParams() {
- String[] input = {
- "/**", //
- " *",
- " *",
- " * @param this is a param",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * @param this is a param",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ *
+ *
+ * @param this is a param
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * @param this is a param
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void paramsContinuationIndented() {
- String[] input = {
- "/**", //
- " * hello world",
- " *",
- " * @param foo 567890123 567890123 567890123 567890123 567890123 567890123 567890123"
- + " 567890123 567890123",
- " * @param bar another",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * hello world",
- " *",
- " * @param foo 567890123 567890123 567890123 567890123 567890123 567890123 567890123"
- + " 567890123",
- " * 567890123",
- " * @param bar another",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * hello world
+ *
+ * @param foo 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123
+ * @param bar another
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * hello world
+ *
+ * @param foo 567890123 567890123 567890123 567890123 567890123 567890123 567890123 567890123
+ * 567890123
+ * @param bar another
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void paramsOtherIndents() {
- String[] input = {
- "/**", //
- " * hello world",
- " *",
- " * @param foo ab
- a
- x
",
- " * @param bar another",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * hello world",
- " *",
- " * @param foo a",
- " * b",
- " *
",
- " * - a",
- " *
",
- " * - x",
- " *
",
- " *
",
- " *", // TODO(cpovirk): Ideally we would probably eliminate this.
- " * @param bar another",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * hello world
+ *
+ * @param foo ab
- a
- x
+ * @param bar another
+ */
+ class Test {}\
+ """;
+ // TODO(cpovirk): Ideally we would probably eliminate the blank line before the second @param.
+ String expected =
+ """
+ /**
+ * hello world
+ *
+ * @param foo a
+ * b
+ *
+ * - a
+ *
+ * - x
+ *
+ *
+ *
+ * @param bar another
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void paragraphTag() {
- String[] input = {
- "class Test {",
- " /**",
- " * helloworld",
- " */",
- " void f() {}",
- "",
- " /**",
- " * hello",
- " *
",
- " * world",
- " */",
- " void f() {}",
- "}",
- };
- String[] expected = {
- "class Test {",
- " /**",
- " * hello",
- " *",
- " *
world",
- " */",
- " void f() {}",
- "",
- " /**",
- " * hello",
- " *",
- " *
world",
- " */",
- " void f() {}",
- "}",
- };
+ String input =
+ """
+ class Test {
+ /**
+ * hello
world
+ */
+ void f() {}
+
+ /**
+ * hello
+ *
+ * world
+ */
+ void f() {}
+ }\
+ """;
+ String expected =
+ """
+ class Test {
+ /**
+ * hello
+ *
+ *
world
+ */
+ void f() {}
+
+ /**
+ * hello
+ *
+ *
world
+ */
+ void f() {}
+ }
+ """;
doFormatTest(input, expected);
}
@Test
public void xhtmlParagraphTag() {
- String[] input = {
- "class Test {", //
- " /**",
- " * hello
world",
- " */",
- " void f() {}",
- "",
- "}",
- };
- String[] expected = {
- "class Test {", //
- " /**",
- " * hello",
- " *",
- " * world",
- " */",
- " void f() {}",
- "}",
- };
+ String input =
+ """
+ class Test {
+ /**
+ * hello
world
+ */
+ void f() {}
+
+ }\
+ """;
+ String expected =
+ """
+ class Test {
+ /**
+ * hello
+ *
+ * world
+ */
+ void f() {}
+ }
+ """;
doFormatTest(input, expected);
}
@Test
public void removeInitialParagraphTag() {
- String[] input = {
- "/**", //
- " *
hello
world",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * hello",
- " *",
- " *
world",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ *
hello
world
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * hello
+ *
+ *
world
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void inferParagraphTags() {
- String[] input = {
- "/**",
- " *",
- " *",
- " * foo",
- " * foo",
- " *",
- " *",
- " * foo",
- " *",
- " * bar",
- " *",
- " *
",
- " *",
- " * baz",
- " *",
- " *
",
- " *",
- " * ",
- " * - foo",
- " *",
- " * bar",
- " *
",
- " *",
- " *",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**",
- " * foo foo",
- " *",
- " * foo",
- " *",
- " *
bar",
- " *",
- " *
",
- " *",
- " * baz",
- " *",
- " *
",
- " *",
- " * ",
- " * - foo",
- " *
bar",
- " *
",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ *
+ *
+ * foo
+ * foo
+ *
+ *
+ * foo
+ *
+ * bar
+ *
+ *
+ *
+ * baz
+ *
+ *
+ *
+ *
+ * - foo
+ *
+ * bar
+ *
+ *
+ *
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * foo foo
+ *
+ * foo
+ *
+ *
bar
+ *
+ *
+ *
+ * baz
+ *
+ *
+ *
+ *
+ * - foo
+ *
bar
+ *
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@@ -1208,45 +1285,51 @@ public void listItemSpaces() throws Exception {
@Test
public void htmlTagsInCode() {
- String[] input = {
- "/** abc {@code {}
-
} def */", //
- "class Test {}",
- };
- String[] expected = {
- "/** abc {@code {}
-
} def */", //
- "class Test {}",
- };
+ String input =
+ """
+ /** abc {@code {}
-
} def */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** abc {@code {}
-
} def */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void loneBraceDoesNotStartInlineTag() {
- String[] input = {
- "/** { } */", //
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * {",
- " *",
- " *
}",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /** {
} */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * {
+ *
+ *
}
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void unicodeEscapesNotReplaced() {
// Test that we don't replace them with their interpretations.
- String[] input = {
- "/** foo \\u0000 bar \\u6c34 baz */", //
- "class Test {}",
- };
- String[] expected = {
- "/** foo \\u0000 bar \\u6c34 baz */", //
- "class Test {}",
- };
+ String input =
+ """
+ /** foo \\u0000 bar \\u6c34 baz */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** foo \\u0000 bar \\u6c34 baz */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@@ -1257,45 +1340,51 @@ public void unicodeEscapesNotInterpretedBug() {
* @, *, and other special chars. We don't recognize that, though, so we don't put what is
* effectively "
" on a new line.
*/
- String[] input = {
- "/** a\\u003Cp>b */", //
- "class Test {}",
- };
- String[] expected = {
- "/** a\\u003Cp>b */", //
- "class Test {}",
- };
+ String input =
+ """
+ /** a\\u003Cp>b */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** a\\u003Cp>b */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void trailingLink() {
// Eclipse's parser seems to want to discard the line break after {@link}. Test that we see it.
- String[] input = {
- "/**", //
- " * abc {@link Foo}",
- " * def",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/** abc {@link Foo} def */", //
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * abc {@link Foo}
+ * def
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** abc {@link Foo} def */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void codeInCode() {
// Eclipse's parser seems to get confused at the second {@code}. Test that we handle it.
- String[] input = {
- "/** abc {@code {@code foo}} def */", //
- "class Test {}",
- };
- String[] expected = {
- "/** abc {@code {@code foo}} def */", //
- "class Test {}",
- };
+ String input =
+ """
+ /** abc {@code {@code foo}} def */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** abc {@code {@code foo}} def */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@@ -1305,124 +1394,130 @@ public void quotedTextSplitAcrossLinks() {
* This demonstrates one of multiple reasons that we can't hand the Javadoc *content* to
* Eclipse's lexer as if it were Java code.
*/
- String[] input = {
- "/**", //
- " * abc \"foo",
- " * bar baz\" def",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/** abc \"foo bar baz\" def */", //
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * abc "foo
+ * bar baz" def
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /** abc "foo bar baz" def */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void standardizeTags() {
- String[] input = {
- "/**",
- " * foo",
- " *",
- " *
bar",
- " *",
- " *
baz
",
- " * baz",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**",
- " * foo",
- " *",
- " *
bar",
- " *",
- " *
baz
",
- " * baz",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * foo
+ *
+ *
bar
+ *
+ *
baz
+ * baz
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * foo
+ *
+ *
bar
+ *
+ *
baz
+ * baz
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void removeCloseTags() {
- String[] input = {
- "/**", //
- " * foo
",
- " *",
- " * bar
",
- " */",
- "class Test {}",
- };
- String[] expected = {
- "/**", //
- " * foo",
- " *",
- " * bar",
- " */",
- "class Test {}",
- };
+ String input =
+ """
+ /**
+ * foo
+ *
+ * bar
+ */
+ class Test {}\
+ """;
+ String expected =
+ """
+ /**
+ * foo
+ *
+ * bar
+ */
+ class Test {}
+ """;
doFormatTest(input, expected);
}
@Test
public void javadocFullSentences() {
- String[] input = {
- "/** In our application, bats are often found hanging from the ceiling, especially on"
- + " Wednesdays. Sometimes sick bats have issues where their claws do not close entirely."
- + " This class provides a nice, grippable surface for them to cling to. */",
- "class Grippable {}",
- };
- String[] expected = {
- "/**",
- " * In our application, bats are often found hanging from the ceiling, especially on"
- + " Wednesdays.",
- " * Sometimes sick bats have issues where their claws do not close entirely. This class"
- + " provides a",
- " * nice, grippable surface for them to cling to.",
- " */",
- "class Grippable {}",
- };
+ String input =
+ """
+ /** In our application, bats are often found hanging from the ceiling, especially on Wednesdays. Sometimes sick bats have issues where their claws do not close entirely. This class provides a nice, grippable surface for them to cling to. */
+ class Grippable {}\
+ """;
+ String expected =
+ """
+ /**
+ * In our application, bats are often found hanging from the ceiling, especially on Wednesdays.
+ * Sometimes sick bats have issues where their claws do not close entirely. This class provides a
+ * nice, grippable surface for them to cling to.
+ */
+ class Grippable {}
+ """;
doFormatTest(input, expected);
}
@Test
public void javadocSentenceFragment() {
- String[] input = {
- "/** Provides a comfy, grippable surface for sick bats with claw-closing problems, which are"
- + " sometimes found hanging from the ceiling on Wednesdays. */",
- "class Grippable {}",
- };
- String[] expected = {
- "/**",
- " * Provides a comfy, grippable surface for sick bats with claw-closing problems, which are"
- + " sometimes",
- " * found hanging from the ceiling on Wednesdays.",
- " */",
- "class Grippable {}",
- };
+ String input =
+ """
+ /** Provides a comfy, grippable surface for sick bats with claw-closing problems, which are sometimes found hanging from the ceiling on Wednesdays. */
+ class Grippable {}\
+ """;
+ String expected =
+ """
+ /**
+ * Provides a comfy, grippable surface for sick bats with claw-closing problems, which are sometimes
+ * found hanging from the ceiling on Wednesdays.
+ */
+ class Grippable {}
+ """;
doFormatTest(input, expected);
}
@Test
public void javadocCanEndAnywhere() {
- String[] input = {
- "/** foo
world", " */", "class Test {}",
- };
- for (String separator : Arrays.asList("\r", "\r\n")) {
- String actual = formatter.formatSource(Joiner.on(separator).join(input));
- assertThat(actual).isEqualTo(Joiner.on(separator).join(input) + separator);
+ String input =
+ """
+ /**
+ * hello
+ *
+ * world
+ */
+ class Test {}\
+ """;
+ for (String separator : new String[] {"\r", "\r\n"}) {
+ String actual = formatter.formatSource(input.replace("\n", separator));
+ assertThat(actual).isEqualTo(input.replace("\n", separator) + separator);
}
}
@Test
public void u2028LineSeparator() {
- String[] input = {
- "public class Foo {",
- " /**\u2028",
- " * Set and enable something.",
- " */",
- " public void setSomething() {}",
- "}",
- };
- String[] expected = {
- "public class Foo {",
- " /**",
- " * \u2028 Set and enable something.",
- " */",
- " public void setSomething() {}",
- "}",
- };
+ // The subterfuge with β€ here is needed because of https://bugs.openjdk.org/browse/JDK-8380912.
+ String input =
+ """
+ public class Foo {
+ /**β€
+ * Set and enable something.
+ */
+ public void setSomething() {}
+ }\
+ """
+ .replace("β€", "\u2028");
+ String expected =
+ """
+ public class Foo {
+ /**
+ * β€ Set and enable something.
+ */
+ public void setSomething() {}
+ }
+ """
+ .replace("β€", "\u2028");
doFormatTest(input, expected);
}
@Test
public void missingSummaryFragment() {
- String[] input = {
- "public class Foo {",
- " /**",
- " * @return something.",
- " */",
- " public void setSomething() {}",
- "",
- " /**",
- " * @hide",
- " */",
- " public void setSomething() {}",
- "}",
- };
- String[] expected = {
- "public class Foo {",
- " /**",
- " * @return something.",
- " */",
- " public void setSomething() {}",
- "",
- " /** @hide */",
- " public void setSomething() {}",
- "}",
- };
+ String input =
+ """
+ public class Foo {
+ /**
+ * @return something.
+ */
+ public void setSomething() {}
+
+ /**
+ * @hide
+ */
+ public void setSomething() {}
+ }\
+ """;
+ String expected =
+ """
+ public class Foo {
+ /**
+ * @return something.
+ */
+ public void setSomething() {}
+
+ /** @hide */
+ public void setSomething() {}
+ }
+ """;
doFormatTest(input, expected);
}
@Test
public void simpleMarkdown() {
- String[] input = {
- "package com.example;",
- "",
- "/// # Heading",
- "///",
- "/// A very long line of text, long enough that it will need to be wrapped to fit within the"
- + " maximum line length.",
- "class Test {",
- " /// Another very long line of text, also long enough that it will need to be wrapped to"
- + " fit within the maximum line length.",
- " /// @param a generic type",
- " T method() {",
- " return null;",
- " }",
- "}",
- };
+ String input =
+ """
+ package com.example;
+
+ /// # Heading
+ ///
+ /// A very long line of text, long enough that it will need to be wrapped to fit within the maximum line length.
+ class Test {
+ /// Another very long line of text, also long enough that it will need to be wrapped to fit within the maximum line length.
+ /// @param a generic type
+ T method() {
+ return null;
+ }
+ }\
+ """;
// TODO(emcmanus): Actually format the javadoc. For now, we just leave `///` lines alone, unlike
// `//` lines which get wrapped.
- String[] expected = {
- "package com.example;",
- "",
- "/// # Heading",
- "///",
- "/// A very long line of text, long enough that it will need to be wrapped to fit within the"
- + " maximum line length.",
- "class Test {",
- " /// Another very long line of text, also long enough that it will need to be wrapped to"
- + " fit within the maximum line length.",
- " /// @param a generic type",
- " T method() {",
- " return null;",
- " }",
- "}",
- };
+ String expected =
+ """
+ package com.example;
+
+ /// # Heading
+ ///
+ /// A very long line of text, long enough that it will need to be wrapped to fit within the maximum line length.
+ class Test {
+ /// Another very long line of text, also long enough that it will need to be wrapped to fit within the maximum line length.
+ /// @param a generic type
+ T method() {
+ return null;
+ }
+ }
+ """;
doFormatTest(input, expected);
}
}
diff --git a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java
index 2d9364082..56582848a 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java
@@ -20,7 +20,6 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static java.nio.charset.StandardCharsets.UTF_8;
-import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import java.io.BufferedWriter;
@@ -49,9 +48,6 @@ public class MainTest {
@Rule public TemporaryFolder testFolder = new TemporaryFolder();
- // PrintWriter instances used below are hard-coded to use system-default line separator.
- private final Joiner joiner = Joiner.on(System.lineSeparator());
-
private static final ImmutableList ADD_EXPORTS =
ImmutableList.of(
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
@@ -137,39 +133,38 @@ public void testMain() throws Exception {
// end to end javadoc formatting test
@Test
public void javadoc() throws Exception {
- String[] input = {
- "/**",
- " * graph",
- " *",
- " * graph",
- " *",
- " * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
- + " eiusmod tempor incididunt ut labore et dolore magna aliqua",
- " */",
- "class Test {",
- " /**",
- " * creates entropy",
- " */",
- " public static void main(String... args) {}",
- "}",
- };
- String[] expected = {
- "/**",
- " * graph",
- " *",
- " * graph",
- " *",
- " * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
- + " eiusmod tempor",
- " * incididunt ut labore et dolore magna aliqua",
- " */",
- "class Test {",
- " /** creates entropy */",
- " public static void main(String... args) {}",
- "}",
- "",
- };
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ String input =
+"""
+/**
+ * graph
+ *
+ * graph
+ *
+ * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
+ */
+class Test {
+ /**
+ * creates entropy
+ */
+ public static void main(String... args) {}
+}\
+""";
+ String expected =
+"""
+/**
+ * graph
+ *
+ *
graph
+ *
+ * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
+ * incididunt ut labore et dolore magna aliqua
+ */
+class Test {
+ /** creates entropy */
+ public static void main(String... args) {}
+}
+""";
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -177,35 +172,37 @@ public void javadoc() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
}
// end to end import fixing test
@Test
public void imports() throws Exception {
- String[] input = {
- "import java.util.LinkedList;",
- "import java.util.List;",
- "import java.util.ArrayList;",
- "class Test {",
- " /**",
- " * May be an {@link ArrayList}.",
- " */",
- " public static List names;",
- "}",
- };
- String[] expected = {
- "import java.util.ArrayList;",
- "import java.util.List;",
- "",
- "class Test {",
- " /**",
- " * May be an {@link ArrayList}.",
- " */",
- " public static List names;",
- "}",
- };
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ String input =
+ """
+ import java.util.LinkedList;
+ import java.util.List;
+ import java.util.ArrayList;
+ class Test {
+ /**
+ * May be an {@link ArrayList}.
+ */
+ public static List names;
+ }\
+ """;
+ String expected =
+ """
+ import java.util.ArrayList;
+ import java.util.List;
+
+ class Test {
+ /**
+ * May be an {@link ArrayList}.
+ */
+ public static List names;
+ }\
+ """;
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -213,40 +210,42 @@ public void imports() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("-", "--fix-imports-only")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
}
@Test
public void optimizeImportsDoesNotLeaveEmptyLines() throws Exception {
- String[] input = {
- "package abc;",
- "",
- "import java.util.LinkedList;",
- "import java.util.List;",
- "import java.util.ArrayList;",
- "",
- "import static java.nio.charset.StandardCharsets.UTF_8;",
- "",
- "import java.util.EnumSet;",
- "",
- "class Test ",
- "extends ArrayList {",
- "}"
- };
- String[] expected = {
- "package abc;", //
- "",
- "import java.util.ArrayList;",
- "",
- "class Test extends ArrayList {}",
- ""
- };
+ @SuppressWarnings("MisleadingEscapedSpace") // TODO(b/496180372): remove
+ String input =
+ """
+ package abc;
+
+ import java.util.LinkedList;
+ import java.util.List;
+ import java.util.ArrayList;
+
+ import static java.nio.charset.StandardCharsets.UTF_8;
+
+ import java.util.EnumSet;
+
+ class Test\s
+ extends ArrayList {
+ }\
+ """;
+ String expected =
+ """
+ package abc;
+
+ import java.util.ArrayList;
+
+ class Test extends ArrayList {}
+ """;
// pre-check expectation with local formatter instance
- String optimized = new Formatter().formatSourceAndFixImports(joiner.join(input));
- assertThat(optimized).isEqualTo(joiner.join(expected));
+ String optimized = new Formatter().formatSourceAndFixImports(input);
+ assertThat(optimized).isEqualTo(expected);
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -254,36 +253,39 @@ public void optimizeImportsDoesNotLeaveEmptyLines() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
}
// test that -lines handling works with import removal
@Test
public void importRemovalLines() throws Exception {
- String[] input = {
- "import java.util.ArrayList;",
- "import java.util.List;",
- "class Test {",
- "ArrayList a = new ArrayList<>();",
- "ArrayList b = new ArrayList<>();",
- "}",
- };
- String[] expected = {
- "import java.util.ArrayList;",
- "",
- "class Test {",
- " ArrayList a = new ArrayList<>();",
- "ArrayList b = new ArrayList<>();",
- "}",
- };
+ String input =
+ """
+ import java.util.ArrayList;
+ import java.util.List;
+ class Test {
+ ArrayList a = new ArrayList<>();
+ ArrayList b = new ArrayList<>();
+ }\
+ """;
+ String expected =
+ """
+ import java.util.ArrayList;
+
+ class Test {
+ ArrayList a = new ArrayList<>();
+ ArrayList b = new ArrayList<>();
+ }\
+ """;
StringWriter out = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
- new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
+ new ByteArrayInputStream(input.getBytes(UTF_8)));
assertThat(main.format("-", "-lines", "4")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
+ // Line 4 gets rewritten because of `-lines 4`, but line 5 does not.
}
// test that errors are reported on the right line when imports are removed
@@ -293,19 +295,20 @@ public void importRemoveErrorParseError() throws Exception {
try {
Locale.setDefault(Locale.ROOT);
- String[] input = {
- "import java.util.ArrayList;", //
- "import java.util.List;",
- "class Test {",
- "}}",
- };
+ String input =
+ """
+ import java.util.ArrayList;
+ import java.util.List;
+ class Test {
+ }}\
+ """;
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
- new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
+ new ByteArrayInputStream(input.getBytes(UTF_8)));
assertThat(main.format("-")).isEqualTo(1);
assertThat(err.toString()).contains(":4:2: error: class, interface");
@@ -316,24 +319,24 @@ public void importRemoveErrorParseError() throws Exception {
@Test
public void packageInfo() throws Exception {
- String[] input = {
- "@CheckReturnValue",
- "@ParametersAreNonnullByDefault",
- "package com.google.common.labs.base;",
- "",
- "import com.google.errorprone.annotations.CheckReturnValue;",
- "import javax.annotation.ParametersAreNonnullByDefault;",
- "",
- };
+ String input =
+ """
+ @CheckReturnValue
+ @ParametersAreNonnullByDefault
+ package com.google.common.labs.base;
+
+ import com.google.errorprone.annotations.CheckReturnValue;
+ import javax.annotation.ParametersAreNonnullByDefault;
+ """;
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
- new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
+ new ByteArrayInputStream(input.getBytes(UTF_8)));
assertThat(main.format("-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(input));
+ assertThat(out.toString()).isEqualTo(input);
}
@Test
@@ -497,55 +500,56 @@ public void exitIfChangedFiles() throws Exception {
@Test
public void assumeFilename_error() throws Exception {
- String[] input = {
- "class Test {}}",
- };
+ String input =
+ """
+ class Test {}}\
+ """;
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
- new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
+ new ByteArrayInputStream(input.getBytes(UTF_8)));
assertThat(main.format("--assume-filename=Foo.java", "-")).isEqualTo(1);
assertThat(err.toString()).contains("Foo.java:1:14: error: class, interface");
}
@Test
public void assumeFilename_dryRun() throws Exception {
- String[] input = {
- "class Test {", //
- "}",
- };
+ String input =
+ """
+ class Test {
+ }\
+ """;
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
- new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
+ new ByteArrayInputStream(input.getBytes(UTF_8)));
assertThat(main.format("--dry-run", "--assume-filename=Foo.java", "-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo("Foo.java" + System.lineSeparator());
}
@Test
public void reflowLongStrings() throws Exception {
- String[] input = {
- "class T {", //
- " String s = \"one long incredibly unbroken sentence moving from topic to topic so that no"
- + " one had a chance to interrupt\";",
- "}"
- };
- String[] expected = {
- "class T {",
- " String s =",
- " \"one long incredibly unbroken sentence moving from topic to topic so that no one had"
- + " a chance\"",
- " + \" to interrupt\";",
- "}",
- "",
- };
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ String input =
+ """
+ class T {
+ String s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt";
+ }\
+ """;
+ String expected =
+ """
+ class T {
+ String s =
+ "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance"
+ + " to interrupt";
+ }
+ """;
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -553,26 +557,25 @@ public void reflowLongStrings() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
}
@Test
public void noReflowLongStrings() throws Exception {
- String[] input = {
- "class T {", //
- " String s = \"one long incredibly unbroken sentence moving from topic to topic so that no"
- + " one had a chance to interrupt\";",
- "}"
- };
- String[] expected = {
- "class T {",
- " String s =",
- " \"one long incredibly unbroken sentence moving from topic to topic so that no one had"
- + " a chance to interrupt\";",
- "}",
- "",
- };
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ String input =
+"""
+class T {
+ String s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt";
+}\
+""";
+ String expected =
+"""
+class T {
+ String s =
+ "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt";
+}
+""";
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -580,29 +583,29 @@ public void noReflowLongStrings() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("--skip-reflowing-long-strings", "-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(expected));
+ assertThat(out.toString()).isEqualTo(expected);
}
@Test
public void noFormatJavadoc() throws Exception {
- String[] input = {
- "/**",
- " * graph",
- " *",
- " * graph",
- " *",
- " * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
- + " eiusmod tempor incididunt ut labore et dolore magna aliqua",
- " */",
- "class Test {",
- " /**",
- " * creates entropy",
- " */",
- " public static void main(String... args) {}",
- "}",
- "",
- };
- InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
+ String input =
+ """
+ /**
+ * graph
+ *
+ * graph
+ *
+ * @param foo lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
+ * incididunt ut labore et dolore magna aliqua
+ */
+ class Test {
+ /**
+ * creates entropy
+ */
+ public static void main(String... args) {}
+ }
+ """;
+ InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
@@ -610,84 +613,83 @@ public void noFormatJavadoc() throws Exception {
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0);
- assertThat(out.toString()).isEqualTo(joiner.join(input));
+ assertThat(out.toString()).isEqualTo(input);
}
@Test
public void reorderModifiersOptionTest() throws Exception {
- String[] input = {
- "class Test {", //
- " static public void main(String... args) {}",
- "}",
- "",
- };
- String[] fixed = {
- "class Test {", //
- " public static void main(String... args) {}",
- "}",
- "",
- };
- String source = joiner.join(input);
- assertThat(new Formatter(JavaFormatterOptions.builder().build()).formatSource(source))
- .isEqualTo(joiner.join(fixed));
+ String input =
+ """
+ class Test {
+ static public void main(String... args) {}
+ }
+ """;
+ String fixed =
+ """
+ class Test {
+ public static void main(String... args) {}
+ }
+ """;
+ assertThat(new Formatter(JavaFormatterOptions.builder().build()).formatSource(input))
+ .isEqualTo(fixed);
assertThat(
new Formatter(JavaFormatterOptions.builder().reorderModifiers(false).build())
- .formatSource(source))
- .isEqualTo(source);
+ .formatSource(input))
+ .isEqualTo(input);
}
@Test
public void syntaxError() throws Exception {
Path path = testFolder.newFile("Test.java").toPath();
- String[] input = {
- "class Test {", //
- " void f(int package) {",
- " int",
- " }",
- "}",
- "",
- };
- String source = joiner.join(input);
- Files.writeString(path, source, UTF_8);
+ String input =
+ """
+ class Test {
+ void f(int package) {
+ int
+ }
+ }\
+ """;
+ Files.writeString(path, input, UTF_8);
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
int errorCode = main.format(path.toAbsolutePath().toString());
assertWithMessage("Error Code").that(errorCode).isEqualTo(1);
- String[] expected = {
- path + ":2:13: error: expected",
- " void f(int package) {",
- " ^",
- path + ":3:5: error: not a statement",
- " int",
- " ^",
- path + ":3:8: error: ';' expected",
- " int",
- " ^",
- "",
- };
- assertThat(err.toString()).isEqualTo(joiner.join(expected));
+ String expected =
+ """
+ Β«pathΒ»:2:13: error: expected
+ void f(int package) {
+ ^
+ Β«pathΒ»:3:5: error: not a statement
+ int
+ ^
+ Β«pathΒ»:3:8: error: ';' expected
+ int
+ ^
+ """
+ .replace("Β«pathΒ»", path.toString())
+ .replace("\n", System.lineSeparator());
+ assertThat(err.toString()).isEqualTo(expected);
}
@Test
public void syntaxErrorBeginning() throws Exception {
Path path = testFolder.newFile("Test.java").toPath();
- String[] input = {
- "error", //
- };
- String source = joiner.join(input);
- Files.writeString(path, source, UTF_8);
+ String input = "error";
+ Files.writeString(path, input, UTF_8);
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
int errorCode = main.format(path.toAbsolutePath().toString());
assertWithMessage("Error Code").that(errorCode).isEqualTo(1);
- String[] expected = {
- path + ":1:1: error: reached end of file while parsing", //
- "error",
- "^",
- "",
- };
- assertThat(err.toString()).isEqualTo(joiner.join(expected));
+ String expected =
+ """
+ Β«pathΒ»:1:1: error: reached end of file while parsing
+ error
+ ^
+ """
+ .replace("Β«pathΒ»", path.toString())
+ .replace("\n", System.lineSeparator());
+ assertThat(err.toString()).isEqualTo(expected);
}
}
diff --git a/core/src/test/java/com/google/googlejavaformat/java/ModifierOrdererTest.java b/core/src/test/java/com/google/googlejavaformat/java/ModifierOrdererTest.java
index 013d14358..1cb165435 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/ModifierOrdererTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/ModifierOrdererTest.java
@@ -18,7 +18,6 @@
import static com.google.common.truth.Truth.assertThat;
-import com.google.common.base.Joiner;
import com.google.common.collect.Range;
import java.util.Arrays;
import org.junit.Test;
@@ -67,13 +66,13 @@ public void everythingIncludingDefault() throws FormatterException {
@Test
public void subRange() throws FormatterException {
- String[] lines = {
- "class Test {", //
- " static public int a;",
- " static public int b;",
- "}",
- };
- String input = Joiner.on('\n').join(lines);
+ String input =
+ """
+ class Test {
+ static public int a;
+ static public int b;
+ }\
+ """;
String substring = "static public int a";
int start = input.indexOf(substring);
int end = start + substring.length();
@@ -87,13 +86,13 @@ public void subRange() throws FormatterException {
@Test
public void whitespace() throws FormatterException {
- String[] lines = {
- "class Test {", //
- " static",
- " public int a;",
- "}",
- };
- String input = Joiner.on('\n').join(lines);
+ String input =
+ """
+ class Test {
+ static
+ public int a;
+ }\
+ """;
String substring = "static public int a";
int start = input.indexOf(substring);
int end = start + substring.length();
diff --git a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsCaseLabelsTest.java b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsCaseLabelsTest.java
index c0babb0d4..9b2257a22 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsCaseLabelsTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsCaseLabelsTest.java
@@ -16,9 +16,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.googlejavaformat.java.RemoveUnusedImports.removeUnusedImports;
-import static org.junit.Assume.assumeTrue;
-import com.google.common.base.Joiner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -28,22 +26,21 @@
public class RemoveUnusedImportsCaseLabelsTest {
@Test
public void preserveTypesInCaseLabels() throws FormatterException {
- assumeTrue(Runtime.version().feature() >= 17);
String input =
- Joiner.on('\n')
- .join(
- "package example;",
- "import example.model.SealedInterface;",
- "import example.model.TypeA;",
- "import example.model.TypeB;",
- "public class Main {",
- " public void apply(SealedInterface sealedInterface) {",
- " switch(sealedInterface) {",
- " case TypeA a -> System.out.println(\"A!\");",
- " case TypeB b -> System.out.println(\"B!\");",
- " }",
- " }",
- "}");
+ """
+ package example;
+ import example.model.SealedInterface;
+ import example.model.TypeA;
+ import example.model.TypeB;
+ public class Main {
+ public void apply(SealedInterface sealedInterface) {
+ switch(sealedInterface) {
+ case TypeA a -> System.out.println("A!");
+ case TypeB b -> System.out.println("B!");
+ }
+ }
+ }\
+ """;
assertThat(removeUnusedImports(input)).isEqualTo(input);
}
}
diff --git a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java
index d8b63ef3e..f186b846e 100644
--- a/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java
+++ b/core/src/test/java/com/google/googlejavaformat/java/RemoveUnusedImportsTest.java
@@ -29,248 +29,248 @@
public class RemoveUnusedImportsTest {
@Parameters(name = "{index}: {0}")
public static Collection