xref: /llvm-project/clang/unittests/Format/FormatTestJava.cpp (revision 58fcf6df6557098f442be11d65d9d53b91cfd1f7)
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, ClassDeclarations) {
54   verifyFormat("public class SomeClass {\n"
55                "  private int a;\n"
56                "  private int b;\n"
57                "}");
58   verifyFormat("public class A {\n"
59                "  class B {\n"
60                "    int i;\n"
61                "  }\n"
62                "  class C {\n"
63                "    int j;\n"
64                "  }\n"
65                "}");
66   verifyFormat("public class A extends B.C {}");
67 
68   verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
69                "    implements SomeInterface {}",
70                getStyleWithColumns(60));
71   verifyFormat("abstract class SomeClass\n"
72                "    extends SomeOtherClass\n"
73                "    implements SomeInterface {}",
74                getStyleWithColumns(40));
75   verifyFormat("abstract class SomeClass\n"
76                "    extends SomeOtherClass\n"
77                "    implements SomeInterface,\n"
78                "               AnotherInterface {}",
79                getStyleWithColumns(40));
80   verifyFormat("@SomeAnnotation()\n"
81                "abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb\n"
82                "    implements cccccccccccc {\n"
83                "}",
84                getStyleWithColumns(76));
85   verifyFormat("@SomeAnnotation()\n"
86                "abstract class aaaaaaaaa<a> extends bbbbbbbbbbbb<b>\n"
87                "    implements cccccccccccc {\n"
88                "}",
89                getStyleWithColumns(76));
90 }
91 
92 TEST_F(FormatTestJava, EnumDeclarations) {
93   verifyFormat("enum SomeThing { ABC, CDE }");
94   verifyFormat("enum SomeThing {\n"
95                "  ABC,\n"
96                "  CDE,\n"
97                "}");
98   verifyFormat("public class SomeClass {\n"
99                "  enum SomeThing { ABC, CDE }\n"
100                "  void f() {\n"
101                "  }\n"
102                "}");
103 }
104 
105 TEST_F(FormatTestJava, ThrowsDeclarations) {
106   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
107                "    throws LooooooooooooooooooooooooooooongException {\n}");
108 }
109 
110 TEST_F(FormatTestJava, Annotations) {
111   verifyFormat("@Override\n"
112                "public String toString() {\n}");
113   verifyFormat("@Override\n"
114                "@Nullable\n"
115                "public String getNameIfPresent() {\n}");
116 
117   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
118                "public void doSomething() {\n}");
119   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
120                "@Author(name = \"abc\")\n"
121                "public void doSomething() {\n}");
122 
123   verifyFormat("DoSomething(new A() {\n"
124                "  @Override\n"
125                "  public String toString() {\n"
126                "  }\n"
127                "});");
128 
129   verifyFormat("void SomeFunction(@Nullable String something) {\n"
130                "}");
131 
132   verifyFormat("@Partial @Mock DataLoader loader;");
133   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
134                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
135 
136   verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
137                "private static final long something = 0L;");
138 }
139 
140 TEST_F(FormatTestJava, Generics) {
141   verifyFormat("Iterable<?> a;");
142   verifyFormat("Iterable<?> a;");
143   verifyFormat("Iterable<? extends SomeObject> a;");
144 
145   verifyFormat("A.<B>doSomething();");
146 
147   verifyFormat("@Override\n"
148                "public Map<String, ?> getAll() {\n}");
149 
150   verifyFormat("public static <R> ArrayList<R> get() {\n}");
151   verifyFormat("<T extends B> T getInstance(Class<T> type);");
152 }
153 
154 TEST_F(FormatTestJava, StringConcatenation) {
155   verifyFormat("String someString = \"abc\"\n"
156                "                    + \"cde\";");
157 }
158 
159 TEST_F(FormatTestJava, TryCatchFinally) {
160   verifyFormat("try {\n"
161                "  Something();\n"
162                "} catch (SomeException e) {\n"
163                "  HandleException(e);\n"
164                "}");
165   verifyFormat("try {\n"
166                "  Something();\n"
167                "} finally {\n"
168                "  AlwaysDoThis();\n"
169                "}");
170   verifyFormat("try {\n"
171                "  Something();\n"
172                "} catch (SomeException e) {\n"
173                "  HandleException(e);\n"
174                "} finally {\n"
175                "  AlwaysDoThis();\n"
176                "}");
177 
178   verifyFormat("try {\n"
179                "  Something();\n"
180                "} catch (SomeException | OtherException e) {\n"
181                "  HandleException(e);\n"
182                "}");
183 }
184 
185 TEST_F(FormatTestJava, SynchronizedKeyword) {
186   verifyFormat("synchronized (mData) {\n"
187                "  // ...\n"
188                "}");
189 }
190 
191 TEST_F(FormatTestJava, ImportDeclarations) {
192   verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
193                getStyleWithColumns(50));
194 }
195 
196 } // end namespace tooling
197 } // end namespace clang
198