xref: /llvm-project/clang/unittests/Format/FormatTestJava.cpp (revision 734d52b58bf3056a91e16c866da00f5b49e40098)
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:
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 
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 
40   static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
41     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
42     Style.ColumnLimit = ColumnLimit;
43     return Style;
44   }
45 
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 
53 TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
54   verifyFormat("someObject.and();");
55 }
56 
57 TEST_F(FormatTestJava, ClassDeclarations) {
58   verifyFormat("public class SomeClass {\n"
59                "  private int a;\n"
60                "  private int b;\n"
61                "}");
62   verifyFormat("public class A {\n"
63                "  class B {\n"
64                "    int i;\n"
65                "  }\n"
66                "  class C {\n"
67                "    int j;\n"
68                "  }\n"
69                "}");
70   verifyFormat("public class A extends B.C {}");
71 
72   verifyFormat("abstract class SomeClass\n"
73                "    extends SomeOtherClass implements SomeInterface {}",
74                getStyleWithColumns(60));
75   verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
76                "    implements SomeInterfaceeeeeeeeeeeee {}",
77                getStyleWithColumns(60));
78   verifyFormat("abstract class SomeClass\n"
79                "    extends SomeOtherClass\n"
80                "    implements SomeInterface {}",
81                getStyleWithColumns(40));
82   verifyFormat("abstract class SomeClass\n"
83                "    extends SomeOtherClass\n"
84                "    implements SomeInterface,\n"
85                "               AnotherInterface {}",
86                getStyleWithColumns(40));
87   verifyFormat("abstract class SomeClass\n"
88                "    implements SomeInterface, AnotherInterface {}",
89                getStyleWithColumns(60));
90   verifyFormat("@SomeAnnotation()\n"
91                "abstract class aaaaaaaaaaaa\n"
92                "    extends bbbbbbbbbbbbbbb implements cccccccccccc {\n"
93                "}",
94                getStyleWithColumns(76));
95   verifyFormat("@SomeAnnotation()\n"
96                "abstract class aaaaaaaaa<a>\n"
97                "    extends bbbbbbbbbbbb<b> implements cccccccccccc {\n"
98                "}",
99                getStyleWithColumns(76));
100   verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
101                "  void doStuff(int theStuff);\n"
102                "  void doMoreStuff(int moreStuff);\n"
103                "}");
104   verifyFormat("public interface SomeInterface {\n"
105                "  void doStuff(int theStuff);\n"
106                "  void doMoreStuff(int moreStuff);\n"
107                "}");
108   verifyFormat("@interface SomeInterface {\n"
109                "  void doStuff(int theStuff);\n"
110                "  void doMoreStuff(int moreStuff);\n"
111                "}");
112   verifyFormat("public @interface SomeInterface {\n"
113                "  void doStuff(int theStuff);\n"
114                "  void doMoreStuff(int moreStuff);\n"
115                "}");
116 }
117 
118 TEST_F(FormatTestJava, EnumDeclarations) {
119   verifyFormat("enum SomeThing { ABC, CDE }");
120   verifyFormat("enum SomeThing {\n"
121                "  ABC,\n"
122                "  CDE,\n"
123                "}");
124   verifyFormat("public class SomeClass {\n"
125                "  enum SomeThing { ABC, CDE }\n"
126                "  void f() {\n"
127                "  }\n"
128                "}");
129   verifyFormat("public class SomeClass implements SomeInterface {\n"
130                "  enum SomeThing { ABC, CDE }\n"
131                "  void f() {\n"
132                "  }\n"
133                "}");
134   verifyFormat("enum SomeThing {\n"
135                "  ABC,\n"
136                "  CDE;\n"
137                "  void f() {\n"
138                "  }\n"
139                "}");
140   verifyFormat("enum SomeThing {\n"
141                "  ABC(1, \"ABC\"),\n"
142                "  CDE(2, \"CDE\");\n"
143                "  Something(int i, String s) {\n"
144                "  }\n"
145                "}");
146   verifyFormat("enum SomeThing {\n"
147                "  ABC(new int[]{1, 2}),\n"
148                "  CDE(new int[]{2, 3});\n"
149                "  Something(int[] i) {\n"
150                "  }\n"
151                "}");
152   verifyFormat("public enum SomeThing {\n"
153                "  ABC {\n"
154                "    public String toString() {\n"
155                "      return \"ABC\";\n"
156                "    }\n"
157                "  },\n"
158                "  CDE {\n"
159                "    @Override\n"
160                "    public String toString() {\n"
161                "      return \"CDE\";\n"
162                "    }\n"
163                "  };\n"
164                "  public void f() {\n"
165                "  }\n"
166                "}");
167 }
168 
169 TEST_F(FormatTestJava, ThrowsDeclarations) {
170   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
171                "    throws LooooooooooooooooooooooooooooongException {\n}");
172 }
173 
174 TEST_F(FormatTestJava, Annotations) {
175   verifyFormat("@Override\n"
176                "public String toString() {\n}");
177   verifyFormat("@Override\n"
178                "@Nullable\n"
179                "public String getNameIfPresent() {\n}");
180 
181   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
182                "public void doSomething() {\n}");
183   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
184                "@Author(name = \"abc\")\n"
185                "public void doSomething() {\n}");
186 
187   verifyFormat("DoSomething(new A() {\n"
188                "  @Override\n"
189                "  public String toString() {\n"
190                "  }\n"
191                "});");
192 
193   verifyFormat("void SomeFunction(@Nullable String something) {\n"
194                "}");
195 
196   verifyFormat("@Partial @Mock DataLoader loader;");
197   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
198                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
199 
200   verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
201                "private static final long something = 0L;");
202 }
203 
204 TEST_F(FormatTestJava, Generics) {
205   verifyFormat("Iterable<?> a;");
206   verifyFormat("Iterable<?> a;");
207   verifyFormat("Iterable<? extends SomeObject> a;");
208 
209   verifyFormat("A.<B>doSomething();");
210 
211   verifyFormat("@Override\n"
212                "public Map<String, ?> getAll() {\n}");
213 
214   verifyFormat("public <R> ArrayList<R> get() {\n}");
215   verifyFormat("public static <R> ArrayList<R> get() {\n}");
216   verifyFormat("<T extends B> T getInstance(Class<T> type);");
217   verifyFormat("Function<F, ? extends T> function;");
218 }
219 
220 TEST_F(FormatTestJava, StringConcatenation) {
221   verifyFormat("String someString = \"abc\"\n"
222                "                    + \"cde\";");
223 }
224 
225 TEST_F(FormatTestJava, TryCatchFinally) {
226   verifyFormat("try {\n"
227                "  Something();\n"
228                "} catch (SomeException e) {\n"
229                "  HandleException(e);\n"
230                "}");
231   verifyFormat("try {\n"
232                "  Something();\n"
233                "} finally {\n"
234                "  AlwaysDoThis();\n"
235                "}");
236   verifyFormat("try {\n"
237                "  Something();\n"
238                "} catch (SomeException e) {\n"
239                "  HandleException(e);\n"
240                "} finally {\n"
241                "  AlwaysDoThis();\n"
242                "}");
243 
244   verifyFormat("try {\n"
245                "  Something();\n"
246                "} catch (SomeException | OtherException e) {\n"
247                "  HandleException(e);\n"
248                "}");
249 }
250 
251 TEST_F(FormatTestJava, SynchronizedKeyword) {
252   verifyFormat("synchronized (mData) {\n"
253                "  // ...\n"
254                "}");
255 }
256 
257 TEST_F(FormatTestJava, ImportDeclarations) {
258   verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
259                getStyleWithColumns(50));
260 }
261 
262 } // end namespace tooling
263 } // end namespace clang
264