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, UnderstandsCasts) { 58 verifyFormat("a[b >> 1] = (byte) (c() << 4);"); 59 } 60 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 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 87 TEST_F(FormatTestJava, QualifiedNames) { 88 verifyFormat("public some.package.Type someFunction( // comment\n" 89 " int parameter) {}"); 90 } 91 92 TEST_F(FormatTestJava, ClassKeyword) { 93 verifyFormat("SomeClass.class.getName();"); 94 verifyFormat("Class c = SomeClass.class;"); 95 } 96 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 156 TEST_F(FormatTestJava, EnumDeclarations) { 157 verifyFormat("enum SomeThing { ABC, CDE }"); 158 verifyFormat("enum SomeThing {\n" 159 " ABC,\n" 160 " CDE,\n" 161 "}"); 162 verifyFormat("public class SomeClass {\n" 163 " enum SomeThing { ABC, CDE }\n" 164 " void f() {}\n" 165 "}"); 166 verifyFormat("public class SomeClass implements SomeInterface {\n" 167 " enum SomeThing { ABC, CDE }\n" 168 " void f() {}\n" 169 "}"); 170 verifyFormat("enum SomeThing {\n" 171 " ABC,\n" 172 " CDE;\n" 173 " void f() {}\n" 174 "}"); 175 verifyFormat("enum SomeThing {\n" 176 " ABC(1, \"ABC\"),\n" 177 " CDE(2, \"CDE\");\n" 178 " Something(int i, String s) {}\n" 179 "}"); 180 verifyFormat("enum SomeThing {\n" 181 " ABC(new int[] {1, 2}),\n" 182 " CDE(new int[] {2, 3});\n" 183 " Something(int[] i) {}\n" 184 "}"); 185 verifyFormat("public enum SomeThing {\n" 186 " ABC {\n" 187 " public String toString() {\n" 188 " return \"ABC\";\n" 189 " }\n" 190 " },\n" 191 " CDE {\n" 192 " @Override\n" 193 " public String toString() {\n" 194 " return \"CDE\";\n" 195 " }\n" 196 " };\n" 197 " public void f() {}\n" 198 "}"); 199 verifyFormat("private enum SomeEnum implements Foo<?, B> {\n" 200 " ABC {\n" 201 " @Override\n" 202 " public String toString() {\n" 203 " return \"ABC\";\n" 204 " }\n" 205 " },\n" 206 " CDE {\n" 207 " @Override\n" 208 " public String toString() {\n" 209 " return \"CDE\";\n" 210 " }\n" 211 " };\n" 212 "}"); 213 } 214 215 TEST_F(FormatTestJava, ArrayInitializers) { 216 verifyFormat("new int[] {1, 2, 3, 4};"); 217 verifyFormat("new int[] {\n" 218 " 1, 2, 3, 4,\n" 219 "};"); 220 221 FormatStyle Style = getStyleWithColumns(65); 222 Style.Cpp11BracedListStyle = false; 223 verifyFormat( 224 "expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n" 225 " 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };", 226 Style); 227 } 228 229 TEST_F(FormatTestJava, ThrowsDeclarations) { 230 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n" 231 " throws LooooooooooooooooooooooooooooongException {}"); 232 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n" 233 " throws LoooooooooongException, LooooooooooongException {}"); 234 } 235 236 TEST_F(FormatTestJava, Annotations) { 237 verifyFormat("@Override\n" 238 "public String toString() {}"); 239 verifyFormat("@Override\n" 240 "@Nullable\n" 241 "public String getNameIfPresent() {}"); 242 verifyFormat("@Override // comment\n" 243 "@Nullable\n" 244 "public String getNameIfPresent() {}"); 245 246 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" 247 "public void doSomething() {}"); 248 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" 249 "@Author(name = \"abc\")\n" 250 "public void doSomething() {}"); 251 252 verifyFormat("DoSomething(new A() {\n" 253 " @Override\n" 254 " public String toString() {}\n" 255 "});"); 256 257 verifyFormat("void SomeFunction(@Nullable String something) {}"); 258 259 verifyFormat("@Partial @Mock DataLoader loader;"); 260 verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n" 261 "public static int iiiiiiiiiiiiiiiiiiiiiiii;"); 262 263 verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n" 264 "private static final long something = 0L;"); 265 verifyFormat("@Mock\n" 266 "DataLoader loooooooooooooooooooooooader =\n" 267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 268 getStyleWithColumns(60)); 269 } 270 271 TEST_F(FormatTestJava, Generics) { 272 verifyFormat("Iterable<?> a;"); 273 verifyFormat("Iterable<?> a;"); 274 verifyFormat("Iterable<? extends SomeObject> a;"); 275 276 verifyFormat("A.<B>doSomething();"); 277 278 verifyFormat("@Override\n" 279 "public Map<String, ?> getAll() {}"); 280 281 verifyFormat("public <R> ArrayList<R> get() {}"); 282 verifyFormat("protected <R> ArrayList<R> get() {}"); 283 verifyFormat("private <R> ArrayList<R> get() {}"); 284 verifyFormat("public static <R> ArrayList<R> get() {}"); 285 verifyFormat("public final <X> Foo foo() {}"); 286 verifyFormat("public abstract <X> Foo foo();"); 287 verifyFormat("<T extends B> T getInstance(Class<T> type);"); 288 verifyFormat("Function<F, ? extends T> function;"); 289 290 verifyFormat("private Foo<X, Y>[] foos;"); 291 verifyFormat("Foo<X, Y>[] foos = this.foos;"); 292 293 verifyFormat( 294 "SomeLoooooooooooooooooooooongType name =\n" 295 " SomeType.foo(someArgument)\n" 296 " .<X>method()\n" 297 " .aaaaaaaaaaaaaaaaaaa()\n" 298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 299 } 300 301 TEST_F(FormatTestJava, StringConcatenation) { 302 verifyFormat("String someString = \"abc\"\n" 303 " + \"cde\";"); 304 } 305 306 TEST_F(FormatTestJava, TryCatchFinally) { 307 verifyFormat("try {\n" 308 " Something();\n" 309 "} catch (SomeException e) {\n" 310 " HandleException(e);\n" 311 "}"); 312 verifyFormat("try {\n" 313 " Something();\n" 314 "} finally {\n" 315 " AlwaysDoThis();\n" 316 "}"); 317 verifyFormat("try {\n" 318 " Something();\n" 319 "} catch (SomeException e) {\n" 320 " HandleException(e);\n" 321 "} finally {\n" 322 " AlwaysDoThis();\n" 323 "}"); 324 325 verifyFormat("try {\n" 326 " Something();\n" 327 "} catch (SomeException | OtherException e) {\n" 328 " HandleException(e);\n" 329 "}"); 330 } 331 332 TEST_F(FormatTestJava, SynchronizedKeyword) { 333 verifyFormat("synchronized (mData) {\n" 334 " // ...\n" 335 "}"); 336 } 337 338 TEST_F(FormatTestJava, PackageDeclarations) { 339 verifyFormat("package some.really.loooooooooooooooooooooong.package;", 340 getStyleWithColumns(50)); 341 } 342 343 TEST_F(FormatTestJava, ImportDeclarations) { 344 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;", 345 getStyleWithColumns(50)); 346 } 347 348 TEST_F(FormatTestJava, MethodDeclarations) { 349 verifyFormat("void methodName(Object arg1,\n" 350 " Object arg2, Object arg3) {}", 351 getStyleWithColumns(40)); 352 verifyFormat("void methodName(\n" 353 " Object arg1, Object arg2) {}", 354 getStyleWithColumns(40)); 355 } 356 357 TEST_F(FormatTestJava, CppKeywords) { 358 verifyFormat("public void union(Type a, Type b);"); 359 verifyFormat("public void struct(Object o);"); 360 verifyFormat("public void delete(Object o);"); 361 } 362 363 TEST_F(FormatTestJava, NeverAlignAfterReturn) { 364 verifyFormat("return aaaaaaaaaaaaaaaaaaa\n" 365 " && bbbbbbbbbbbbbbbbbbb\n" 366 " && ccccccccccccccccccc;", 367 getStyleWithColumns(40)); 368 verifyFormat("return (result == null)\n" 369 " ? aaaaaaaaaaaaaaaaa\n" 370 " : bbbbbbbbbbbbbbbbb;", 371 getStyleWithColumns(40)); 372 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n" 373 " .bbbbbbbbbbbbbbbbbbb()\n" 374 " .ccccccccccccccccccc();", 375 getStyleWithColumns(40)); 376 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n" 377 " .bbbbbbbbbbbbbbbbbbb(\n" 378 " ccccccccccccccc)\n" 379 " .ccccccccccccccccccc();", 380 getStyleWithColumns(40)); 381 } 382 383 TEST_F(FormatTestJava, FormatsInnerBlocks) { 384 verifyFormat("someObject.someFunction(new Runnable() {\n" 385 " @Override\n" 386 " public void run() {\n" 387 " System.out.println(42);\n" 388 " }\n" 389 "}, someOtherParameter);"); 390 verifyFormat("someObject.someFunction(\n" 391 " new Runnable() {\n" 392 " @Override\n" 393 " public void run() {\n" 394 " System.out.println(42);\n" 395 " }\n" 396 " },\n" 397 " new Runnable() {\n" 398 " @Override\n" 399 " public void run() {\n" 400 " System.out.println(43);\n" 401 " }\n" 402 " },\n" 403 " someOtherParameter);"); 404 } 405 406 TEST_F(FormatTestJava, FormatsLambdas) { 407 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;"); 408 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n" 409 " -> aaaaaaaaaa + bbbbbbbbbb;", 410 getStyleWithColumns(40)); 411 verifyFormat("Runnable someLambda = () -> DoSomething();"); 412 verifyFormat("Runnable someLambda = () -> {\n" 413 " DoSomething();\n" 414 "}"); 415 416 verifyFormat("Runnable someLambda =\n" 417 " (int aaaaa) -> DoSomething(aaaaa);", 418 getStyleWithColumns(40)); 419 } 420 421 TEST_F(FormatTestJava, BreaksStringLiterals) { 422 // FIXME: String literal breaking is currently disabled for Java and JS, as it 423 // requires strings to be merged using "+" which we don't support. 424 EXPECT_EQ("\"some text other\";", 425 format("\"some text other\";", getStyleWithColumns(14))); 426 } 427 428 TEST_F(FormatTestJava, AlignsBlockComments) { 429 EXPECT_EQ("/*\n" 430 " * Really multi-line\n" 431 " * comment.\n" 432 " */\n" 433 "void f() {}", 434 format(" /*\n" 435 " * Really multi-line\n" 436 " * comment.\n" 437 " */\n" 438 " void f() {}")); 439 } 440 441 } // end namespace tooling 442 } // end namespace clang 443