-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PrettyPrintVisitorTest.java
374 lines (325 loc) · 13.4 KB
/
PrettyPrintVisitorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.printer;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.CastExpr;
import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.type.Type;
import org.junit.jupiter.api.Test;
import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol;
import static com.github.javaparser.utils.Utils.EOL;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PrettyPrintVisitorTest {
@Test
void getMaximumCommonTypeWithoutAnnotations() {
VariableDeclarationExpr vde1 = JavaParser.parseVariableDeclarationExpr("int a[], b[]");
assertEquals("int[]", vde1.getMaximumCommonType().get().toString());
VariableDeclarationExpr vde2 = JavaParser.parseVariableDeclarationExpr("int[][] a[], b[]");
assertEquals("int[][][]", vde2.getMaximumCommonType().get().toString());
VariableDeclarationExpr vde3 = JavaParser.parseVariableDeclarationExpr("int[][] a, b[]");
assertEquals("int[][]", vde3.getMaximumCommonType().get().toString());
}
@Test
void getMaximumCommonTypeWithAnnotations() {
VariableDeclarationExpr vde1 = JavaParser.parseVariableDeclarationExpr("int a @Foo [], b[]");
assertEquals("int", vde1.getMaximumCommonType().get().toString());
VariableDeclarationExpr vde2 = JavaParser.parseVariableDeclarationExpr("int[]@Foo [] a[], b[]");
assertEquals("int[] @Foo [][]", vde2.getMaximumCommonType().get().toString());
}
private String print(Node node) {
return new PrettyPrinter().print(node);
}
@Test
void printSimpleClassExpr() {
ClassExpr expr = JavaParser.parseExpression("Foo.class");
assertEquals("Foo.class", print(expr));
}
@Test
void printArrayClassExpr() {
ClassExpr expr = JavaParser.parseExpression("Foo[].class");
assertEquals("Foo[].class", print(expr));
}
@Test
void printGenericClassExpr() {
ClassExpr expr = JavaParser.parseExpression("Foo<String>.class");
assertEquals("Foo<String>.class", print(expr));
}
@Test
void printSimplestClass() {
Node node = JavaParser.parse("class A {}");
assertEquals("class A {" + EOL +
"}" + EOL, print(node));
}
@Test
void printAClassWithField() {
Node node = JavaParser.parse("class A { int a; }");
assertEquals("class A {" + EOL
+ EOL +
" int a;" + EOL +
"}" + EOL, print(node));
}
@Test
void printAReceiverParameter() {
Node node = JavaParser.parseBodyDeclaration("int x(@O X A.B.this, int y) { }");
assertEquals("int x(@O X A.B.this, int y) {" + EOL + "}", print(node));
}
@Test
void printLambdaIntersectionTypeAssignment() {
String code = "class A {" + EOL +
" void f() {" + EOL +
" Runnable r = (Runnable & Serializable) (() -> {});" + EOL +
" r = (Runnable & Serializable)() -> {};" + EOL +
" r = (Runnable & I)() -> {};" + EOL +
" }}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = (MethodDeclaration) cu.getType(0).getMember(0);
assertEquals("Runnable r = (Runnable & Serializable) (() -> {" + EOL + "});", print(methodDeclaration.getBody().get().getStatements().get(0)));
}
@Test
void printIntersectionType() {
String code = "(Runnable & Serializable) (() -> {})";
Expression expression = JavaParser.parseExpression(code);
Type type = ((CastExpr) expression).getType();
assertEquals("Runnable & Serializable", print(type));
}
@Test
void printLambdaIntersectionTypeReturn() {
String code = "class A {" + EOL
+ " Object f() {" + EOL
+ " return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL
+ "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = (MethodDeclaration) cu.getType(0).getMember(0);
assertEquals("return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey());", print(methodDeclaration.getBody().get().getStatements().get(0)));
}
@Test
void printClassWithoutJavaDocButWithComment() {
String code = String.format("/** javadoc */ public class A { %s// stuff%s}", EOL, EOL);
CompilationUnit cu = JavaParser.parse(code);
PrettyPrinterConfiguration ignoreJavaDoc = new PrettyPrinterConfiguration().setPrintJavadoc(false);
String content = cu.toString(ignoreJavaDoc);
assertEquals(String.format("public class A {%s // stuff%s}%s", EOL, EOL, EOL), content);
}
@Test
void printImportsDefaultOrder() {
String code = "import x.y.z;import a.b.c;import static b.c.d;class c {}";
CompilationUnit cu = JavaParser.parse(code);
String content = cu.toString();
assertEqualsNoEol("import x.y.z;\n" +
"import a.b.c;\n" +
"import static b.c.d;\n" +
"\n" +
"class c {\n" +
"}\n", content);
}
@Test
void printImportsOrdered() {
String code = "import x.y.z;import a.b.c;import static b.c.d;class c {}";
CompilationUnit cu = JavaParser.parse(code);
PrettyPrinterConfiguration orderImports = new PrettyPrinterConfiguration().setOrderImports(true);
String content = cu.toString(orderImports);
assertEqualsNoEol("import static b.c.d;\n" +
"import a.b.c;\n" +
"import x.y.z;\n" +
"\n" +
"class c {\n" +
"}\n", content);
}
@Test
void multilineJavadocGetsFormatted() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("line1\n line2 *\n * line3");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" * line1\n" +
" * line2 *\n" +
" * line3\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void emptyJavadocGetsFormatted() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void multilineJavadocWithLotsOfEmptyLinesGetsFormattedNeatly() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("\n\n\n ab\n\n\n cd\n\n\n");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" * ab\n" +
" *\n" +
" * cd\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void singlelineJavadocGetsFormatted() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("line1");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" * line1\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void javadocAlwaysGetsASpaceBetweenTheAsteriskAndTheRestOfTheLine() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("line1\nline2");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" * line1\n" +
" * line2\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void javadocAlwaysGetsAnAdditionalSpaceOrNeverGetsIt() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setJavadocComment("line1\n" +
"line2\n" +
" 3");
assertEqualsNoEol("public class X {\n" +
"\n" +
" /**\n" +
" * line1\n" +
" * line2\n" +
" * 3\n" +
" */\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void singlelineCommentGetsFormatted() {
CompilationUnit cu = new CompilationUnit();
cu.addClass("X").addMethod("abc").setComment(new LineComment(" line1 \n "));
assertEqualsNoEol("public class X {\n" +
"\n" +
" // line1\n" +
" void abc() {\n" +
" }\n" +
"}\n", cu.toString());
}
@Test
void blockcommentGetsNoFormatting() {
CompilationUnit cu = JavaParser.parse("class A {\n" +
" public void helloWorld(String greeting, String name) {\n" +
" //sdfsdfsdf\n" +
" //sdfds\n" +
" /*\n" +
" dgfdgfdgfdgfdgfd\n" +
" */\n" +
" }\n" +
"}\n");
assertEqualsNoEol("class A {\n" +
"\n" +
" public void helloWorld(String greeting, String name) {\n" +
" // sdfsdfsdf\n" +
" // sdfds\n" +
" /*\n" +
" dgfdgfdgfdgfdgfd\n" +
" */\n" +
" }\n" +
"}\n", cu.toString());
}
private String expected = "public class SomeClass {\n" +
"\n" +
" /**\n" +
" * tester line\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" */\n" +
" public void add(int x, int y) {\n" +
" }\n" +
"}\n";
@Test
void javadocIssue1907_allLeadingSpaces() {
String input_allLeadingSpaces = "public class SomeClass{" +
"/**\n" +
" * tester line\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" multi line comment\n" +
" */\n" +
"public void add(int x, int y){}}";
CompilationUnit cu_allLeadingSpaces = JavaParser.parse(input_allLeadingSpaces);
assertEqualsNoEol(expected, cu_allLeadingSpaces.toString());
}
@Test
void javadocIssue1907_singleMissingLeadingSpace() {
String input_singleMissingLeadingSpace = "public class SomeClass{" +
"/**\n" +
"* tester line\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" multi line comment\n" +
" */\n" +
"public void add(int x, int y){}}";
CompilationUnit cu_singleMissingLeadingSpace = JavaParser.parse(input_singleMissingLeadingSpace);
assertEqualsNoEol(expected, cu_singleMissingLeadingSpace.toString());
}
@Test
void javadocIssue1907_leadingTab() {
String input_leadingTab = "public class SomeClass{" +
"/**\n" +
"\t * tester line\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" * multi line comment\n" +
" multi line comment\n" +
" */\n" +
"public void add(int x, int y){}}";
CompilationUnit cu_leadingTab = JavaParser.parse(input_leadingTab);
assertEqualsNoEol(expected, cu_leadingTab.toString());
}
}