xref: /llvm-project/clang/unittests/Format/FormatTestJava.cpp (revision df2ff002f0fd203119e8d5abfbbfa71f15b1b499)
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 }
81 
82 TEST_F(FormatTestJava, EnumDeclarations) {
83   verifyFormat("enum SomeThing { ABC, CDE }");
84   verifyFormat("enum SomeThing {\n"
85                "  ABC,\n"
86                "  CDE,\n"
87                "}");
88   verifyFormat("public class SomeClass {\n"
89                "  enum SomeThing { ABC, CDE }\n"
90                "  void f() {\n"
91                "  }\n"
92                "}");
93 }
94 
95 TEST_F(FormatTestJava, ThrowsDeclarations) {
96   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
97                "    throws LooooooooooooooooooooooooooooongException {\n}");
98 }
99 
100 TEST_F(FormatTestJava, Annotations) {
101   verifyFormat("@Override\n"
102                "public String toString() {\n}");
103   verifyFormat("@Override\n"
104                "@Nullable\n"
105                "public String getNameIfPresent() {\n}");
106 
107   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
108                "public void doSomething() {\n}");
109   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
110                "@Author(name = \"abc\")\n"
111                "public void doSomething() {\n}");
112 
113   verifyFormat("DoSomething(new A() {\n"
114                "  @Override\n"
115                "  public String toString() {\n"
116                "  }\n"
117                "});");
118 
119   verifyFormat("void SomeFunction(@Nullable String something) {\n"
120                "}");
121 
122   verifyFormat("@Partial @Mock DataLoader loader;");
123   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
124                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
125 
126   verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
127                "private static final long something = 0L;");
128 }
129 
130 TEST_F(FormatTestJava, Generics) {
131   verifyFormat("Iterable<?> a;");
132   verifyFormat("Iterable<?> a;");
133   verifyFormat("Iterable<? extends SomeObject> a;");
134 
135   verifyFormat("A.<B>doSomething();");
136 
137   verifyFormat("@Override\n"
138                "public Map<String, ?> getAll() {\n"
139                "}");
140 }
141 
142 TEST_F(FormatTestJava, StringConcatenation) {
143   verifyFormat("String someString = \"abc\"\n"
144                "                    + \"cde\";");
145 }
146 
147 TEST_F(FormatTestJava, TryCatchFinally) {
148   verifyFormat("try {\n"
149                "  Something();\n"
150                "} catch (SomeException e) {\n"
151                "  HandleException(e);\n"
152                "}");
153   verifyFormat("try {\n"
154                "  Something();\n"
155                "} finally {\n"
156                "  AlwaysDoThis();\n"
157                "}");
158   verifyFormat("try {\n"
159                "  Something();\n"
160                "} catch (SomeException e) {\n"
161                "  HandleException(e);\n"
162                "} finally {\n"
163                "  AlwaysDoThis();\n"
164                "}");
165 
166   verifyFormat("try {\n"
167                "  Something();\n"
168                "} catch (SomeException | OtherException e) {\n"
169                "  HandleException(e);\n"
170                "}");
171 }
172 
173 TEST_F(FormatTestJava, SynchronizedKeyword) {
174   verifyFormat("synchronized (mData) {\n"
175                "  // ...\n"
176                "}");
177 }
178 
179 TEST_F(FormatTestJava, ImportDeclarations) {
180   verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
181                getStyleWithColumns(50));
182 }
183 
184 } // end namespace tooling
185 } // end namespace clang
186