[gn build] Port 0e80f9a1b51e
[llvm-project.git] / clang / unittests / Format / FormatTestJava.cpp
blob33998bc7ff858fa7848c2c8e2caafcd0fd5d4f53
1 //===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test"
13 namespace clang {
14 namespace format {
15 namespace test {
16 namespace {
18 class FormatTestJava : public test::FormatTestBase {
19 protected:
20 FormatStyle getDefaultStyle() const override {
21 return getGoogleStyle(FormatStyle::LK_Java);
24 static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
25 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
26 Style.ColumnLimit = ColumnLimit;
27 return Style;
31 TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
32 verifyFormat("someObject.and();");
35 TEST_F(FormatTestJava, UnderstandsCasts) {
36 verifyFormat("a[b >> 1] = (byte) (c() << 4);");
39 TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) {
40 FormatStyle Style = getStyleWithColumns(50);
41 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
42 " instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
43 Style);
44 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
45 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n"
46 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
47 Style);
48 verifyFormat("return aaaaaaaaaaaaaaaaaaa instanceof bbbbbbbbbbbbbbbbbbbbbbb\n"
49 " && ccccccccccccccccccc instanceof dddddddddddddddddddddd;");
52 TEST_F(FormatTestJava, Chromium) {
53 verifyFormat("class SomeClass {\n"
54 " void f() {}\n"
55 " int g() {\n"
56 " return 0;\n"
57 " }\n"
58 " void h() {\n"
59 " while (true) f();\n"
60 " for (;;) f();\n"
61 " if (true) f();\n"
62 " }\n"
63 "}",
64 getChromiumStyle(FormatStyle::LK_Java));
67 TEST_F(FormatTestJava, QualifiedNames) {
68 verifyFormat("public some.package.Type someFunction( // comment\n"
69 " int parameter) {}");
72 TEST_F(FormatTestJava, ClassKeyword) {
73 verifyFormat("SomeClass.class.getName();");
74 verifyFormat("Class c = SomeClass.class;");
77 TEST_F(FormatTestJava, ClassDeclarations) {
78 verifyFormat("public class SomeClass {\n"
79 " private int a;\n"
80 " private int b;\n"
81 "}");
82 verifyFormat("public class A {\n"
83 " class B {\n"
84 " int i;\n"
85 " }\n"
86 " class C {\n"
87 " int j;\n"
88 " }\n"
89 "}");
90 verifyFormat("public class A extends B.C {}");
92 verifyFormat("abstract class SomeClass\n"
93 " extends SomeOtherClass implements SomeInterface {}",
94 getStyleWithColumns(60));
95 verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
96 " implements SomeInterfaceeeeeeeeeeeee {}",
97 getStyleWithColumns(60));
98 verifyFormat("abstract class SomeClass\n"
99 " extends SomeOtherClass\n"
100 " implements SomeInterface {}",
101 getStyleWithColumns(40));
102 verifyFormat("abstract class SomeClass\n"
103 " extends SomeOtherClass\n"
104 " implements SomeInterface,\n"
105 " AnotherInterface {}",
106 getStyleWithColumns(40));
107 verifyFormat("abstract class SomeClass\n"
108 " implements SomeInterface, AnotherInterface {}",
109 getStyleWithColumns(60));
110 verifyFormat("@SomeAnnotation()\n"
111 "abstract class aaaaaaaaaaaa\n"
112 " extends bbbbbbbbbbbbbbb implements cccccccccccc {}",
113 getStyleWithColumns(76));
114 verifyFormat("@SomeAnnotation()\n"
115 "abstract class aaaaaaaaa<a>\n"
116 " extends bbbbbbbbbbbb<b> implements cccccccccccc {}",
117 getStyleWithColumns(76));
118 verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
119 " void doStuff(int theStuff);\n"
120 " void doMoreStuff(int moreStuff);\n"
121 "}");
122 verifyFormat("public interface SomeInterface {\n"
123 " void doStuff(int theStuff);\n"
124 " void doMoreStuff(int moreStuff);\n"
125 " default void doStuffWithDefault() {}\n"
126 "}");
127 verifyFormat("@interface SomeInterface {\n"
128 " void doStuff(int theStuff);\n"
129 " void doMoreStuff(int moreStuff);\n"
130 "}");
131 verifyFormat("public @interface SomeInterface {\n"
132 " void doStuff(int theStuff);\n"
133 " void doMoreStuff(int moreStuff);\n"
134 "}");
135 verifyFormat("class A {\n"
136 " public @interface SomeInterface {\n"
137 " int stuff;\n"
138 " void doMoreStuff(int moreStuff);\n"
139 " }\n"
140 "}");
141 verifyFormat("class A {\n"
142 " public @interface SomeInterface {}\n"
143 "}");
146 TEST_F(FormatTestJava, AnonymousClasses) {
147 verifyFormat("return new A() {\n"
148 " public String toString() {\n"
149 " return \"NotReallyA\";\n"
150 " }\n"
151 "};");
152 verifyFormat("A a = new A() {\n"
153 " public String toString() {\n"
154 " return \"NotReallyA\";\n"
155 " }\n"
156 "};");
159 TEST_F(FormatTestJava, EnumDeclarations) {
160 verifyFormat("enum SomeThing { ABC, CDE }");
161 verifyFormat("enum SomeThing {\n"
162 " ABC,\n"
163 " CDE,\n"
164 "}");
165 verifyFormat("public class SomeClass {\n"
166 " enum SomeThing { ABC, CDE }\n"
167 " void f() {}\n"
168 "}");
169 verifyFormat("public class SomeClass implements SomeInterface {\n"
170 " enum SomeThing { ABC, CDE }\n"
171 " void f() {}\n"
172 "}");
173 verifyFormat("enum SomeThing {\n"
174 " ABC,\n"
175 " CDE;\n"
176 " void f() {}\n"
177 "}");
178 verifyFormat("enum SomeThing {\n"
179 " void f() {}");
180 verifyFormat("enum SomeThing {\n"
181 " ABC(1, \"ABC\"),\n"
182 " CDE(2, \"CDE\");\n"
183 " Something(int i, String s) {}\n"
184 "}");
185 verifyFormat("enum SomeThing {\n"
186 " ABC(new int[] {1, 2}),\n"
187 " CDE(new int[] {2, 3});\n"
188 " Something(int[] i) {}\n"
189 "}");
190 verifyFormat("public enum SomeThing {\n"
191 " ABC {\n"
192 " public String toString() {\n"
193 " return \"ABC\";\n"
194 " }\n"
195 " },\n"
196 " CDE {\n"
197 " @Override\n"
198 " public String toString() {\n"
199 " return \"CDE\";\n"
200 " }\n"
201 " };\n"
202 " public void f() {}\n"
203 "}");
204 verifyFormat("private enum SomeEnum implements Foo<?, B> {\n"
205 " ABC {\n"
206 " @Override\n"
207 " public String toString() {\n"
208 " return \"ABC\";\n"
209 " }\n"
210 " },\n"
211 " CDE {\n"
212 " @Override\n"
213 " public String toString() {\n"
214 " return \"CDE\";\n"
215 " }\n"
216 " };\n"
217 "}");
218 verifyFormat("public enum VeryLongEnum {\n"
219 " ENUM_WITH_MANY_PARAMETERS(\n"
220 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"bbbbbbbbbbbbbbbb\", "
221 "\"cccccccccccccccccccccccc\"),\n"
222 " SECOND_ENUM(\"a\", \"b\", \"c\");\n"
223 " private VeryLongEnum(String a, String b, String c) {}\n"
224 "}");
227 TEST_F(FormatTestJava, ArrayInitializers) {
228 verifyFormat("new int[] {1, 2, 3, 4};");
229 verifyFormat("new int[] {\n"
230 " 1,\n"
231 " 2,\n"
232 " 3,\n"
233 " 4,\n"
234 "};");
236 FormatStyle Style = getStyleWithColumns(65);
237 Style.Cpp11BracedListStyle = false;
238 verifyFormat(
239 "expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n"
240 " 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };",
241 Style);
244 TEST_F(FormatTestJava, ThrowsDeclarations) {
245 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
246 " throws LooooooooooooooooooooooooooooongException {}");
247 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
248 " throws LoooooooooongException, LooooooooooongException {}");
251 TEST_F(FormatTestJava, Annotations) {
252 verifyFormat("@Override\n"
253 "public String toString() {}");
254 verifyFormat("@Override\n"
255 "@Nullable\n"
256 "public String getNameIfPresent() {}");
257 verifyFormat("@Override // comment\n"
258 "@Nullable\n"
259 "public String getNameIfPresent() {}");
260 verifyFormat("@java.lang.Override // comment\n"
261 "@Nullable\n"
262 "public String getNameIfPresent() {}");
264 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
265 "public void doSomething() {}");
266 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
267 "@Author(name = \"abc\")\n"
268 "public void doSomething() {}");
270 verifyFormat("DoSomething(new A() {\n"
271 " @Override\n"
272 " public String toString() {}\n"
273 "});");
275 verifyFormat("void SomeFunction(@Nullable String something) {}");
276 verifyFormat("void SomeFunction(@org.llvm.Nullable String something) {}");
278 verifyFormat("@Partial @Mock DataLoader loader;");
279 verifyFormat("@Partial\n"
280 "@Mock\n"
281 "DataLoader loader;",
282 getChromiumStyle(FormatStyle::LK_Java));
283 verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
284 "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
286 verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
287 "private static final long something = 0L;");
288 verifyFormat("@org.llvm.Qualified(\"With some really looooooooooong text\")\n"
289 "private static final long something = 0L;");
290 verifyFormat("@Mock\n"
291 "DataLoader loooooooooooooooooooooooader =\n"
292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
293 getStyleWithColumns(60));
294 verifyFormat("@org.llvm.QualifiedMock\n"
295 "DataLoader loooooooooooooooooooooooader =\n"
296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
297 getStyleWithColumns(60));
298 verifyFormat("@Test(a)\n"
299 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa);");
301 verifyFormat("@SomeAnnotation(\n"
302 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa)\n"
303 "int i;",
304 getStyleWithColumns(50));
305 verifyFormat("@Test\n"
306 "ReturnType doSomething(\n"
307 " String aaaaaaaaaaaaa, String bbbbbbbbbbbbbbb) {}",
308 getStyleWithColumns(60));
309 verifyFormat("{\n"
310 " boolean someFunction(\n"
311 " @Param(aaaaaaaaaaaaaaaa) String aaaaa,\n"
312 " String bbbbbbbbbbbbbbb) {}\n"
313 "}",
314 getStyleWithColumns(60));
315 verifyFormat("@Annotation(\"Some\"\n"
316 " + \" text\")\n"
317 "List<Integer> list;");
319 verifyFormat(
320 "@Test\n"
321 "@Feature({\"Android-TabSwitcher\"})\n"
322 "@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})\n"
323 "@Features.EnableFeatures({FEATURE})\n"
324 "public void test(@Foo.bar(\"baz\") @Quux.Qoob int theFirstParaaaaam,\n"
325 " @Foo.bar(\"baz\") @Quux.Qoob int theSecondParaaaaaaaaaaaaaaaam) {}");
328 TEST_F(FormatTestJava, Generics) {
329 verifyFormat("Iterable<?> a;");
330 verifyFormat("Iterable<?> a;");
331 verifyFormat("Iterable<? extends SomeObject> a;");
333 verifyFormat("A.<B>doSomething();");
334 verifyFormat("A.<B<C>>doSomething();");
335 verifyFormat("A.<B<C<D>>>doSomething();");
336 verifyFormat("A.<B<C<D<E>>>>doSomething();");
338 verifyFormat("OrderedPair<String, List<Box<Integer>>> p = null;");
340 verifyFormat("@Override\n"
341 "public Map<String, ?> getAll() {}");
343 verifyFormat("public <R> ArrayList<R> get() {}");
344 verifyFormat("protected <R> ArrayList<R> get() {}");
345 verifyFormat("private <R> ArrayList<R> get() {}");
346 verifyFormat("public static <R> ArrayList<R> get() {}");
347 verifyFormat("public static native <R> ArrayList<R> get();");
348 verifyFormat("public final <X> Foo foo() {}");
349 verifyFormat("public abstract <X> Foo foo();");
350 verifyFormat("<T extends B> T getInstance(Class<T> type);");
351 verifyFormat("Function<F, ? extends T> function;");
353 verifyFormat("private Foo<X, Y>[] foos;");
354 verifyFormat("Foo<X, Y>[] foos = this.foos;");
355 verifyFormat("return (a instanceof List<?>)\n"
356 " ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
357 " : aaaaaaaaaaaaaaaaaaaaaaa;",
358 getStyleWithColumns(60));
360 verifyFormat(
361 "SomeLoooooooooooooooooooooongType name =\n"
362 " SomeType.foo(someArgument)\n"
363 " .<X>method()\n"
364 " .aaaaaaaaaaaaaaaaaaa()\n"
365 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
368 TEST_F(FormatTestJava, StringConcatenation) {
369 verifyFormat("String someString = \"abc\"\n"
370 " + \"cde\";");
373 TEST_F(FormatTestJava, TryCatchFinally) {
374 verifyFormat("try {\n"
375 " Something();\n"
376 "} catch (SomeException e) {\n"
377 " HandleException(e);\n"
378 "}");
379 verifyFormat("try {\n"
380 " Something();\n"
381 "} finally {\n"
382 " AlwaysDoThis();\n"
383 "}");
384 verifyFormat("try {\n"
385 " Something();\n"
386 "} catch (SomeException e) {\n"
387 " HandleException(e);\n"
388 "} finally {\n"
389 " AlwaysDoThis();\n"
390 "}");
392 verifyFormat("try {\n"
393 " Something();\n"
394 "} catch (SomeException | OtherException e) {\n"
395 " HandleException(e);\n"
396 "}");
399 TEST_F(FormatTestJava, TryWithResources) {
400 verifyFormat("try (SomeResource rs = someFunction()) {\n"
401 " Something();\n"
402 "}");
403 verifyFormat("try (SomeResource rs = someFunction()) {\n"
404 " Something();\n"
405 "} catch (SomeException e) {\n"
406 " HandleException(e);\n"
407 "}");
410 TEST_F(FormatTestJava, SynchronizedKeyword) {
411 verifyFormat("synchronized (mData) {\n"
412 " // ...\n"
413 "}");
415 FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
416 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
418 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
419 Style.BraceWrapping.AfterFunction = false;
420 verifyFormat("synchronized (mData)\n"
421 "{\n"
422 " // ...\n"
423 "}",
424 Style);
426 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
427 Style.BraceWrapping.AfterFunction = true;
428 verifyFormat("synchronized (mData) {\n"
429 " // ...\n"
430 "}",
431 Style);
434 TEST_F(FormatTestJava, AssertKeyword) {
435 verifyFormat("assert a && b;");
436 verifyFormat("assert (a && b);");
439 TEST_F(FormatTestJava, PackageDeclarations) {
440 verifyFormat("package some.really.loooooooooooooooooooooong.package;",
441 getStyleWithColumns(50));
444 TEST_F(FormatTestJava, ImportDeclarations) {
445 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
446 getStyleWithColumns(50));
447 verifyFormat("import static some.really.looooooooooooooooong.imported.Class;",
448 getStyleWithColumns(50));
451 TEST_F(FormatTestJava, MethodDeclarations) {
452 verifyFormat("void methodName(Object arg1,\n"
453 " Object arg2, Object arg3) {}",
454 getStyleWithColumns(40));
455 verifyFormat("void methodName(\n"
456 " Object arg1, Object arg2) {}",
457 getStyleWithColumns(40));
460 TEST_F(FormatTestJava, MethodReference) {
461 EXPECT_EQ("private void foo() {\n"
462 " f(this::methodReference);\n"
463 " f(C.super::methodReference);\n"
464 " Consumer<String> c = System.out::println;\n"
465 " Iface<Integer> mRef = Ty::<Integer>meth;\n"
466 "}",
467 format("private void foo() {\n"
468 " f(this ::methodReference);\n"
469 " f(C.super ::methodReference);\n"
470 " Consumer<String> c = System.out ::println;\n"
471 " Iface<Integer> mRef = Ty :: <Integer> meth;\n"
472 "}"));
475 TEST_F(FormatTestJava, CppKeywords) {
476 verifyFormat("public void union(Type a, Type b);");
477 verifyFormat("public void struct(Object o);");
478 verifyFormat("public void delete(Object o);");
479 verifyFormat("return operator && (aa);");
482 TEST_F(FormatTestJava, NeverAlignAfterReturn) {
483 verifyFormat("return aaaaaaaaaaaaaaaaaaa\n"
484 " && bbbbbbbbbbbbbbbbbbb\n"
485 " && ccccccccccccccccccc;",
486 getStyleWithColumns(40));
487 verifyFormat("return (result == null)\n"
488 " ? aaaaaaaaaaaaaaaaa\n"
489 " : bbbbbbbbbbbbbbbbb;",
490 getStyleWithColumns(40));
491 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
492 " .bbbbbbbbbbbbbbbbbbb()\n"
493 " .ccccccccccccccccccc();",
494 getStyleWithColumns(40));
495 verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
496 " .bbbbbbbbbbbbbbbbbbb(\n"
497 " ccccccccccccccc)\n"
498 " .ccccccccccccccccccc();",
499 getStyleWithColumns(40));
502 TEST_F(FormatTestJava, FormatsInnerBlocks) {
503 verifyFormat("someObject.someFunction(new Runnable() {\n"
504 " @Override\n"
505 " public void run() {\n"
506 " System.out.println(42);\n"
507 " }\n"
508 "}, someOtherParameter);");
509 verifyFormat("someFunction(new Runnable() {\n"
510 " public void run() {\n"
511 " System.out.println(42);\n"
512 " }\n"
513 "});");
514 verifyFormat("someObject.someFunction(\n"
515 " new Runnable() {\n"
516 " @Override\n"
517 " public void run() {\n"
518 " System.out.println(42);\n"
519 " }\n"
520 " },\n"
521 " new Runnable() {\n"
522 " @Override\n"
523 " public void run() {\n"
524 " System.out.println(43);\n"
525 " }\n"
526 " },\n"
527 " someOtherParameter);");
530 TEST_F(FormatTestJava, FormatsLambdas) {
531 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
532 verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
533 " -> aaaaaaaaaa + bbbbbbbbbb;",
534 getStyleWithColumns(40));
535 verifyFormat("Runnable someLambda = () -> DoSomething();");
536 verifyFormat("Runnable someLambda = () -> {\n"
537 " DoSomething();\n"
538 "}");
540 verifyFormat("Runnable someLambda =\n"
541 " (int aaaaa) -> DoSomething(aaaaa);",
542 getStyleWithColumns(40));
545 TEST_F(FormatTestJava, BreaksStringLiterals) {
546 verifyFormat("x = \"some text \"\n"
547 " + \"other\";",
548 "x = \"some text other\";", getStyleWithColumns(18));
551 TEST_F(FormatTestJava, AlignsBlockComments) {
552 EXPECT_EQ("/*\n"
553 " * Really multi-line\n"
554 " * comment.\n"
555 " */\n"
556 "void f() {}",
557 format(" /*\n"
558 " * Really multi-line\n"
559 " * comment.\n"
560 " */\n"
561 " void f() {}"));
564 TEST_F(FormatTestJava, AlignDeclarations) {
565 FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
566 Style.AlignConsecutiveDeclarations.Enabled = true;
567 verifyFormat("private final String[] args;\n"
568 "private final A_ParserHelper parserHelper;\n"
569 "private final int numOfCmdArgs;\n"
570 "private int numOfCmdArgs;\n"
571 "private String[] args;",
572 Style);
575 TEST_F(FormatTestJava, KeepsDelimitersOnOwnLineInJavaDocComments) {
576 EXPECT_EQ("/**\n"
577 " * javadoc line 1\n"
578 " * javadoc line 2\n"
579 " */",
580 format("/** javadoc line 1\n"
581 " * javadoc line 2 */"));
584 TEST_F(FormatTestJava, RetainsLogicalShifts) {
585 verifyFormat("void f() {\n"
586 " int a = 1;\n"
587 " a >>>= 1;\n"
588 "}");
589 verifyFormat("void f() {\n"
590 " int a = 1;\n"
591 " a = a >>> 1;\n"
592 "}");
595 TEST_F(FormatTestJava, ShortFunctions) {
596 FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
597 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
598 verifyFormat("enum Enum {\n"
599 " E1,\n"
600 " E2;\n"
601 " void f() { return; }\n"
602 "}",
603 Style);
606 TEST_F(FormatTestJava, ConfigurableSpacesInSquareBrackets) {
607 FormatStyle Spaces = getLLVMStyle(FormatStyle::LK_Java);
609 verifyFormat("Object[] arguments", Spaces);
610 verifyFormat("final Class<?>[] types = new Class<?>[numElements];", Spaces);
611 verifyFormat("types[i] = arguments[i].getClass();", Spaces);
613 Spaces.SpacesInSquareBrackets = true;
615 verifyFormat("Object[ ] arguments", Spaces);
616 verifyFormat("final Class<?>[ ] types = new Class<?>[ numElements ];",
617 Spaces);
618 verifyFormat("types[ i ] = arguments[ i ].getClass();", Spaces);
621 TEST_F(FormatTestJava, SwitchExpression) {
622 auto Style = getLLVMStyle(FormatStyle::LK_Java);
623 EXPECT_TRUE(Style.AllowShortCaseExpressionOnASingleLine);
625 verifyFormat("foo(switch (day) {\n"
626 " case THURSDAY, SATURDAY -> 8;\n"
627 " case WEDNESDAY -> 9;\n"
628 " default -> 1;\n"
629 "});",
630 Style);
632 constexpr StringRef Code1{"i = switch (day) {\n"
633 " case THURSDAY, SATURDAY -> 8;\n"
634 " case WEDNESDAY -> 9;\n"
635 " default -> 0;\n"
636 "};"};
637 verifyFormat(Code1, Style);
639 Style.IndentCaseLabels = true;
640 verifyFormat(Code1, Style);
642 constexpr StringRef Code2{"i = switch (day) {\n"
643 " case THURSDAY, SATURDAY -> {\n"
644 " foo();\n"
645 " yield 8;\n"
646 " }\n"
647 " case WEDNESDAY -> {\n"
648 " bar();\n"
649 " yield 9;\n"
650 " }\n"
651 " default -> {\n"
652 " yield 0;\n"
653 " }\n"
654 "};"};
655 verifyFormat(Code2, Style);
657 Style.IndentCaseLabels = false;
658 verifyFormat(Code2, Style);
660 constexpr StringRef Code3{"switch (day) {\n"
661 "case THURSDAY, SATURDAY -> i = 8;\n"
662 "case WEDNESDAY -> i = 9;\n"
663 "default -> i = 0;\n"
664 "};"};
665 verifyFormat(Code3, Style);
667 Style.IndentCaseLabels = true;
668 verifyFormat("switch (day) {\n"
669 " case THURSDAY, SATURDAY -> i = 8;\n"
670 " case WEDNESDAY -> i = 9;\n"
671 " default -> i = 0;\n"
672 "};",
673 Code3, Style);
676 TEST_F(FormatTestJava, ShortCaseExpression) {
677 auto Style = getLLVMStyle(FormatStyle::LK_Java);
679 verifyFormat("i = switch (a) {\n"
680 " case 1 -> 1;\n"
681 " case 2 -> // comment\n"
682 " 2;\n"
683 " case 3 ->\n"
684 " // comment\n"
685 " 3;\n"
686 " case 4 -> 4; // comment\n"
687 " default -> 0;\n"
688 "};",
689 Style);
691 verifyNoChange("i = switch (a) {\n"
692 " case 1 -> 1;\n"
693 " // comment\n"
694 " case 2 -> 2;\n"
695 " // comment 1\n"
696 " // comment 2\n"
697 " case 3 -> 3; /* comment */\n"
698 " case 4 -> /* comment */ 4;\n"
699 " case 5 -> x + /* comment */ 1;\n"
700 " default ->\n"
701 " 0; // comment line 1\n"
702 " // comment line 2\n"
703 "};",
704 Style);
706 Style.ColumnLimit = 18;
707 verifyFormat("i = switch (a) {\n"
708 " case Monday ->\n"
709 " 1;\n"
710 " default -> 9999;\n"
711 "};",
712 Style);
714 Style.ColumnLimit = 80;
715 Style.AllowShortCaseExpressionOnASingleLine = false;
716 Style.IndentCaseLabels = true;
717 verifyFormat("i = switch (n) {\n"
718 " default /*comments*/ ->\n"
719 " 1;\n"
720 " case 0 ->\n"
721 " 0;\n"
722 "};",
723 Style);
725 Style.AllowShortCaseExpressionOnASingleLine = true;
726 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
727 Style.BraceWrapping.AfterCaseLabel = true;
728 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
729 verifyFormat("i = switch (n)\n"
730 "{\n"
731 " case 0 ->\n"
732 " {\n"
733 " yield 0;\n"
734 " }\n"
735 " default ->\n"
736 " {\n"
737 " yield 1;\n"
738 " }\n"
739 "};",
740 Style);
743 TEST_F(FormatTestJava, AlignCaseArrows) {
744 auto Style = getLLVMStyle(FormatStyle::LK_Java);
745 Style.AlignConsecutiveShortCaseStatements.Enabled = true;
747 verifyFormat("foo(switch (day) {\n"
748 " case THURSDAY, SATURDAY -> 8;\n"
749 " case WEDNESDAY -> 9;\n"
750 " default -> 1;\n"
751 "});",
752 Style);
754 verifyFormat("i = switch (day) {\n"
755 " case THURSDAY, SATURDAY -> 8;\n"
756 " case WEDNESDAY -> 9;\n"
757 " default -> 0;\n"
758 "};",
759 Style);
761 verifyFormat("switch (day) {\n"
762 "case THURSDAY, SATURDAY -> i = 8;\n"
763 "case WEDNESDAY -> i = 9;\n"
764 "default -> i = 0;\n"
765 "};",
766 Style);
768 Style.AlignConsecutiveShortCaseStatements.AlignCaseArrows = true;
770 verifyFormat("foo(switch (day) {\n"
771 " case THURSDAY, SATURDAY -> 8;\n"
772 " case WEDNESDAY -> 9;\n"
773 " default -> 1;\n"
774 "});",
775 Style);
777 verifyFormat("i = switch (day) {\n"
778 " case THURSDAY, SATURDAY -> 8;\n"
779 " case WEDNESDAY -> 9;\n"
780 " default -> 0;\n"
781 "};",
782 Style);
784 verifyFormat("switch (day) {\n"
785 "case THURSDAY, SATURDAY -> i = 8;\n"
786 "case WEDNESDAY -> i = 9;\n"
787 "default -> i = 0;\n"
788 "};",
789 Style);
792 } // namespace
793 } // namespace test
794 } // namespace format
795 } // namespace clang