Skip to content

Commit 3dc82da

Browse files
committed
Make StringConcatToTextBlock an error
The Java sources that DiagnosticTest and FormatterTest feed the formatter were strings joined by + or by Joiner.on('\n'). They are text blocks now, ending in \ where the source had no final newline, and Joiner is no longer imported.
1 parent 1bf5401 commit 3dc82da

3 files changed

Lines changed: 86 additions & 50 deletions

File tree

‎buildSrc/src/main/groovy/open-java-format.error-prone-conventions.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tasks.withType(JavaCompile).configureEach {
3434
error('RedundantStringConversion')
3535
error('RefactorSwitch')
3636
error('SameNameButDifferent')
37+
error('StringConcatToTextBlock')
3738
error('UnusedVariable')
3839
disable('IsInstanceLambdaUsage')
3940
disable('LexicographicalAnnotationAttributeListing')

‎open-java-format/src/test/java/com/palantir/javaformat/java/DiagnosticTest.java‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static java.nio.charset.StandardCharsets.UTF_8;
1919

20-
import com.google.common.base.Joiner;
2120
import java.io.ByteArrayInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
@@ -53,19 +52,19 @@ void restoreLocale() throws Exception {
5352

5453
@Test
5554
void parseError() throws Exception {
56-
String input = Joiner.on('\n')
57-
.join(
58-
"public class InvalidSyntax {",
59-
" private static NumPrinter {",
60-
" public static void print(int n) {",
61-
" System.out.printf(\"%d%n\", n);",
62-
" }",
63-
" }",
64-
"",
65-
" public static void main(String[] args) {",
66-
" NumPrinter.print(args.length);",
67-
" }",
68-
"}");
55+
String input = """
56+
public class InvalidSyntax {
57+
private static NumPrinter {
58+
public static void print(int n) {
59+
System.out.printf("%d%n", n);
60+
}
61+
}
62+
63+
public static void main(String[] args) {
64+
NumPrinter.print(args.length);
65+
}
66+
}\
67+
""";
6968

7069
StringWriter stdout = new StringWriter();
7170
StringWriter stderr = new StringWriter();

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2323
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
2424

25-
import com.google.common.base.Joiner;
2625
import com.google.common.collect.Range;
2726
import com.google.common.io.CharStreams;
2827
import java.io.ByteArrayInputStream;
@@ -70,8 +69,18 @@ void formatNonJavaFiles() throws Exception {
7069

7170
@Test
7271
void formatStdinStdoutWithDashFlag() throws Exception {
73-
String input = "class Foo{\n" + "void f\n" + "() {\n" + "}\n" + "}\n";
74-
String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n";
72+
String input = """
73+
class Foo{
74+
void f
75+
() {
76+
}
77+
}
78+
""";
79+
String expectedOutput = """
80+
class Foo {
81+
void f() {}
82+
}
83+
""";
7584

7685
InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
7786
StringWriter out = new StringWriter();
@@ -89,8 +98,23 @@ void formatStdinStdoutWithDashFlag() throws Exception {
8998

9099
@Test
91100
void formatLengthUpToEOF() throws Exception {
92-
String input = "class Foo{\n" + "void f\n" + "() {\n" + "}\n" + "}\n\n\n\n\n\n";
93-
String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n";
101+
String input = """
102+
class Foo{
103+
void f
104+
() {
105+
}
106+
}
107+
108+
109+
110+
111+
112+
""";
113+
String expectedOutput = """
114+
class Foo {
115+
void f() {}
116+
}
117+
""";
94118

95119
Path path = testFolder.resolve("Foo.java");
96120
Files.write(path, input.getBytes(UTF_8));
@@ -128,19 +152,19 @@ void blankInClassBodyNoTrailing() throws FormatterException {
128152
assertThat(output).isEqualTo(expect);
129153
}
130154

131-
private static final String UNORDERED_IMPORTS = Joiner.on('\n')
132-
.join(
133-
"import com.google.common.base.Preconditions;",
134-
"",
135-
"import static org.junit.Assert.fail;",
136-
"import static com.google.truth.Truth.assertThat;",
137-
"",
138-
"import org.junit.runners.JUnit4;",
139-
"import org.junit.runner.RunWith;",
140-
"",
141-
"import java.util.List;",
142-
"",
143-
"import javax.annotations.Nullable;");
155+
private static final String UNORDERED_IMPORTS = """
156+
import com.google.common.base.Preconditions;
157+
158+
import static org.junit.Assert.fail;
159+
import static com.google.truth.Truth.assertThat;
160+
161+
import org.junit.runners.JUnit4;
162+
import org.junit.runner.RunWith;
163+
164+
import java.util.List;
165+
166+
import javax.annotations.Nullable;\
167+
""";
144168

145169
@Test
146170
void importsNotReorderedByDefault() throws FormatterException {
@@ -217,17 +241,27 @@ void throwsFormatterException() throws Exception {
217241

218242
@Test
219243
void blankLinesImportComment() throws FormatterException {
220-
String withBlank = "package p;\n"
221-
+ "\n"
222-
+ "/** test */\n"
223-
+ "\n"
224-
+ "import a.A;\n"
225-
+ "\n"
226-
+ "class T {\n"
227-
+ " A a;\n"
228-
+ "}\n";
229-
String withoutBlank =
230-
"package p;\n" + "\n" + "/** test */\n" + "import a.A;\n" + "\n" + "class T {\n" + " A a;\n" + "}\n";
244+
String withBlank = """
245+
package p;
246+
247+
/** test */
248+
249+
import a.A;
250+
251+
class T {
252+
A a;
253+
}
254+
""";
255+
String withoutBlank = """
256+
package p;
257+
258+
/** test */
259+
import a.A;
260+
261+
class T {
262+
A a;
263+
}
264+
""";
231265

232266
// Formatting deletes the blank line between the "javadoc" and the first import.
233267
assertThat(FORMATTER.formatSource(withBlank)).isEqualTo(withoutBlank);
@@ -254,13 +288,15 @@ void canParse_java9_private_interface_methods() {
254288
@Test
255289
void producesFormattingChangesOnAlreadyFormattedFiles() throws FormatterException {
256290
Formatter formatter = FORMATTER;
257-
String simpleClass = "package com.palantir;\n"
258-
+ "\n"
259-
+ "public final class Formatted {\n"
260-
+ " public static int count(int number) {\n"
261-
+ " return number;\n"
262-
+ " }\n"
263-
+ "}";
291+
String simpleClass = """
292+
package com.palantir;
293+
294+
public final class Formatted {
295+
public static int count(int number) {
296+
return number;
297+
}
298+
}\
299+
""";
264300
// This is kinda overkill but the point of the test is to show that it
265301
// is for sure formatted and still producing a replacement.
266302
String formattedClass = formatter.formatSourceAndFixImports(simpleClass);

0 commit comments

Comments
 (0)