diff options
Diffstat (limited to 'core/src/test')
38 files changed, 660 insertions, 85 deletions
diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index 61a4346..cf15ecb 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -52,6 +52,15 @@ public class FormatterIntegrationTest { .putAll(15, "I603") .putAll(16, "I588") .putAll(17, "I683", "I684", "I696") + .putAll( + 21, + "SwitchGuardClause", + "SwitchRecord", + "SwitchDouble", + "SwitchUnderscore", + "I880", + "Unnamed", + "I981") .build(); @Parameters(name = "{index}: {0}") @@ -93,7 +102,7 @@ public class FormatterIntegrationTest { String expectedOutput = outputs.get(fileName); Optional<Integer> version = VERSIONED_TESTS.inverse().get(fileName).stream().collect(toOptional()); - if (version.isPresent() && Runtime.version().feature() < version.get()) { + if (Runtime.version().feature() < version.orElse(Integer.MAX_VALUE)) { continue; } testInputs.add(new Object[] {fileName, input, expectedOutput}); diff --git a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java index 15e4522..3d41a07 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java @@ -46,7 +46,7 @@ public class GoogleJavaFormatToolProviderTest { int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help"); - assertThat(result).isEqualTo(0); + assertThat(result).isNotEqualTo(0); String usage = err.toString(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolTest.java b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolTest.java new file mode 100644 index 0000000..691bb22 --- /dev/null +++ b/core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.googlejavaformat.java; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.ServiceLoader; +import javax.tools.Tool; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link GoogleJavaFormatToolProvider}. */ +@RunWith(JUnit4.class) +public class GoogleJavaFormatToolTest { + + @Test + public void testUsageOutputAfterLoadingViaToolName() { + String name = "google-java-format"; + + assertThat( + ServiceLoader.load(Tool.class).stream() + .map(ServiceLoader.Provider::get) + .map(Tool::name)) + .contains(name); + + Tool format = + ServiceLoader.load(Tool.class).stream() + .filter(provider -> name.equals(provider.get().name())) + .findFirst() + .get() + .get(); + + InputStream in = new ByteArrayInputStream(new byte[0]); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + + int result = format.run(in, out, err, "--help"); + + assertThat(result).isNotEqualTo(0); + + String usage = new String(err.toByteArray(), UTF_8); + + // Check that doc links are included. + assertThat(usage).containsMatch("http.*/google-java-format"); + assertThat(usage).contains("Usage: google-java-format"); + } +} 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 ac3eb39..42e12d8 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java @@ -54,13 +54,12 @@ public class MainTest { private static final ImmutableList<String> ADD_EXPORTS = ImmutableList.of( + "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", - "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED", "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", - "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", - "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", - "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED"); + "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"); @Test public void testUsageOutput() { @@ -132,7 +131,7 @@ public class MainTest { process.waitFor(); String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8); assertThat(err).contains("Usage: google-java-format"); - assertThat(process.exitValue()).isEqualTo(0); + assertThat(process.exitValue()).isEqualTo(2); } // end to end javadoc formatting test @@ -613,4 +612,27 @@ public class MainTest { assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0); assertThat(out.toString()).isEqualTo(joiner.join(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)); + assertThat( + new Formatter(JavaFormatterOptions.builder().reorderModifiers(false).build()) + .formatSource(source)) + .isEqualTo(source); + } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java index b1142b3..1750049 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java @@ -679,6 +679,7 @@ public final class PartialFormattingTest { String input = lines( " package com.google.googlejavaformat.java;", + "", "/*", " * Copyright 2015 Google Inc.", " *", diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index 99e1b2f..f7be369 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -15,6 +15,7 @@ package com.google.googlejavaformat.java; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assume.assumeTrue; import com.google.common.base.Joiner; import org.junit.Test; @@ -52,6 +53,26 @@ public class StringWrapperTest { assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(output); } + @Test + public void textBlock() throws Exception { + assumeTrue(Runtime.version().feature() >= 15); + String input = + lines( + "package com.mypackage;", + "public class ReproBug {", + " private String myString;", + " private ReproBug() {", + " String str =", + " \"\"\"", + " " + + " {\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint\":\"ri.some" + + "thing.1-1.object-internal.2\",\"typeId\":\"typeId\"}\"\"\";", + " myString = str;", + " }", + "}"); + assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(input); + } + private static String lines(String... line) { return Joiner.on('\n').join(line) + '\n'; } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input index 7317f17..86e46d5 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.input @@ -1,6 +1,6 @@ public class B20844369 { private static final String ID_PATTERN = - // TODO(user): add min/max lengths for the numbers here, e.g. android ID + // TODO(daw): add min/max lengths for the numbers here, e.g. android ID "(?:(?<androidId>\\d+)\\+)?" // optional Android ID + "(?<type>\\d+)" // type + ":" diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output index 62f9721..982dc2b 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output @@ -1,6 +1,6 @@ public class B20844369 { private static final String ID_PATTERN = - // TODO(user): add min/max lengths for the numbers here, e.g. android ID + // TODO(daw): add min/max lengths for the numbers here, e.g. android ID "(?:(?<androidId>\\d+)\\+)?" // optional Android ID + "(?<type>\\d+)" // type + ":" diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output index f98a37b..777303e 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21283374.output @@ -14,41 +14,41 @@ class B21283374 { { f( - /*flagA=*/ Boolean.TRUE, - /*flagB=*/ Boolean.FALSE, - /*flagC=*/ Boolean.FALSE, - /*flagD=*/ Boolean.FALSE, - /*flagE=*/ Boolean.FALSE, - /*flagF=*/ Boolean.FALSE, - /*flagG=*/ Boolean.FALSE, - /*flagH=*/ Boolean.FALSE, - /*flagI=*/ Boolean.FALSE, - /*flagJ=*/ Boolean.FALSE, - /*flagK=*/ Boolean.FALSE); + /* flagA= */ Boolean.TRUE, + /* flagB= */ Boolean.FALSE, + /* flagC= */ Boolean.FALSE, + /* flagD= */ Boolean.FALSE, + /* flagE= */ Boolean.FALSE, + /* flagF= */ Boolean.FALSE, + /* flagG= */ Boolean.FALSE, + /* flagH= */ Boolean.FALSE, + /* flagI= */ Boolean.FALSE, + /* flagJ= */ Boolean.FALSE, + /* flagK= */ Boolean.FALSE); f( - /*flagA=*/ Boolean.TRUE, - /*flagB=*/ Boolean.FALSE, - /*flagC=*/ Boolean.FALSE, - /*flagD=*/ Boolean.FALSE, - /*flagE=*/ Boolean.FALSE, - /*flagF=*/ Boolean.FALSE, - /*flagG=*/ Boolean.FALSE, - /*flagH=*/ Boolean.FALSE, - /*flagI=*/ Boolean.FALSE, - /*flagJ=*/ Boolean.FALSE, - /*flagK=*/ Boolean.FALSE); + /* flagA= */ Boolean.TRUE, + /* flagB= */ Boolean.FALSE, + /* flagC= */ Boolean.FALSE, + /* flagD= */ Boolean.FALSE, + /* flagE= */ Boolean.FALSE, + /* flagF= */ Boolean.FALSE, + /* flagG= */ Boolean.FALSE, + /* flagH= */ Boolean.FALSE, + /* flagI= */ Boolean.FALSE, + /* flagJ= */ Boolean.FALSE, + /* flagK= */ Boolean.FALSE); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); - assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); + assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something("")); - f(/*paramName=*/ false); + f(/* paramName= */ false); assertThat__________________________________________________________( - /*paramName=*/ false, thingy); + /* paramName= */ false, thingy); assertThat__________________________________________________________( - /*paramName=*/ false, thingy); + /* paramName= */ false, thingy); f( arg1, /* which arg is this attached to? */ diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output index 945a40f..aedafb7 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B26694550.output @@ -3,15 +3,15 @@ class B26694550 { /* === not a param comment */ fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), /*&=*/ - /*keepGoing=*/ false, + /* keepGoing= */ false, ggggggggggggggggggggggggggggggggggggggggggg); fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), - /*keepGoing=*/ false, + /* keepGoing= */ false, ggggggggggggggggggggggggggggggggggggggggggg); fffffffffffffffffffffffffffffff( ImmutableList.copyOf(keys), - /*foo_bar=*/ false, + /* foo_bar= */ false, /*foo-bar=*/ false, ggggggggggggggggggggggggggggggggggggggggggg); } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output index b1e33e9..3d591f8 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output @@ -6,6 +6,7 @@ enum TrailingComment { /** a */ Object a; + /** b */ Object b; } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.input deleted file mode 100644 index a38d10b..0000000 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.input +++ /dev/null @@ -1,7 +0,0 @@ -import a.A;; -import b.B; - -class Test { - A a; - B b; -} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.output deleted file mode 100644 index 8a1db80..0000000 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B28788559.output +++ /dev/null @@ -1,8 +0,0 @@ -import a.A; -; -import b.B; - -class Test { - A a; - B b; -} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input new file mode 100644 index 0000000..089a3f1 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.input @@ -0,0 +1,23 @@ +class C { + @A(0x14) + int f(Object o) { + @A(0x40) + int local; + try (@A(0x41) + JarFile jarFile = new JarFile("hello.jar")) { + } catch ( + @A(0x42) + IOException e) { + } + if (o instanceof @A(0x43) String) {} + new @A(0x44) ArrayList<>(); + Supplier<List<?>> a = @A(0x45) ArrayList::new; + Supplier<List<?>> b = @A(0x46) ImmutableList::of; + String s = (@A(0x47) String) o; + List<?> xs = new ArrayList<@A(0x48) String>(); + xs = ImmutableList.<@A(0x49) String>of(); + Supplier<List<?>> c = ArrayList<@A(0x4A) String>::new; + Supplier<List<?>> d = ImmutableList::<@A(0x4B) String>of; + return 0; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output new file mode 100644 index 0000000..089a3f1 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B308157568.output @@ -0,0 +1,23 @@ +class C { + @A(0x14) + int f(Object o) { + @A(0x40) + int local; + try (@A(0x41) + JarFile jarFile = new JarFile("hello.jar")) { + } catch ( + @A(0x42) + IOException e) { + } + if (o instanceof @A(0x43) String) {} + new @A(0x44) ArrayList<>(); + Supplier<List<?>> a = @A(0x45) ArrayList::new; + Supplier<List<?>> b = @A(0x46) ImmutableList::of; + String s = (@A(0x47) String) o; + List<?> xs = new ArrayList<@A(0x48) String>(); + xs = ImmutableList.<@A(0x49) String>of(); + Supplier<List<?>> c = ArrayList<@A(0x4A) String>::new; + Supplier<List<?>> d = ImmutableList::<@A(0x4B) String>of; + return 0; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.input deleted file mode 100644 index be76390..0000000 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.input +++ /dev/null @@ -1,6 +0,0 @@ -package foo;; - -import com.google.second.Foo; -import com.google.first.Bar; - -public class Blim {} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.output deleted file mode 100644 index 025d237..0000000 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/CL240367479.output +++ /dev/null @@ -1,6 +0,0 @@ -package foo; -; -import com.google.second.Foo; -import com.google.first.Bar; - -public class Blim {} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input index 9dc3426..d012d17 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input @@ -1,5 +1,12 @@ class Fields { + int a = 1; + int b = 1; + + int c = 1; + /** Javadoc */ + int d = 1; + int x = 1; int y = 1; diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output index 376e8b6..81ebd2a 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output @@ -1,5 +1,13 @@ class Fields { + int a = 1; + int b = 1; + + int c = 1; + + /** Javadoc */ + int d = 1; + int x = 1; int y = 1; diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.input new file mode 100644 index 0000000..dfc8a4c --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.input @@ -0,0 +1,19 @@ +class I880 { + public String f(int i) { + return switch (i) { + case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -> "looooooooooooooooooooooooooooooooooooooooong expression"; + default -> "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong expression"; + }; + } + + public boolean test(int i) { + return switch (i) { + case 0 -> // zero + false; + case 1 -> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".length() + == 0; + default -> // otherwise + true; + }; + } +}
\ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.output new file mode 100644 index 0000000..f918665 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I880.output @@ -0,0 +1,21 @@ +class I880 { + public String f(int i) { + return switch (i) { + case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -> + "looooooooooooooooooooooooooooooooooooooooong expression"; + default -> + "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong expression"; + }; + } + + public boolean test(int i) { + return switch (i) { + case 0 -> // zero + false; + case 1 -> + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".length() == 0; + default -> // otherwise + true; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input new file mode 100644 index 0000000..0660079 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +}
\ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output new file mode 100644 index 0000000..76a07f4 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.input new file mode 100644 index 0000000..bba0b72 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.input @@ -0,0 +1,12 @@ +class Foo { + private static final int X = 42; + private static final String A = STR."\{X} = \{X}"; + private static final String B = STR.""; + private static final String C = STR."\{X}"; + private static final String D = STR."\{X}\{X}"; + private static final String E = STR."\{X}\{X}\{X}"; + private static final String F = STR." \{X}"; + private static final String G = STR."\{X} "; + private static final String H = STR."\{X} one long incredibly unbroken sentence moving from "+"topic to topic so that no-one had a chance to interrupt"; + private static final String I = STR."\{X} \uD83D\uDCA9 "; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.output new file mode 100644 index 0000000..ff173fb --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I981.output @@ -0,0 +1,14 @@ +class Foo { + private static final int X = 42; + private static final String A = STR."\{X} = \{X}"; + private static final String B = STR.""; + private static final String C = STR."\{X}"; + private static final String D = STR."\{X}\{X}"; + private static final String E = STR."\{X}\{X}\{X}"; + private static final String F = STR." \{X}"; + private static final String G = STR."\{X} "; + private static final String H = + STR."\{X} one long incredibly unbroken sentence moving from " + + "topic to topic so that no-one had a chance to interrupt"; + private static final String I = STR."\{X} \uD83D\uDCA9 "; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output index dbad1bb..d28d5dd 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/M.output @@ -36,8 +36,16 @@ class M { /** JavaDoc. */ void methodName3(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 {} + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 {} /** JavaDoc. */ void methodName4( @@ -114,8 +122,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 {} + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 {} Pair< Pair<Pair<Pair<T, T>, Pair<T, T>>, Pair<Pair<T, T>, Pair<T, T>>>, @@ -135,8 +151,16 @@ class M { Pair<Pair<Pair<T, T>, Pair<T, T>>, Pair<Pair<T, T>, Pair<T, T>>>, Pair<Pair<Pair<T, T>, Pair<T, T>>, Pair<Pair<T, T>, Pair<T, T>>>> methodName11(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -225,8 +249,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -338,8 +370,16 @@ class M { T30, T31> T methodName22(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -518,8 +558,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, Exception6, - Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -640,8 +688,16 @@ class M { Pair<Pair<Pair<T, T>, Pair<T, T>>, Pair<Pair<T, T>, Pair<T, T>>>, Pair<Pair<Pair<T, T>, Pair<T, T>>, Pair<Pair<T, T>, Pair<T, T>>>> methodName(int x) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, - Exception6, Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -829,8 +885,16 @@ class M { int t, int u, int v) - throws Exception0, Exception1, Exception2, Exception3, Exception4, Exception5, - Exception6, Exception7, Exception8, Exception9 { + throws Exception0, + Exception1, + Exception2, + Exception3, + Exception4, + Exception5, + Exception6, + Exception7, + Exception8, + Exception9 { return null; } @@ -1011,6 +1075,14 @@ class M { ZZZZZZZZZZ z, ZZZZZZZZZZ z, ZZZZZZZZZZ z) - throws EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE, - EEEEEEEEEE, EEEEEEEEEE, EEEEEEEEEE {} + throws EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE, + EEEEEEEEEE {} } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input new file mode 100644 index 0000000..decebe9 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.input @@ -0,0 +1,29 @@ +package com.google.googlejavaformat.java.test; + +/** Tests for formatting of ParameterComments. */ +class Q { + static void f(int a) { + f(/*a=*/ 1); + f( + /* a= */ 1 + ); + } + static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) { + g( + /*a=*/ 1, + /*b=*/ 1, + /*c=*/ 1, + /*d=*/ 1, + /*e=*/ 1, + /*f=*/ 1, + /*g=*/ 1, + /*h=*/ 1, + /*i=*/ 1, + /*j=*/ 1, + /*k=*/ 1); + g(/*a=*/ 1, /*b=*/ 1, /*c=*/ 1, /*d=*/ 1, /*e=*/ 1, /*f=*/ 1, /*g=*/ 1, /*h=*/ 1, /*i=*/ 1); + } + static void h(Object... xs) { + h(/*xs...=*/ null); + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output new file mode 100644 index 0000000..d26ca11 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/ParameterComment.output @@ -0,0 +1,38 @@ +package com.google.googlejavaformat.java.test; + +/** Tests for formatting of ParameterComments. */ +class Q { + static void f(int a) { + f(/* a= */ 1); + f(/* a= */ 1); + } + + static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) { + g( + /* a= */ 1, + /* b= */ 1, + /* c= */ 1, + /* d= */ 1, + /* e= */ 1, + /* f= */ 1, + /* g= */ 1, + /* h= */ 1, + /* i= */ 1, + /* j= */ 1, + /* k= */ 1); + g( + /* a= */ 1, + /* b= */ 1, + /* c= */ 1, + /* d= */ 1, + /* e= */ 1, + /* f= */ 1, + /* g= */ 1, + /* h= */ 1, + /* i= */ 1); + } + + static void h(Object... xs) { + h(/* xs...= */ null); + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.input new file mode 100644 index 0000000..54e22a7 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.input @@ -0,0 +1,7 @@ +class SwitchDouble { + void x(Object o) { + switch (o) { + case null, default: + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.output new file mode 100644 index 0000000..54e22a7 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchDouble.output @@ -0,0 +1,7 @@ +class SwitchDouble { + void x(Object o) { + switch (o) { + case null, default: + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input new file mode 100644 index 0000000..25df580 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input @@ -0,0 +1,9 @@ +class SwitchGuardClause { + boolean test(Object x) { + return switch (x) { + case String s when s.length() < 5 -> true; + case Integer i -> false; + default -> true; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output new file mode 100644 index 0000000..25df580 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output @@ -0,0 +1,9 @@ +class SwitchGuardClause { + boolean test(Object x) { + return switch (x) { + case String s when s.length() < 5 -> true; + case Integer i -> false; + default -> true; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.input new file mode 100644 index 0000000..2f4fb35 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.input @@ -0,0 +1,21 @@ +record SwitchRecord(int i) { + int x(Object o) { + return switch (o) { + case SwitchRecord(int i) -> i; + default -> 0; + }; + } + int f(Object o) { + return switch (o) { + case SwitchRecord(int one, int two, int three, int four, int five, int six, int seven, int eight, int nine) -> nine; + default -> 0; + }; + } + int g(Object o) { + return switch (o) { + case SwitchRecord(int one, int two, int three, int four, int five, int six, int seven, int eight, int nine) -> + System.err.println("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); + default -> 0; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.output new file mode 100644 index 0000000..89d212f --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchRecord.output @@ -0,0 +1,46 @@ +record SwitchRecord(int i) { + int x(Object o) { + return switch (o) { + case SwitchRecord(int i) -> i; + default -> 0; + }; + } + + int f(Object o) { + return switch (o) { + case SwitchRecord( + int one, + int two, + int three, + int four, + int five, + int six, + int seven, + int eight, + int nine) -> + nine; + default -> 0; + }; + } + + int g(Object o) { + return switch (o) { + case SwitchRecord( + int one, + int two, + int three, + int four, + int five, + int six, + int seven, + int eight, + int nine) -> + System.err.println( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" + + " incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis" + + " nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo" + + " consequat."); + default -> 0; + }; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.input new file mode 100644 index 0000000..8d611d2 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.input @@ -0,0 +1,8 @@ +public class SwitchUnderscore { + void x(Object o) { + switch (o) { + case String _: + default: + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.output new file mode 100644 index 0000000..8d611d2 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchUnderscore.output @@ -0,0 +1,8 @@ +public class SwitchUnderscore { + void x(Object o) { + switch (o) { + case String _: + default: + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.input new file mode 100644 index 0000000..6c5efd1 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.input @@ -0,0 +1,44 @@ +class Unnamed { + { + int acc = 0; + for (Order _ : orders) { + if (acc < LIMIT) { + acc++; + } + } + + for (int i = 0, _ = sideEffect(); i < 10; i++) { } + + Queue<Integer> q = null; + while (q.size() >= 3) { + var x = q.remove(); + var y = q.remove(); + var _ = q.remove(); + new Point(x, y); + } + + while (q.size() >= 3) { + var x = q.remove(); + var _ = q.remove(); + var _ = q.remove(); + new Point(x, 0) ; + } + + String s = null; + try { + int i = Integer.parseInt(s); + } catch (NumberFormatException _) { + System.out.println("Bad number: " + s); + } + + try { doSomething(); } + catch (Exception _) { doSomething(); } + catch (Throwable _) { doSomething(); } + + try (var _ = ScopedContext.acquire()) { + doSomething(); + } + + stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA")); + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.output new file mode 100644 index 0000000..84d8f87 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/Unnamed.output @@ -0,0 +1,48 @@ +class Unnamed { + { + int acc = 0; + for (Order _ : orders) { + if (acc < LIMIT) { + acc++; + } + } + + for (int i = 0, _ = sideEffect(); i < 10; i++) {} + + Queue<Integer> q = null; + while (q.size() >= 3) { + var x = q.remove(); + var y = q.remove(); + var _ = q.remove(); + new Point(x, y); + } + + while (q.size() >= 3) { + var x = q.remove(); + var _ = q.remove(); + var _ = q.remove(); + new Point(x, 0); + } + + String s = null; + try { + int i = Integer.parseInt(s); + } catch (NumberFormatException _) { + System.out.println("Bad number: " + s); + } + + try { + doSomething(); + } catch (Exception _) { + doSomething(); + } catch (Throwable _) { + doSomething(); + } + + try (var _ = ScopedContext.acquire()) { + doSomething(); + } + + stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA")); + } +} |