blob: d60ab965b9dd28cfc976fb93a072aaf28717d8d1 [file] [log] [blame]
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001//===- 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
17namespace clang {
18namespace format {
19
20class FormatTestJava : public ::testing::Test {
21protected:
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
Daniel Jasper50b4bd72014-11-02 19:16:41 +000040 static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
41 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
42 Style.ColumnLimit = ColumnLimit;
43 return Style;
44 }
45
Daniel Jasperc58c70e2014-09-15 11:21:46 +000046 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
Daniel Jasper30a24062014-11-14 09:02:28 +000053TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
54 verifyFormat("someObject.and();");
55}
56
Daniel Jasperc58c70e2014-09-15 11:21:46 +000057TEST_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 "}");
Daniel Jasper4bf9d472014-10-21 09:31:29 +000070 verifyFormat("public class A extends B.C {}");
Daniel Jasper50b4bd72014-11-02 19:16:41 +000071
Daniel Jasper734d52b2014-11-14 10:15:56 +000072 verifyFormat("abstract class SomeClass\n"
73 " extends SomeOtherClass implements SomeInterface {}",
74 getStyleWithColumns(60));
Daniel Jasper50b4bd72014-11-02 19:16:41 +000075 verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
Daniel Jasper734d52b2014-11-14 10:15:56 +000076 " implements SomeInterfaceeeeeeeeeeeee {}",
Daniel Jasper50b4bd72014-11-02 19:16:41 +000077 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));
Daniel Jasper734d52b2014-11-14 10:15:56 +000087 verifyFormat("abstract class SomeClass\n"
88 " implements SomeInterface, AnotherInterface {}",
89 getStyleWithColumns(60));
Daniel Jasper39af6cd2014-11-03 02:27:28 +000090 verifyFormat("@SomeAnnotation()\n"
Daniel Jasper734d52b2014-11-14 10:15:56 +000091 "abstract class aaaaaaaaaaaa\n"
92 " extends bbbbbbbbbbbbbbb implements cccccccccccc {\n"
Daniel Jasper39af6cd2014-11-03 02:27:28 +000093 "}",
94 getStyleWithColumns(76));
Daniel Jasper58fcf6d2014-11-04 10:53:14 +000095 verifyFormat("@SomeAnnotation()\n"
Daniel Jasper734d52b2014-11-14 10:15:56 +000096 "abstract class aaaaaaaaa<a>\n"
97 " extends bbbbbbbbbbbb<b> implements cccccccccccc {\n"
Daniel Jasper58fcf6d2014-11-04 10:53:14 +000098 "}",
99 getStyleWithColumns(76));
Nico Webera644d7f2014-11-10 16:30:02 +0000100 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 "}");
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000116}
117
Daniel Jasperdf2ff002014-11-02 22:31:39 +0000118TEST_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 "}");
Daniel Jasper6be0f552014-11-13 15:56:28 +0000129 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 "}");
Daniel Jasperdf2ff002014-11-02 22:31:39 +0000167}
168
Daniel Jasperf26c7552014-10-17 13:36:14 +0000169TEST_F(FormatTestJava, ThrowsDeclarations) {
170 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
Daniel Jaspere003b782014-10-28 16:29:56 +0000171 " throws LooooooooooooooooooooooooooooongException {\n}");
Daniel Jasperf26c7552014-10-17 13:36:14 +0000172}
173
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000174TEST_F(FormatTestJava, Annotations) {
175 verifyFormat("@Override\n"
176 "public String toString() {\n}");
177 verifyFormat("@Override\n"
178 "@Nullable\n"
179 "public String getNameIfPresent() {\n}");
Daniel Jasperf1f0c352014-10-21 09:25:39 +0000180
181 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
182 "public void doSomething() {\n}");
Daniel Jasperd78c4222014-10-21 11:17:56 +0000183 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
184 "@Author(name = \"abc\")\n"
185 "public void doSomething() {\n}");
Daniel Jasperf1f0c352014-10-21 09:25:39 +0000186
Daniel Jasperc7d024a2014-10-21 10:02:03 +0000187 verifyFormat("DoSomething(new A() {\n"
188 " @Override\n"
189 " public String toString() {\n"
190 " }\n"
191 "});");
192
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000193 verifyFormat("void SomeFunction(@Nullable String something) {\n"
194 "}");
195
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000196 verifyFormat("@Partial @Mock DataLoader loader;");
Daniel Jasperfd681912014-10-21 10:58:14 +0000197 verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
198 "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000199
200 verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
201 "private static final long something = 0L;");
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000202}
203
Daniel Jasper16b107e2014-10-21 09:57:09 +0000204TEST_F(FormatTestJava, Generics) {
205 verifyFormat("Iterable<?> a;");
206 verifyFormat("Iterable<?> a;");
207 verifyFormat("Iterable<? extends SomeObject> a;");
Daniel Jasper5ffcb7f2014-10-21 11:13:31 +0000208
209 verifyFormat("A.<B>doSomething();");
Daniel Jasper7bd618f2014-11-02 21:52:57 +0000210
211 verifyFormat("@Override\n"
Daniel Jasperdb9a7a22014-11-03 02:35:14 +0000212 "public Map<String, ?> getAll() {\n}");
213
Daniel Jasper09f6abe2014-11-14 09:05:32 +0000214 verifyFormat("public <R> ArrayList<R> get() {\n}");
Daniel Jasper4bfa7362014-11-14 17:30:15 +0000215 verifyFormat("protected <R> ArrayList<R> get() {\n}");
216 verifyFormat("private <R> ArrayList<R> get() {\n}");
Daniel Jasperdb9a7a22014-11-03 02:35:14 +0000217 verifyFormat("public static <R> ArrayList<R> get() {\n}");
Daniel Jasperf056f452014-11-03 02:45:58 +0000218 verifyFormat("<T extends B> T getInstance(Class<T> type);");
Daniel Jasper61d81972014-11-14 08:22:46 +0000219 verifyFormat("Function<F, ? extends T> function;");
Daniel Jasper6761b422014-11-18 23:48:01 +0000220
221 verifyFormat("private Foo<X, Y>[] foos;");
222 verifyFormat("Foo<X, Y>[] foos = this.foos;");
Daniel Jasper16b107e2014-10-21 09:57:09 +0000223}
224
Daniel Jasperc0126862014-10-21 11:34:53 +0000225TEST_F(FormatTestJava, StringConcatenation) {
226 verifyFormat("String someString = \"abc\"\n"
227 " + \"cde\";");
228}
229
Daniel Jaspera3ddf862014-11-02 19:21:48 +0000230TEST_F(FormatTestJava, TryCatchFinally) {
231 verifyFormat("try {\n"
232 " Something();\n"
233 "} catch (SomeException e) {\n"
234 " HandleException(e);\n"
235 "}");
236 verifyFormat("try {\n"
237 " Something();\n"
238 "} finally {\n"
239 " AlwaysDoThis();\n"
240 "}");
241 verifyFormat("try {\n"
242 " Something();\n"
243 "} catch (SomeException e) {\n"
244 " HandleException(e);\n"
245 "} finally {\n"
246 " AlwaysDoThis();\n"
247 "}");
248
249 verifyFormat("try {\n"
250 " Something();\n"
251 "} catch (SomeException | OtherException e) {\n"
252 " HandleException(e);\n"
253 "}");
254}
255
Daniel Jasperb9d3db62014-11-02 22:00:57 +0000256TEST_F(FormatTestJava, SynchronizedKeyword) {
257 verifyFormat("synchronized (mData) {\n"
258 " // ...\n"
259 "}");
260}
261
Daniel Jasper5e7be1d2014-11-02 22:13:03 +0000262TEST_F(FormatTestJava, ImportDeclarations) {
263 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
264 getStyleWithColumns(50));
265}
266
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000267} // end namespace tooling
268} // end namespace clang