• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTestJava : public ::testing::Test {
21 protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)22   static std::string format(llvm::StringRef Code, unsigned Offset,
23                             unsigned Length, const FormatStyle &Style) {
24     DEBUG(llvm::errs() << "---\n");
25     DEBUG(llvm::errs() << Code << "\n\n");
26     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28     std::string Result = applyAllReplacements(Code, Replaces);
29     EXPECT_NE("", Result);
30     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31     return Result;
32   }
33 
format(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_Java))34   static std::string format(
35       llvm::StringRef Code,
36       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
37     return format(Code, 0, Code.size(), Style);
38   }
39 
getStyleWithColumns(unsigned ColumnLimit)40   static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
41     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
42     Style.ColumnLimit = ColumnLimit;
43     return Style;
44   }
45 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_Java))46   static void verifyFormat(
47       llvm::StringRef Code,
48       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
49     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50   }
51 };
52 
TEST_F(FormatTestJava,NoAlternativeOperatorNames)53 TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
54   verifyFormat("someObject.and();");
55 }
56 
TEST_F(FormatTestJava,UnderstandsCasts)57 TEST_F(FormatTestJava, UnderstandsCasts) {
58   verifyFormat("a[b >> 1] = (byte) (c() << 4);");
59 }
60 
TEST_F(FormatTestJava,FormatsInstanceOfLikeOperators)61 TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) {
62   FormatStyle Style = getStyleWithColumns(50);
63   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
64                "    instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
65                Style);
66   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
67   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n"
68                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
69                Style);
70 }
71 
TEST_F(FormatTestJava,Chromium)72 TEST_F(FormatTestJava, Chromium) {
73   verifyFormat("class SomeClass {\n"
74                "    void f() {}\n"
75                "    int g() {\n"
76                "        return 0;\n"
77                "    }\n"
78                "    void h() {\n"
79                "        while (true) f();\n"
80                "        for (;;) f();\n"
81                "        if (true) f();\n"
82                "    }\n"
83                "}",
84                getChromiumStyle(FormatStyle::LK_Java));
85 }
86 
TEST_F(FormatTestJava,QualifiedNames)87 TEST_F(FormatTestJava, QualifiedNames) {
88   verifyFormat("public some.package.Type someFunction( // comment\n"
89                "    int parameter) {}");
90 }
91 
TEST_F(FormatTestJava,ClassKeyword)92 TEST_F(FormatTestJava, ClassKeyword) {
93   verifyFormat("SomeClass.class.getName();");
94   verifyFormat("Class c = SomeClass.class;");
95 }
96 
TEST_F(FormatTestJava,ClassDeclarations)97 TEST_F(FormatTestJava, ClassDeclarations) {
98   verifyFormat("public class SomeClass {\n"
99                "  private int a;\n"
100                "  private int b;\n"
101                "}");
102   verifyFormat("public class A {\n"
103                "  class B {\n"
104                "    int i;\n"
105                "  }\n"
106                "  class C {\n"
107                "    int j;\n"
108                "  }\n"
109                "}");
110   verifyFormat("public class A extends B.C {}");
111 
112   verifyFormat("abstract class SomeClass\n"
113                "    extends SomeOtherClass implements SomeInterface {}",
114                getStyleWithColumns(60));
115   verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
116                "    implements SomeInterfaceeeeeeeeeeeee {}",
117                getStyleWithColumns(60));
118   verifyFormat("abstract class SomeClass\n"
119                "    extends SomeOtherClass\n"
120                "    implements SomeInterface {}",
121                getStyleWithColumns(40));
122   verifyFormat("abstract class SomeClass\n"
123                "    extends SomeOtherClass\n"
124                "    implements SomeInterface,\n"
125                "               AnotherInterface {}",
126                getStyleWithColumns(40));
127   verifyFormat("abstract class SomeClass\n"
128                "    implements SomeInterface, AnotherInterface {}",
129                getStyleWithColumns(60));
130   verifyFormat("@SomeAnnotation()\n"
131                "abstract class aaaaaaaaaaaa\n"
132                "    extends bbbbbbbbbbbbbbb implements cccccccccccc {}",
133                getStyleWithColumns(76));
134   verifyFormat("@SomeAnnotation()\n"
135                "abstract class aaaaaaaaa<a>\n"
136                "    extends bbbbbbbbbbbb<b> implements cccccccccccc {}",
137                getStyleWithColumns(76));
138   verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
139                "  void doStuff(int theStuff);\n"
140                "  void doMoreStuff(int moreStuff);\n"
141                "}");
142   verifyFormat("public interface SomeInterface {\n"
143                "  void doStuff(int theStuff);\n"
144                "  void doMoreStuff(int moreStuff);\n"
145                "}");
146   verifyFormat("@interface SomeInterface {\n"
147                "  void doStuff(int theStuff);\n"
148                "  void doMoreStuff(int moreStuff);\n"
149                "}");
150   verifyFormat("public @interface SomeInterface {\n"
151                "  void doStuff(int theStuff);\n"
152                "  void doMoreStuff(int moreStuff);\n"
153                "}");
154 }
155 
TEST_F(FormatTestJava,AnonymousClasses)156 TEST_F(FormatTestJava, AnonymousClasses) {
157   verifyFormat("return new A() {\n"
158                "  public String toString() {\n"
159                "    return \"NotReallyA\";\n"
160                "  }\n"
161                "};");
162   verifyFormat("A a = new A() {\n"
163                "  public String toString() {\n"
164                "    return \"NotReallyA\";\n"
165                "  }\n"
166                "};");
167 }
168 
TEST_F(FormatTestJava,EnumDeclarations)169 TEST_F(FormatTestJava, EnumDeclarations) {
170   verifyFormat("enum SomeThing { ABC, CDE }");
171   verifyFormat("enum SomeThing {\n"
172                "  ABC,\n"
173                "  CDE,\n"
174                "}");
175   verifyFormat("public class SomeClass {\n"
176                "  enum SomeThing { ABC, CDE }\n"
177                "  void f() {}\n"
178                "}");
179   verifyFormat("public class SomeClass implements SomeInterface {\n"
180                "  enum SomeThing { ABC, CDE }\n"
181                "  void f() {}\n"
182                "}");
183   verifyFormat("enum SomeThing {\n"
184                "  ABC,\n"
185                "  CDE;\n"
186                "  void f() {}\n"
187                "}");
188   verifyFormat("enum SomeThing {\n"
189                "  ABC(1, \"ABC\"),\n"
190                "  CDE(2, \"CDE\");\n"
191                "  Something(int i, String s) {}\n"
192                "}");
193   verifyFormat("enum SomeThing {\n"
194                "  ABC(new int[] {1, 2}),\n"
195                "  CDE(new int[] {2, 3});\n"
196                "  Something(int[] i) {}\n"
197                "}");
198   verifyFormat("public enum SomeThing {\n"
199                "  ABC {\n"
200                "    public String toString() {\n"
201                "      return \"ABC\";\n"
202                "    }\n"
203                "  },\n"
204                "  CDE {\n"
205                "    @Override\n"
206                "    public String toString() {\n"
207                "      return \"CDE\";\n"
208                "    }\n"
209                "  };\n"
210                "  public void f() {}\n"
211                "}");
212   verifyFormat("private enum SomeEnum implements Foo<?, B> {\n"
213                "  ABC {\n"
214                "    @Override\n"
215                "    public String toString() {\n"
216                "      return \"ABC\";\n"
217                "    }\n"
218                "  },\n"
219                "  CDE {\n"
220                "    @Override\n"
221                "    public String toString() {\n"
222                "      return \"CDE\";\n"
223                "    }\n"
224                "  };\n"
225                "}");
226 }
227 
TEST_F(FormatTestJava,ArrayInitializers)228 TEST_F(FormatTestJava, ArrayInitializers) {
229   verifyFormat("new int[] {1, 2, 3, 4};");
230   verifyFormat("new int[] {\n"
231                "    1, 2, 3, 4,\n"
232                "};");
233 
234   FormatStyle Style = getStyleWithColumns(65);
235   Style.Cpp11BracedListStyle = false;
236   verifyFormat(
237       "expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n"
238       "  100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };",
239       Style);
240 }
241 
TEST_F(FormatTestJava,ThrowsDeclarations)242 TEST_F(FormatTestJava, ThrowsDeclarations) {
243   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
244                "    throws LooooooooooooooooooooooooooooongException {}");
245   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
246                "    throws LoooooooooongException, LooooooooooongException {}");
247 }
248 
TEST_F(FormatTestJava,Annotations)249 TEST_F(FormatTestJava, Annotations) {
250   verifyFormat("@Override\n"
251                "public String toString() {}");
252   verifyFormat("@Override\n"
253                "@Nullable\n"
254                "public String getNameIfPresent() {}");
255   verifyFormat("@Override // comment\n"
256                "@Nullable\n"
257                "public String getNameIfPresent() {}");
258   verifyFormat("@java.lang.Override // comment\n"
259                "@Nullable\n"
260                "public String getNameIfPresent() {}");
261 
262   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
263                "public void doSomething() {}");
264   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
265                "@Author(name = \"abc\")\n"
266                "public void doSomething() {}");
267 
268   verifyFormat("DoSomething(new A() {\n"
269                "  @Override\n"
270                "  public String toString() {}\n"
271                "});");
272 
273   verifyFormat("void SomeFunction(@Nullable String something) {}");
274   verifyFormat("void SomeFunction(@org.llvm.Nullable String something) {}");
275 
276   verifyFormat("@Partial @Mock DataLoader loader;");
277   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
278                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
279 
280   verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
281                "private static final long something = 0L;");
282   verifyFormat("@org.llvm.Qualified(\"With some really looooooooooong text\")\n"
283                "private static final long something = 0L;");
284   verifyFormat("@Mock\n"
285                "DataLoader loooooooooooooooooooooooader =\n"
286                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
287                getStyleWithColumns(60));
288   verifyFormat("@org.llvm.QualifiedMock\n"
289                "DataLoader loooooooooooooooooooooooader =\n"
290                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
291                getStyleWithColumns(60));
292   verifyFormat("@Test(a)\n"
293                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
294                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa);");
295   verifyFormat("@SomeAnnotation(\n"
296                "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa)\n"
297                "int i;",
298                getStyleWithColumns(50));
299   verifyFormat("@Test\n"
300                "ReturnType doSomething(\n"
301                "    String aaaaaaaaaaaaa, String bbbbbbbbbbbbbbb) {}",
302                getStyleWithColumns(60));
303   verifyFormat("{\n"
304                "  boolean someFunction(\n"
305                "      @Param(aaaaaaaaaaaaaaaa) String aaaaa,\n"
306                "      String bbbbbbbbbbbbbbb) {}\n"
307                "}",
308                getStyleWithColumns(60));
309 }
310 
TEST_F(FormatTestJava,Generics)311 TEST_F(FormatTestJava, Generics) {
312   verifyFormat("Iterable<?> a;");
313   verifyFormat("Iterable<?> a;");
314   verifyFormat("Iterable<? extends SomeObject> a;");
315 
316   verifyFormat("A.<B>doSomething();");
317 
318   verifyFormat("@Override\n"
319                "public Map<String, ?> getAll() {}");
320 
321   verifyFormat("public <R> ArrayList<R> get() {}");
322   verifyFormat("protected <R> ArrayList<R> get() {}");
323   verifyFormat("private <R> ArrayList<R> get() {}");
324   verifyFormat("public static <R> ArrayList<R> get() {}");
325   verifyFormat("public static native <R> ArrayList<R> get();");
326   verifyFormat("public final <X> Foo foo() {}");
327   verifyFormat("public abstract <X> Foo foo();");
328   verifyFormat("<T extends B> T getInstance(Class<T> type);");
329   verifyFormat("Function<F, ? extends T> function;");
330 
331   verifyFormat("private Foo<X, Y>[] foos;");
332   verifyFormat("Foo<X, Y>[] foos = this.foos;");
333   verifyFormat("return (a instanceof List<?>)\n"
334                "    ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
335                "    : aaaaaaaaaaaaaaaaaaaaaaa;",
336                getStyleWithColumns(60));
337 
338   verifyFormat(
339       "SomeLoooooooooooooooooooooongType name =\n"
340       "    SomeType.foo(someArgument)\n"
341       "        .<X>method()\n"
342       "        .aaaaaaaaaaaaaaaaaaa()\n"
343       "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
344 }
345 
TEST_F(FormatTestJava,StringConcatenation)346 TEST_F(FormatTestJava, StringConcatenation) {
347   verifyFormat("String someString = \"abc\"\n"
348                "    + \"cde\";");
349 }
350 
TEST_F(FormatTestJava,TryCatchFinally)351 TEST_F(FormatTestJava, TryCatchFinally) {
352   verifyFormat("try {\n"
353                "  Something();\n"
354                "} catch (SomeException e) {\n"
355                "  HandleException(e);\n"
356                "}");
357   verifyFormat("try {\n"
358                "  Something();\n"
359                "} finally {\n"
360                "  AlwaysDoThis();\n"
361                "}");
362   verifyFormat("try {\n"
363                "  Something();\n"
364                "} catch (SomeException e) {\n"
365                "  HandleException(e);\n"
366                "} finally {\n"
367                "  AlwaysDoThis();\n"
368                "}");
369 
370   verifyFormat("try {\n"
371                "  Something();\n"
372                "} catch (SomeException | OtherException e) {\n"
373                "  HandleException(e);\n"
374                "}");
375 }
376 
TEST_F(FormatTestJava,TryWithResources)377 TEST_F(FormatTestJava, TryWithResources) {
378   verifyFormat("try (SomeResource rs = someFunction()) {\n"
379                "  Something();\n"
380                "}");
381   verifyFormat("try (SomeResource rs = someFunction()) {\n"
382                "  Something();\n"
383                "} catch (SomeException e) {\n"
384                "  HandleException(e);\n"
385                "}");
386 }
387 
TEST_F(FormatTestJava,SynchronizedKeyword)388 TEST_F(FormatTestJava, SynchronizedKeyword) {
389   verifyFormat("synchronized (mData) {\n"
390                "  // ...\n"
391                "}");
392 }
393 
TEST_F(FormatTestJava,PackageDeclarations)394 TEST_F(FormatTestJava, PackageDeclarations) {
395   verifyFormat("package some.really.loooooooooooooooooooooong.package;",
396                getStyleWithColumns(50));
397 }
398 
TEST_F(FormatTestJava,ImportDeclarations)399 TEST_F(FormatTestJava, ImportDeclarations) {
400   verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
401                getStyleWithColumns(50));
402   verifyFormat("import static some.really.looooooooooooooooong.imported.Class;",
403                getStyleWithColumns(50));
404 }
405 
TEST_F(FormatTestJava,MethodDeclarations)406 TEST_F(FormatTestJava, MethodDeclarations) {
407   verifyFormat("void methodName(Object arg1,\n"
408                "    Object arg2, Object arg3) {}",
409                getStyleWithColumns(40));
410   verifyFormat("void methodName(\n"
411                "    Object arg1, Object arg2) {}",
412                getStyleWithColumns(40));
413 }
414 
TEST_F(FormatTestJava,CppKeywords)415 TEST_F(FormatTestJava, CppKeywords) {
416   verifyFormat("public void union(Type a, Type b);");
417   verifyFormat("public void struct(Object o);");
418   verifyFormat("public void delete(Object o);");
419 }
420 
TEST_F(FormatTestJava,NeverAlignAfterReturn)421 TEST_F(FormatTestJava, NeverAlignAfterReturn) {
422   verifyFormat("return aaaaaaaaaaaaaaaaaaa\n"
423                "    && bbbbbbbbbbbbbbbbbbb\n"
424                "    && ccccccccccccccccccc;",
425                getStyleWithColumns(40));
426   verifyFormat("return (result == null)\n"
427                "    ? aaaaaaaaaaaaaaaaa\n"
428                "    : bbbbbbbbbbbbbbbbb;",
429                getStyleWithColumns(40));
430   verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
431                "    .bbbbbbbbbbbbbbbbbbb()\n"
432                "    .ccccccccccccccccccc();",
433                getStyleWithColumns(40));
434   verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
435                "    .bbbbbbbbbbbbbbbbbbb(\n"
436                "        ccccccccccccccc)\n"
437                "    .ccccccccccccccccccc();",
438                getStyleWithColumns(40));
439 }
440 
TEST_F(FormatTestJava,FormatsInnerBlocks)441 TEST_F(FormatTestJava, FormatsInnerBlocks) {
442   verifyFormat("someObject.someFunction(new Runnable() {\n"
443                "  @Override\n"
444                "  public void run() {\n"
445                "    System.out.println(42);\n"
446                "  }\n"
447                "}, someOtherParameter);");
448   verifyFormat("someFunction(new Runnable() {\n"
449                "  public void run() {\n"
450                "    System.out.println(42);\n"
451                "  }\n"
452                "});");
453   verifyFormat("someObject.someFunction(\n"
454                "    new Runnable() {\n"
455                "      @Override\n"
456                "      public void run() {\n"
457                "        System.out.println(42);\n"
458                "      }\n"
459                "    },\n"
460                "    new Runnable() {\n"
461                "      @Override\n"
462                "      public void run() {\n"
463                "        System.out.println(43);\n"
464                "      }\n"
465                "    },\n"
466                "    someOtherParameter);");
467 }
468 
TEST_F(FormatTestJava,FormatsLambdas)469 TEST_F(FormatTestJava, FormatsLambdas) {
470   verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
471   verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
472                "    -> aaaaaaaaaa + bbbbbbbbbb;",
473                getStyleWithColumns(40));
474   verifyFormat("Runnable someLambda = () -> DoSomething();");
475   verifyFormat("Runnable someLambda = () -> {\n"
476                "  DoSomething();\n"
477                "}");
478 
479   verifyFormat("Runnable someLambda =\n"
480                "    (int aaaaa) -> DoSomething(aaaaa);",
481                getStyleWithColumns(40));
482 }
483 
TEST_F(FormatTestJava,BreaksStringLiterals)484 TEST_F(FormatTestJava, BreaksStringLiterals) {
485   // FIXME: String literal breaking is currently disabled for Java and JS, as it
486   // requires strings to be merged using "+" which we don't support.
487   EXPECT_EQ("\"some text other\";",
488             format("\"some text other\";", getStyleWithColumns(14)));
489 }
490 
TEST_F(FormatTestJava,AlignsBlockComments)491 TEST_F(FormatTestJava, AlignsBlockComments) {
492   EXPECT_EQ("/*\n"
493             " * Really multi-line\n"
494             " * comment.\n"
495             " */\n"
496             "void f() {}",
497             format("  /*\n"
498                    "   * Really multi-line\n"
499                    "   * comment.\n"
500                    "   */\n"
501                    "  void f() {}"));
502 }
503 
504 } // end namespace tooling
505 } // end namespace clang
506