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 verifyFormat("return (a instanceof List<?>)\n" 293 " ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 294 " : aaaaaaaaaaaaaaaaaaaaaaa;", 295 getStyleWithColumns(60)); 296 297 verifyFormat( 298 "SomeLoooooooooooooooooooooongType name =\n" 299 " SomeType.foo(someArgument)\n" 300 " .<X>method()\n" 301 " .aaaaaaaaaaaaaaaaaaa()\n" 302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 303 } 304 305 TEST_F(FormatTestJava, StringConcatenation) { 306 verifyFormat("String someString = \"abc\"\n" 307 " + \"cde\";"); 308 } 309 310 TEST_F(FormatTestJava, TryCatchFinally) { 311 verifyFormat("try {\n" 312 " Something();\n" 313 "} catch (SomeException e) {\n" 314 " HandleException(e);\n" 315 "}"); 316 verifyFormat("try {\n" 317 " Something();\n" 318 "} finally {\n" 319 " AlwaysDoThis();\n" 320 "}"); 321 verifyFormat("try {\n" 322 " Something();\n" 323 "} catch (SomeException e) {\n" 324 " HandleException(e);\n" 325 "} finally {\n" 326 " AlwaysDoThis();\n" 327 "}"); 328 329 verifyFormat("try {\n" 330 " Something();\n" 331 "} catch (SomeException | OtherException e) {\n" 332 " HandleException(e);\n" 333 "}"); 334 } 335 336 TEST_F(FormatTestJava, SynchronizedKeyword) { 337 verifyFormat("synchronized (mData) {\n" 338 " // ...\n" 339 "}"); 340 } 341 342 TEST_F(FormatTestJava, PackageDeclarations) { 343 verifyFormat("package some.really.loooooooooooooooooooooong.package;", 344 getStyleWithColumns(50)); 345 } 346 347 TEST_F(FormatTestJava, ImportDeclarations) { 348 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;", 349 getStyleWithColumns(50)); 350 } 351 352 TEST_F(FormatTestJava, MethodDeclarations) { 353 verifyFormat("void methodName(Object arg1,\n" 354 " Object arg2, Object arg3) {}", 355 getStyleWithColumns(40)); 356 verifyFormat("void methodName(\n" 357 " Object arg1, Object arg2) {}", 358 getStyleWithColumns(40)); 359 } 360 361 TEST_F(FormatTestJava, CppKeywords) { 362 verifyFormat("public void union(Type a, Type b);"); 363 verifyFormat("public void struct(Object o);"); 364 verifyFormat("public void delete(Object o);"); 365 } 366 367 TEST_F(FormatTestJava, NeverAlignAfterReturn) { 368 verifyFormat("return aaaaaaaaaaaaaaaaaaa\n" 369 " && bbbbbbbbbbbbbbbbbbb\n" 370 " && ccccccccccccccccccc;", 371 getStyleWithColumns(40)); 372 verifyFormat("return (result == null)\n" 373 " ? aaaaaaaaaaaaaaaaa\n" 374 " : bbbbbbbbbbbbbbbbb;", 375 getStyleWithColumns(40)); 376 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n" 377 " .bbbbbbbbbbbbbbbbbbb()\n" 378 " .ccccccccccccccccccc();", 379 getStyleWithColumns(40)); 380 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n" 381 " .bbbbbbbbbbbbbbbbbbb(\n" 382 " ccccccccccccccc)\n" 383 " .ccccccccccccccccccc();", 384 getStyleWithColumns(40)); 385 } 386 387 TEST_F(FormatTestJava, FormatsInnerBlocks) { 388 verifyFormat("someObject.someFunction(new Runnable() {\n" 389 " @Override\n" 390 " public void run() {\n" 391 " System.out.println(42);\n" 392 " }\n" 393 "}, someOtherParameter);"); 394 verifyFormat("someFunction(new Runnable() {\n" 395 " public void run() {\n" 396 " System.out.println(42);\n" 397 " }\n" 398 "});"); 399 verifyFormat("someObject.someFunction(\n" 400 " new Runnable() {\n" 401 " @Override\n" 402 " public void run() {\n" 403 " System.out.println(42);\n" 404 " }\n" 405 " },\n" 406 " new Runnable() {\n" 407 " @Override\n" 408 " public void run() {\n" 409 " System.out.println(43);\n" 410 " }\n" 411 " },\n" 412 " someOtherParameter);"); 413 } 414 415 TEST_F(FormatTestJava, FormatsLambdas) { 416 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;"); 417 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n" 418 " -> aaaaaaaaaa + bbbbbbbbbb;", 419 getStyleWithColumns(40)); 420 verifyFormat("Runnable someLambda = () -> DoSomething();"); 421 verifyFormat("Runnable someLambda = () -> {\n" 422 " DoSomething();\n" 423 "}"); 424 425 verifyFormat("Runnable someLambda =\n" 426 " (int aaaaa) -> DoSomething(aaaaa);", 427 getStyleWithColumns(40)); 428 } 429 430 TEST_F(FormatTestJava, BreaksStringLiterals) { 431 // FIXME: String literal breaking is currently disabled for Java and JS, as it 432 // requires strings to be merged using "+" which we don't support. 433 EXPECT_EQ("\"some text other\";", 434 format("\"some text other\";", getStyleWithColumns(14))); 435 } 436 437 TEST_F(FormatTestJava, AlignsBlockComments) { 438 EXPECT_EQ("/*\n" 439 " * Really multi-line\n" 440 " * comment.\n" 441 " */\n" 442 "void f() {}", 443 format(" /*\n" 444 " * Really multi-line\n" 445 " * comment.\n" 446 " */\n" 447 " void f() {}")); 448 } 449 450 } // end namespace tooling 451 } // end namespace clang 452