FormatTestJava.cpp
20.9 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
class FormatTestJava : public ::testing::Test {
protected:
static std::string format(llvm::StringRef Code, unsigned Offset,
unsigned Length, const FormatStyle &Style) {
LLVM_DEBUG(llvm::errs() << "---\n");
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
auto Result = applyAllReplacements(Code, Replaces);
EXPECT_TRUE(static_cast<bool>(Result));
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
return *Result;
}
static std::string
format(llvm::StringRef Code,
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
return format(Code, 0, Code.size(), Style);
}
static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
Style.ColumnLimit = ColumnLimit;
return Style;
}
static void verifyFormat(
llvm::StringRef Code,
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
};
TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
verifyFormat("someObject.and();");
}
TEST_F(FormatTestJava, UnderstandsCasts) {
verifyFormat("a[b >> 1] = (byte) (c() << 4);");
}
TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) {
FormatStyle Style = getStyleWithColumns(50);
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
Style);
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
Style);
verifyFormat("return aaaaaaaaaaaaaaaaaaa instanceof bbbbbbbbbbbbbbbbbbbbbbb\n"
" && ccccccccccccccccccc instanceof dddddddddddddddddddddd;");
}
TEST_F(FormatTestJava, Chromium) {
verifyFormat("class SomeClass {\n"
" void f() {}\n"
" int g() {\n"
" return 0;\n"
" }\n"
" void h() {\n"
" while (true) f();\n"
" for (;;) f();\n"
" if (true) f();\n"
" }\n"
"}",
getChromiumStyle(FormatStyle::LK_Java));
}
TEST_F(FormatTestJava, QualifiedNames) {
verifyFormat("public some.package.Type someFunction( // comment\n"
" int parameter) {}");
}
TEST_F(FormatTestJava, ClassKeyword) {
verifyFormat("SomeClass.class.getName();");
verifyFormat("Class c = SomeClass.class;");
}
TEST_F(FormatTestJava, ClassDeclarations) {
verifyFormat("public class SomeClass {\n"
" private int a;\n"
" private int b;\n"
"}");
verifyFormat("public class A {\n"
" class B {\n"
" int i;\n"
" }\n"
" class C {\n"
" int j;\n"
" }\n"
"}");
verifyFormat("public class A extends B.C {}");
verifyFormat("abstract class SomeClass\n"
" extends SomeOtherClass implements SomeInterface {}",
getStyleWithColumns(60));
verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
" implements SomeInterfaceeeeeeeeeeeee {}",
getStyleWithColumns(60));
verifyFormat("abstract class SomeClass\n"
" extends SomeOtherClass\n"
" implements SomeInterface {}",
getStyleWithColumns(40));
verifyFormat("abstract class SomeClass\n"
" extends SomeOtherClass\n"
" implements SomeInterface,\n"
" AnotherInterface {}",
getStyleWithColumns(40));
verifyFormat("abstract class SomeClass\n"
" implements SomeInterface, AnotherInterface {}",
getStyleWithColumns(60));
verifyFormat("@SomeAnnotation()\n"
"abstract class aaaaaaaaaaaa\n"
" extends bbbbbbbbbbbbbbb implements cccccccccccc {}",
getStyleWithColumns(76));
verifyFormat("@SomeAnnotation()\n"
"abstract class aaaaaaaaa<a>\n"
" extends bbbbbbbbbbbb<b> implements cccccccccccc {}",
getStyleWithColumns(76));
verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
" void doStuff(int theStuff);\n"
" void doMoreStuff(int moreStuff);\n"
"}");
verifyFormat("public interface SomeInterface {\n"
" void doStuff(int theStuff);\n"
" void doMoreStuff(int moreStuff);\n"
" default void doStuffWithDefault() {}\n"
"}");
verifyFormat("@interface SomeInterface {\n"
" void doStuff(int theStuff);\n"
" void doMoreStuff(int moreStuff);\n"
"}");
verifyFormat("public @interface SomeInterface {\n"
" void doStuff(int theStuff);\n"
" void doMoreStuff(int moreStuff);\n"
"}");
verifyFormat("class A {\n"
" public @interface SomeInterface {\n"
" int stuff;\n"
" void doMoreStuff(int moreStuff);\n"
" }\n"
"}");
verifyFormat("class A {\n"
" public @interface SomeInterface {}\n"
"}");
}
TEST_F(FormatTestJava, AnonymousClasses) {
verifyFormat("return new A() {\n"
" public String toString() {\n"
" return \"NotReallyA\";\n"
" }\n"
"};");
verifyFormat("A a = new A() {\n"
" public String toString() {\n"
" return \"NotReallyA\";\n"
" }\n"
"};");
}
TEST_F(FormatTestJava, EnumDeclarations) {
verifyFormat("enum SomeThing { ABC, CDE }");
verifyFormat("enum SomeThing {\n"
" ABC,\n"
" CDE,\n"
"}");
verifyFormat("public class SomeClass {\n"
" enum SomeThing { ABC, CDE }\n"
" void f() {}\n"
"}");
verifyFormat("public class SomeClass implements SomeInterface {\n"
" enum SomeThing { ABC, CDE }\n"
" void f() {}\n"
"}");
verifyFormat("enum SomeThing {\n"
" ABC,\n"
" CDE;\n"
" void f() {}\n"
"}");
verifyFormat("enum SomeThing {\n"
" ABC(1, \"ABC\"),\n"
" CDE(2, \"CDE\");\n"
" Something(int i, String s) {}\n"
"}");
verifyFormat("enum SomeThing {\n"
" ABC(new int[] {1, 2}),\n"
" CDE(new int[] {2, 3});\n"
" Something(int[] i) {}\n"
"}");
verifyFormat("public enum SomeThing {\n"
" ABC {\n"
" public String toString() {\n"
" return \"ABC\";\n"
" }\n"
" },\n"
" CDE {\n"
" @Override\n"
" public String toString() {\n"
" return \"CDE\";\n"
" }\n"
" };\n"
" public void f() {}\n"
"}");
verifyFormat("private enum SomeEnum implements Foo<?, B> {\n"
" ABC {\n"
" @Override\n"
" public String toString() {\n"
" return \"ABC\";\n"
" }\n"
" },\n"
" CDE {\n"
" @Override\n"
" public String toString() {\n"
" return \"CDE\";\n"
" }\n"
" };\n"
"}");
verifyFormat("public enum VeryLongEnum {\n"
" ENUM_WITH_MANY_PARAMETERS(\n"
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"bbbbbbbbbbbbbbbb\", "
"\"cccccccccccccccccccccccc\"),\n"
" SECOND_ENUM(\"a\", \"b\", \"c\");\n"
" private VeryLongEnum(String a, String b, String c) {}\n"
"}\n");
}
TEST_F(FormatTestJava, ArrayInitializers) {
verifyFormat("new int[] {1, 2, 3, 4};");
verifyFormat("new int[] {\n"
" 1,\n"
" 2,\n"
" 3,\n"
" 4,\n"
"};");
FormatStyle Style = getStyleWithColumns(65);
Style.Cpp11BracedListStyle = false;
verifyFormat(
"expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n"
" 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };",
Style);
}
TEST_F(FormatTestJava, ThrowsDeclarations) {
verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
" throws LooooooooooooooooooooooooooooongException {}");
verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
" throws LoooooooooongException, LooooooooooongException {}");
}
TEST_F(FormatTestJava, Annotations) {
verifyFormat("@Override\n"
"public String toString() {}");
verifyFormat("@Override\n"
"@Nullable\n"
"public String getNameIfPresent() {}");
verifyFormat("@Override // comment\n"
"@Nullable\n"
"public String getNameIfPresent() {}");
verifyFormat("@java.lang.Override // comment\n"
"@Nullable\n"
"public String getNameIfPresent() {}");
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
"public void doSomething() {}");
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
"@Author(name = \"abc\")\n"
"public void doSomething() {}");
verifyFormat("DoSomething(new A() {\n"
" @Override\n"
" public String toString() {}\n"
"});");
verifyFormat("void SomeFunction(@Nullable String something) {}");
verifyFormat("void SomeFunction(@org.llvm.Nullable String something) {}");
verifyFormat("@Partial @Mock DataLoader loader;");
verifyFormat("@Partial\n"
"@Mock\n"
"DataLoader loader;",
getChromiumStyle(FormatStyle::LK_Java));
verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
"public static int iiiiiiiiiiiiiiiiiiiiiiii;");
verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
"private static final long something = 0L;");
verifyFormat("@org.llvm.Qualified(\"With some really looooooooooong text\")\n"
"private static final long something = 0L;");
verifyFormat("@Mock\n"
"DataLoader loooooooooooooooooooooooader =\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getStyleWithColumns(60));
verifyFormat("@org.llvm.QualifiedMock\n"
"DataLoader loooooooooooooooooooooooader =\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getStyleWithColumns(60));
verifyFormat("@Test(a)\n"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("@SomeAnnotation(\n"
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa)\n"
"int i;",
getStyleWithColumns(50));
verifyFormat("@Test\n"
"ReturnType doSomething(\n"
" String aaaaaaaaaaaaa, String bbbbbbbbbbbbbbb) {}",
getStyleWithColumns(60));
verifyFormat("{\n"
" boolean someFunction(\n"
" @Param(aaaaaaaaaaaaaaaa) String aaaaa,\n"
" String bbbbbbbbbbbbbbb) {}\n"
"}",
getStyleWithColumns(60));
verifyFormat("@Annotation(\"Some\"\n"
" + \" text\")\n"
"List<Integer> list;");
verifyFormat(
"@Test\n"
"@Feature({\"Android-TabSwitcher\"})\n"
"@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})\n"
"@Features.EnableFeatures({FEATURE})\n"
"public void test(@Foo.bar(\"baz\") @Quux.Qoob int theFirstParaaaaam,\n"
" @Foo.bar(\"baz\") @Quux.Qoob int theSecondParaaaaaaaaaaaaaaaam) {}");
}
TEST_F(FormatTestJava, Generics) {
verifyFormat("Iterable<?> a;");
verifyFormat("Iterable<?> a;");
verifyFormat("Iterable<? extends SomeObject> a;");
verifyFormat("A.<B>doSomething();");
verifyFormat("A.<B<C>>doSomething();");
verifyFormat("A.<B<C<D>>>doSomething();");
verifyFormat("A.<B<C<D<E>>>>doSomething();");
verifyFormat("OrderedPair<String, List<Box<Integer>>> p = null;");
verifyFormat("@Override\n"
"public Map<String, ?> getAll() {}");
verifyFormat("public <R> ArrayList<R> get() {}");
verifyFormat("protected <R> ArrayList<R> get() {}");
verifyFormat("private <R> ArrayList<R> get() {}");
verifyFormat("public static <R> ArrayList<R> get() {}");
verifyFormat("public static native <R> ArrayList<R> get();");
verifyFormat("public final <X> Foo foo() {}");
verifyFormat("public abstract <X> Foo foo();");
verifyFormat("<T extends B> T getInstance(Class<T> type);");
verifyFormat("Function<F, ? extends T> function;");
verifyFormat("private Foo<X, Y>[] foos;");
verifyFormat("Foo<X, Y>[] foos = this.foos;");
verifyFormat("return (a instanceof List<?>)\n"
" ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
" : aaaaaaaaaaaaaaaaaaaaaaa;",
getStyleWithColumns(60));
verifyFormat(
"SomeLoooooooooooooooooooooongType name =\n"
" SomeType.foo(someArgument)\n"
" .<X>method()\n"
" .aaaaaaaaaaaaaaaaaaa()\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
}
TEST_F(FormatTestJava, StringConcatenation) {
verifyFormat("String someString = \"abc\"\n"
" + \"cde\";");
}
TEST_F(FormatTestJava, TryCatchFinally) {
verifyFormat("try {\n"
" Something();\n"
"} catch (SomeException e) {\n"
" HandleException(e);\n"
"}");
verifyFormat("try {\n"
" Something();\n"
"} finally {\n"
" AlwaysDoThis();\n"
"}");
verifyFormat("try {\n"
" Something();\n"
"} catch (SomeException e) {\n"
" HandleException(e);\n"
"} finally {\n"
" AlwaysDoThis();\n"
"}");
verifyFormat("try {\n"
" Something();\n"
"} catch (SomeException | OtherException e) {\n"
" HandleException(e);\n"
"}");
}
TEST_F(FormatTestJava, TryWithResources) {
verifyFormat("try (SomeResource rs = someFunction()) {\n"
" Something();\n"
"}");
verifyFormat("try (SomeResource rs = someFunction()) {\n"
" Something();\n"
"} catch (SomeException e) {\n"
" HandleException(e);\n"
"}");
}
TEST_F(FormatTestJava, SynchronizedKeyword) {
verifyFormat("synchronized (mData) {\n"
" // ...\n"
"}");
}
TEST_F(FormatTestJava, AssertKeyword) {
verifyFormat("assert a && b;");
verifyFormat("assert (a && b);");
}
TEST_F(FormatTestJava, PackageDeclarations) {
verifyFormat("package some.really.loooooooooooooooooooooong.package;",
getStyleWithColumns(50));
}
TEST_F(FormatTestJava, ImportDeclarations) {
verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
getStyleWithColumns(50));
verifyFormat("import static some.really.looooooooooooooooong.imported.Class;",
getStyleWithColumns(50));
}
TEST_F(FormatTestJava, MethodDeclarations) {
verifyFormat("void methodName(Object arg1,\n"
" Object arg2, Object arg3) {}",
getStyleWithColumns(40));
verifyFormat("void methodName(\n"
" Object arg1, Object arg2) {}",
getStyleWithColumns(40));
}
TEST_F(FormatTestJava, MethodReference) {
EXPECT_EQ(
"private void foo() {\n"
" f(this::methodReference);\n"
" f(C.super::methodReference);\n"
" Consumer<String> c = System.out::println;\n"
" Iface<Integer> mRef = Ty::<Integer>meth;\n"
"}",
format("private void foo() {\n"
" f(this ::methodReference);\n"
" f(C.super ::methodReference);\n"
" Consumer<String> c = System.out ::println;\n"
" Iface<Integer> mRef = Ty :: <Integer> meth;\n"
"}"));
}
TEST_F(FormatTestJava, CppKeywords) {
verifyFormat("public void union(Type a, Type b);");
verifyFormat("public void struct(Object o);");
verifyFormat("public void delete(Object o);");
verifyFormat("return operator && (aa);");
}
TEST_F(FormatTestJava, NeverAlignAfterReturn) {
verifyFormat("return aaaaaaaaaaaaaaaaaaa\n"
" && bbbbbbbbbbbbbbbbbbb\n"
" && ccccccccccccccccccc;",
getStyleWithColumns(40));
verifyFormat("return (result == null)\n"
" ? aaaaaaaaaaaaaaaaa\n"
" : bbbbbbbbbbbbbbbbb;",
getStyleWithColumns(40));
verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
" .bbbbbbbbbbbbbbbbbbb()\n"
" .ccccccccccccccccccc();",
getStyleWithColumns(40));
verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
" .bbbbbbbbbbbbbbbbbbb(\n"
" ccccccccccccccc)\n"
" .ccccccccccccccccccc();",
getStyleWithColumns(40));
}
TEST_F(FormatTestJava, FormatsInnerBlocks) {
verifyFormat("someObject.someFunction(new Runnable() {\n"
" @Override\n"
" public void run() {\n"
" System.out.println(42);\n"
" }\n"
"}, someOtherParameter);");
verifyFormat("someFunction(new Runnable() {\n"
" public void run() {\n"
" System.out.println(42);\n"
" }\n"
"});");
verifyFormat("someObject.someFunction(\n"
" new Runnable() {\n"
" @Override\n"
" public void run() {\n"
" System.out.println(42);\n"
" }\n"
" },\n"
" new Runnable() {\n"
" @Override\n"
" public void run() {\n"
" System.out.println(43);\n"
" }\n"
" },\n"
" someOtherParameter);");
}
TEST_F(FormatTestJava, FormatsLambdas) {
verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
" -> aaaaaaaaaa + bbbbbbbbbb;",
getStyleWithColumns(40));
verifyFormat("Runnable someLambda = () -> DoSomething();");
verifyFormat("Runnable someLambda = () -> {\n"
" DoSomething();\n"
"}");
verifyFormat("Runnable someLambda =\n"
" (int aaaaa) -> DoSomething(aaaaa);",
getStyleWithColumns(40));
}
TEST_F(FormatTestJava, BreaksStringLiterals) {
// FIXME: String literal breaking is currently disabled for Java and JS, as it
// requires strings to be merged using "+" which we don't support.
EXPECT_EQ("\"some text other\";",
format("\"some text other\";", getStyleWithColumns(14)));
}
TEST_F(FormatTestJava, AlignsBlockComments) {
EXPECT_EQ("/*\n"
" * Really multi-line\n"
" * comment.\n"
" */\n"
"void f() {}",
format(" /*\n"
" * Really multi-line\n"
" * comment.\n"
" */\n"
" void f() {}"));
}
TEST_F(FormatTestJava, KeepsDelimitersOnOwnLineInJavaDocComments) {
EXPECT_EQ("/**\n"
" * javadoc line 1\n"
" * javadoc line 2\n"
" */",
format("/** javadoc line 1\n"
" * javadoc line 2 */"));
}
TEST_F(FormatTestJava, RetainsLogicalShifts) {
verifyFormat("void f() {\n"
" int a = 1;\n"
" a >>>= 1;\n"
"}");
verifyFormat("void f() {\n"
" int a = 1;\n"
" a = a >>> 1;\n"
"}");
}
} // end namespace tooling
} // end namespace clang