xref: /minix3/external/bsd/llvm/dist/clang/unittests/Format/FormatTestJava.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include "FormatTestUtils.h"
11*0a6a1f1dSLionel Sambuc #include "clang/Format/Format.h"
12*0a6a1f1dSLionel Sambuc #include "llvm/Support/Debug.h"
13*0a6a1f1dSLionel Sambuc #include "gtest/gtest.h"
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "format-test"
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc namespace clang {
18*0a6a1f1dSLionel Sambuc namespace format {
19*0a6a1f1dSLionel Sambuc 
20*0a6a1f1dSLionel Sambuc class FormatTestJava : public ::testing::Test {
21*0a6a1f1dSLionel Sambuc protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)22*0a6a1f1dSLionel Sambuc   static std::string format(llvm::StringRef Code, unsigned Offset,
23*0a6a1f1dSLionel Sambuc                             unsigned Length, const FormatStyle &Style) {
24*0a6a1f1dSLionel Sambuc     DEBUG(llvm::errs() << "---\n");
25*0a6a1f1dSLionel Sambuc     DEBUG(llvm::errs() << Code << "\n\n");
26*0a6a1f1dSLionel Sambuc     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27*0a6a1f1dSLionel Sambuc     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28*0a6a1f1dSLionel Sambuc     std::string Result = applyAllReplacements(Code, Replaces);
29*0a6a1f1dSLionel Sambuc     EXPECT_NE("", Result);
30*0a6a1f1dSLionel Sambuc     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31*0a6a1f1dSLionel Sambuc     return Result;
32*0a6a1f1dSLionel Sambuc   }
33*0a6a1f1dSLionel Sambuc 
format(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_Java))34*0a6a1f1dSLionel Sambuc   static std::string format(
35*0a6a1f1dSLionel Sambuc       llvm::StringRef Code,
36*0a6a1f1dSLionel Sambuc       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
37*0a6a1f1dSLionel Sambuc     return format(Code, 0, Code.size(), Style);
38*0a6a1f1dSLionel Sambuc   }
39*0a6a1f1dSLionel Sambuc 
getStyleWithColumns(unsigned ColumnLimit)40*0a6a1f1dSLionel Sambuc   static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
41*0a6a1f1dSLionel Sambuc     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
42*0a6a1f1dSLionel Sambuc     Style.ColumnLimit = ColumnLimit;
43*0a6a1f1dSLionel Sambuc     return Style;
44*0a6a1f1dSLionel Sambuc   }
45*0a6a1f1dSLionel Sambuc 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_Java))46*0a6a1f1dSLionel Sambuc   static void verifyFormat(
47*0a6a1f1dSLionel Sambuc       llvm::StringRef Code,
48*0a6a1f1dSLionel Sambuc       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
49*0a6a1f1dSLionel Sambuc     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50*0a6a1f1dSLionel Sambuc   }
51*0a6a1f1dSLionel Sambuc };
52*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,NoAlternativeOperatorNames)53*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
54*0a6a1f1dSLionel Sambuc   verifyFormat("someObject.and();");
55*0a6a1f1dSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,UnderstandsCasts)57*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, UnderstandsCasts) {
58*0a6a1f1dSLionel Sambuc   verifyFormat("a[b >> 1] = (byte) (c() << 4);");
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,FormatsInstanceOfLikeOperators)61*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) {
62*0a6a1f1dSLionel Sambuc   FormatStyle Style = getStyleWithColumns(50);
63*0a6a1f1dSLionel Sambuc   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
64*0a6a1f1dSLionel Sambuc                "    instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
65*0a6a1f1dSLionel Sambuc                Style);
66*0a6a1f1dSLionel Sambuc   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
67*0a6a1f1dSLionel Sambuc   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n"
68*0a6a1f1dSLionel Sambuc                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
69*0a6a1f1dSLionel Sambuc                Style);
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,Chromium)72*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, Chromium) {
73*0a6a1f1dSLionel Sambuc   verifyFormat("class SomeClass {\n"
74*0a6a1f1dSLionel Sambuc                "    void f() {}\n"
75*0a6a1f1dSLionel Sambuc                "    int g() {\n"
76*0a6a1f1dSLionel Sambuc                "        return 0;\n"
77*0a6a1f1dSLionel Sambuc                "    }\n"
78*0a6a1f1dSLionel Sambuc                "    void h() {\n"
79*0a6a1f1dSLionel Sambuc                "        while (true) f();\n"
80*0a6a1f1dSLionel Sambuc                "        for (;;) f();\n"
81*0a6a1f1dSLionel Sambuc                "        if (true) f();\n"
82*0a6a1f1dSLionel Sambuc                "    }\n"
83*0a6a1f1dSLionel Sambuc                "}",
84*0a6a1f1dSLionel Sambuc                getChromiumStyle(FormatStyle::LK_Java));
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,QualifiedNames)87*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, QualifiedNames) {
88*0a6a1f1dSLionel Sambuc   verifyFormat("public some.package.Type someFunction( // comment\n"
89*0a6a1f1dSLionel Sambuc                "    int parameter) {}");
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,ClassKeyword)92*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, ClassKeyword) {
93*0a6a1f1dSLionel Sambuc   verifyFormat("SomeClass.class.getName();");
94*0a6a1f1dSLionel Sambuc   verifyFormat("Class c = SomeClass.class;");
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,ClassDeclarations)97*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, ClassDeclarations) {
98*0a6a1f1dSLionel Sambuc   verifyFormat("public class SomeClass {\n"
99*0a6a1f1dSLionel Sambuc                "  private int a;\n"
100*0a6a1f1dSLionel Sambuc                "  private int b;\n"
101*0a6a1f1dSLionel Sambuc                "}");
102*0a6a1f1dSLionel Sambuc   verifyFormat("public class A {\n"
103*0a6a1f1dSLionel Sambuc                "  class B {\n"
104*0a6a1f1dSLionel Sambuc                "    int i;\n"
105*0a6a1f1dSLionel Sambuc                "  }\n"
106*0a6a1f1dSLionel Sambuc                "  class C {\n"
107*0a6a1f1dSLionel Sambuc                "    int j;\n"
108*0a6a1f1dSLionel Sambuc                "  }\n"
109*0a6a1f1dSLionel Sambuc                "}");
110*0a6a1f1dSLionel Sambuc   verifyFormat("public class A extends B.C {}");
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc   verifyFormat("abstract class SomeClass\n"
113*0a6a1f1dSLionel Sambuc                "    extends SomeOtherClass implements SomeInterface {}",
114*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
115*0a6a1f1dSLionel Sambuc   verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
116*0a6a1f1dSLionel Sambuc                "    implements SomeInterfaceeeeeeeeeeeee {}",
117*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
118*0a6a1f1dSLionel Sambuc   verifyFormat("abstract class SomeClass\n"
119*0a6a1f1dSLionel Sambuc                "    extends SomeOtherClass\n"
120*0a6a1f1dSLionel Sambuc                "    implements SomeInterface {}",
121*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
122*0a6a1f1dSLionel Sambuc   verifyFormat("abstract class SomeClass\n"
123*0a6a1f1dSLionel Sambuc                "    extends SomeOtherClass\n"
124*0a6a1f1dSLionel Sambuc                "    implements SomeInterface,\n"
125*0a6a1f1dSLionel Sambuc                "               AnotherInterface {}",
126*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
127*0a6a1f1dSLionel Sambuc   verifyFormat("abstract class SomeClass\n"
128*0a6a1f1dSLionel Sambuc                "    implements SomeInterface, AnotherInterface {}",
129*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
130*0a6a1f1dSLionel Sambuc   verifyFormat("@SomeAnnotation()\n"
131*0a6a1f1dSLionel Sambuc                "abstract class aaaaaaaaaaaa\n"
132*0a6a1f1dSLionel Sambuc                "    extends bbbbbbbbbbbbbbb implements cccccccccccc {}",
133*0a6a1f1dSLionel Sambuc                getStyleWithColumns(76));
134*0a6a1f1dSLionel Sambuc   verifyFormat("@SomeAnnotation()\n"
135*0a6a1f1dSLionel Sambuc                "abstract class aaaaaaaaa<a>\n"
136*0a6a1f1dSLionel Sambuc                "    extends bbbbbbbbbbbb<b> implements cccccccccccc {}",
137*0a6a1f1dSLionel Sambuc                getStyleWithColumns(76));
138*0a6a1f1dSLionel Sambuc   verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
139*0a6a1f1dSLionel Sambuc                "  void doStuff(int theStuff);\n"
140*0a6a1f1dSLionel Sambuc                "  void doMoreStuff(int moreStuff);\n"
141*0a6a1f1dSLionel Sambuc                "}");
142*0a6a1f1dSLionel Sambuc   verifyFormat("public interface SomeInterface {\n"
143*0a6a1f1dSLionel Sambuc                "  void doStuff(int theStuff);\n"
144*0a6a1f1dSLionel Sambuc                "  void doMoreStuff(int moreStuff);\n"
145*0a6a1f1dSLionel Sambuc                "}");
146*0a6a1f1dSLionel Sambuc   verifyFormat("@interface SomeInterface {\n"
147*0a6a1f1dSLionel Sambuc                "  void doStuff(int theStuff);\n"
148*0a6a1f1dSLionel Sambuc                "  void doMoreStuff(int moreStuff);\n"
149*0a6a1f1dSLionel Sambuc                "}");
150*0a6a1f1dSLionel Sambuc   verifyFormat("public @interface SomeInterface {\n"
151*0a6a1f1dSLionel Sambuc                "  void doStuff(int theStuff);\n"
152*0a6a1f1dSLionel Sambuc                "  void doMoreStuff(int moreStuff);\n"
153*0a6a1f1dSLionel Sambuc                "}");
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,EnumDeclarations)156*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, EnumDeclarations) {
157*0a6a1f1dSLionel Sambuc   verifyFormat("enum SomeThing { ABC, CDE }");
158*0a6a1f1dSLionel Sambuc   verifyFormat("enum SomeThing {\n"
159*0a6a1f1dSLionel Sambuc                "  ABC,\n"
160*0a6a1f1dSLionel Sambuc                "  CDE,\n"
161*0a6a1f1dSLionel Sambuc                "}");
162*0a6a1f1dSLionel Sambuc   verifyFormat("public class SomeClass {\n"
163*0a6a1f1dSLionel Sambuc                "  enum SomeThing { ABC, CDE }\n"
164*0a6a1f1dSLionel Sambuc                "  void f() {}\n"
165*0a6a1f1dSLionel Sambuc                "}");
166*0a6a1f1dSLionel Sambuc   verifyFormat("public class SomeClass implements SomeInterface {\n"
167*0a6a1f1dSLionel Sambuc                "  enum SomeThing { ABC, CDE }\n"
168*0a6a1f1dSLionel Sambuc                "  void f() {}\n"
169*0a6a1f1dSLionel Sambuc                "}");
170*0a6a1f1dSLionel Sambuc   verifyFormat("enum SomeThing {\n"
171*0a6a1f1dSLionel Sambuc                "  ABC,\n"
172*0a6a1f1dSLionel Sambuc                "  CDE;\n"
173*0a6a1f1dSLionel Sambuc                "  void f() {}\n"
174*0a6a1f1dSLionel Sambuc                "}");
175*0a6a1f1dSLionel Sambuc   verifyFormat("enum SomeThing {\n"
176*0a6a1f1dSLionel Sambuc                "  ABC(1, \"ABC\"),\n"
177*0a6a1f1dSLionel Sambuc                "  CDE(2, \"CDE\");\n"
178*0a6a1f1dSLionel Sambuc                "  Something(int i, String s) {}\n"
179*0a6a1f1dSLionel Sambuc                "}");
180*0a6a1f1dSLionel Sambuc   verifyFormat("enum SomeThing {\n"
181*0a6a1f1dSLionel Sambuc                "  ABC(new int[] {1, 2}),\n"
182*0a6a1f1dSLionel Sambuc                "  CDE(new int[] {2, 3});\n"
183*0a6a1f1dSLionel Sambuc                "  Something(int[] i) {}\n"
184*0a6a1f1dSLionel Sambuc                "}");
185*0a6a1f1dSLionel Sambuc   verifyFormat("public enum SomeThing {\n"
186*0a6a1f1dSLionel Sambuc                "  ABC {\n"
187*0a6a1f1dSLionel Sambuc                "    public String toString() {\n"
188*0a6a1f1dSLionel Sambuc                "      return \"ABC\";\n"
189*0a6a1f1dSLionel Sambuc                "    }\n"
190*0a6a1f1dSLionel Sambuc                "  },\n"
191*0a6a1f1dSLionel Sambuc                "  CDE {\n"
192*0a6a1f1dSLionel Sambuc                "    @Override\n"
193*0a6a1f1dSLionel Sambuc                "    public String toString() {\n"
194*0a6a1f1dSLionel Sambuc                "      return \"CDE\";\n"
195*0a6a1f1dSLionel Sambuc                "    }\n"
196*0a6a1f1dSLionel Sambuc                "  };\n"
197*0a6a1f1dSLionel Sambuc                "  public void f() {}\n"
198*0a6a1f1dSLionel Sambuc                "}");
199*0a6a1f1dSLionel Sambuc   verifyFormat("private enum SomeEnum implements Foo<?, B> {\n"
200*0a6a1f1dSLionel Sambuc                "  ABC {\n"
201*0a6a1f1dSLionel Sambuc                "    @Override\n"
202*0a6a1f1dSLionel Sambuc                "    public String toString() {\n"
203*0a6a1f1dSLionel Sambuc                "      return \"ABC\";\n"
204*0a6a1f1dSLionel Sambuc                "    }\n"
205*0a6a1f1dSLionel Sambuc                "  },\n"
206*0a6a1f1dSLionel Sambuc                "  CDE {\n"
207*0a6a1f1dSLionel Sambuc                "    @Override\n"
208*0a6a1f1dSLionel Sambuc                "    public String toString() {\n"
209*0a6a1f1dSLionel Sambuc                "      return \"CDE\";\n"
210*0a6a1f1dSLionel Sambuc                "    }\n"
211*0a6a1f1dSLionel Sambuc                "  };\n"
212*0a6a1f1dSLionel Sambuc                "}");
213*0a6a1f1dSLionel Sambuc }
214*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,ArrayInitializers)215*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, ArrayInitializers) {
216*0a6a1f1dSLionel Sambuc   verifyFormat("new int[] {1, 2, 3, 4};");
217*0a6a1f1dSLionel Sambuc   verifyFormat("new int[] {\n"
218*0a6a1f1dSLionel Sambuc                "    1, 2, 3, 4,\n"
219*0a6a1f1dSLionel Sambuc                "};");
220*0a6a1f1dSLionel Sambuc 
221*0a6a1f1dSLionel Sambuc   FormatStyle Style = getStyleWithColumns(65);
222*0a6a1f1dSLionel Sambuc   Style.Cpp11BracedListStyle = false;
223*0a6a1f1dSLionel Sambuc   verifyFormat(
224*0a6a1f1dSLionel Sambuc       "expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n"
225*0a6a1f1dSLionel Sambuc       "  100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };",
226*0a6a1f1dSLionel Sambuc       Style);
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,ThrowsDeclarations)229*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, ThrowsDeclarations) {
230*0a6a1f1dSLionel Sambuc   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
231*0a6a1f1dSLionel Sambuc                "    throws LooooooooooooooooooooooooooooongException {}");
232*0a6a1f1dSLionel Sambuc   verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
233*0a6a1f1dSLionel Sambuc                "    throws LoooooooooongException, LooooooooooongException {}");
234*0a6a1f1dSLionel Sambuc }
235*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,Annotations)236*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, Annotations) {
237*0a6a1f1dSLionel Sambuc   verifyFormat("@Override\n"
238*0a6a1f1dSLionel Sambuc                "public String toString() {}");
239*0a6a1f1dSLionel Sambuc   verifyFormat("@Override\n"
240*0a6a1f1dSLionel Sambuc                "@Nullable\n"
241*0a6a1f1dSLionel Sambuc                "public String getNameIfPresent() {}");
242*0a6a1f1dSLionel Sambuc   verifyFormat("@Override // comment\n"
243*0a6a1f1dSLionel Sambuc                "@Nullable\n"
244*0a6a1f1dSLionel Sambuc                "public String getNameIfPresent() {}");
245*0a6a1f1dSLionel Sambuc   verifyFormat("@java.lang.Override // comment\n"
246*0a6a1f1dSLionel Sambuc                "@Nullable\n"
247*0a6a1f1dSLionel Sambuc                "public String getNameIfPresent() {}");
248*0a6a1f1dSLionel Sambuc 
249*0a6a1f1dSLionel Sambuc   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
250*0a6a1f1dSLionel Sambuc                "public void doSomething() {}");
251*0a6a1f1dSLionel Sambuc   verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
252*0a6a1f1dSLionel Sambuc                "@Author(name = \"abc\")\n"
253*0a6a1f1dSLionel Sambuc                "public void doSomething() {}");
254*0a6a1f1dSLionel Sambuc 
255*0a6a1f1dSLionel Sambuc   verifyFormat("DoSomething(new A() {\n"
256*0a6a1f1dSLionel Sambuc                "  @Override\n"
257*0a6a1f1dSLionel Sambuc                "  public String toString() {}\n"
258*0a6a1f1dSLionel Sambuc                "});");
259*0a6a1f1dSLionel Sambuc 
260*0a6a1f1dSLionel Sambuc   verifyFormat("void SomeFunction(@Nullable String something) {}");
261*0a6a1f1dSLionel Sambuc   verifyFormat("void SomeFunction(@org.llvm.Nullable String something) {}");
262*0a6a1f1dSLionel Sambuc 
263*0a6a1f1dSLionel Sambuc   verifyFormat("@Partial @Mock DataLoader loader;");
264*0a6a1f1dSLionel Sambuc   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
265*0a6a1f1dSLionel Sambuc                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
266*0a6a1f1dSLionel Sambuc 
267*0a6a1f1dSLionel Sambuc   verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
268*0a6a1f1dSLionel Sambuc                "private static final long something = 0L;");
269*0a6a1f1dSLionel Sambuc   verifyFormat("@org.llvm.Qualified(\"With some really looooooooooong text\")\n"
270*0a6a1f1dSLionel Sambuc                "private static final long something = 0L;");
271*0a6a1f1dSLionel Sambuc   verifyFormat("@Mock\n"
272*0a6a1f1dSLionel Sambuc                "DataLoader loooooooooooooooooooooooader =\n"
273*0a6a1f1dSLionel Sambuc                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
274*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
275*0a6a1f1dSLionel Sambuc   verifyFormat("@org.llvm.QualifiedMock\n"
276*0a6a1f1dSLionel Sambuc                "DataLoader loooooooooooooooooooooooader =\n"
277*0a6a1f1dSLionel Sambuc                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
278*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
279*0a6a1f1dSLionel Sambuc   verifyFormat("@Test(a)\n"
280*0a6a1f1dSLionel Sambuc                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
281*0a6a1f1dSLionel Sambuc                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa);");
282*0a6a1f1dSLionel Sambuc   verifyFormat("@SomeAnnotation(\n"
283*0a6a1f1dSLionel Sambuc                "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa)\n"
284*0a6a1f1dSLionel Sambuc                "int i;",
285*0a6a1f1dSLionel Sambuc                getStyleWithColumns(50));
286*0a6a1f1dSLionel Sambuc   verifyFormat("@Test\n"
287*0a6a1f1dSLionel Sambuc                "ReturnType doSomething(\n"
288*0a6a1f1dSLionel Sambuc                "    String aaaaaaaaaaaaa, String bbbbbbbbbbbbbbb) {}",
289*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
290*0a6a1f1dSLionel Sambuc   verifyFormat("{\n"
291*0a6a1f1dSLionel Sambuc                "  boolean someFunction(\n"
292*0a6a1f1dSLionel Sambuc                "      @Param(aaaaaaaaaaaaaaaa) String aaaaa,\n"
293*0a6a1f1dSLionel Sambuc                "      String bbbbbbbbbbbbbbb) {}\n"
294*0a6a1f1dSLionel Sambuc                "}",
295*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
296*0a6a1f1dSLionel Sambuc }
297*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,Generics)298*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, Generics) {
299*0a6a1f1dSLionel Sambuc   verifyFormat("Iterable<?> a;");
300*0a6a1f1dSLionel Sambuc   verifyFormat("Iterable<?> a;");
301*0a6a1f1dSLionel Sambuc   verifyFormat("Iterable<? extends SomeObject> a;");
302*0a6a1f1dSLionel Sambuc 
303*0a6a1f1dSLionel Sambuc   verifyFormat("A.<B>doSomething();");
304*0a6a1f1dSLionel Sambuc 
305*0a6a1f1dSLionel Sambuc   verifyFormat("@Override\n"
306*0a6a1f1dSLionel Sambuc                "public Map<String, ?> getAll() {}");
307*0a6a1f1dSLionel Sambuc 
308*0a6a1f1dSLionel Sambuc   verifyFormat("public <R> ArrayList<R> get() {}");
309*0a6a1f1dSLionel Sambuc   verifyFormat("protected <R> ArrayList<R> get() {}");
310*0a6a1f1dSLionel Sambuc   verifyFormat("private <R> ArrayList<R> get() {}");
311*0a6a1f1dSLionel Sambuc   verifyFormat("public static <R> ArrayList<R> get() {}");
312*0a6a1f1dSLionel Sambuc   verifyFormat("public static native <R> ArrayList<R> get();");
313*0a6a1f1dSLionel Sambuc   verifyFormat("public final <X> Foo foo() {}");
314*0a6a1f1dSLionel Sambuc   verifyFormat("public abstract <X> Foo foo();");
315*0a6a1f1dSLionel Sambuc   verifyFormat("<T extends B> T getInstance(Class<T> type);");
316*0a6a1f1dSLionel Sambuc   verifyFormat("Function<F, ? extends T> function;");
317*0a6a1f1dSLionel Sambuc 
318*0a6a1f1dSLionel Sambuc   verifyFormat("private Foo<X, Y>[] foos;");
319*0a6a1f1dSLionel Sambuc   verifyFormat("Foo<X, Y>[] foos = this.foos;");
320*0a6a1f1dSLionel Sambuc   verifyFormat("return (a instanceof List<?>)\n"
321*0a6a1f1dSLionel Sambuc                "    ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
322*0a6a1f1dSLionel Sambuc                "    : aaaaaaaaaaaaaaaaaaaaaaa;",
323*0a6a1f1dSLionel Sambuc                getStyleWithColumns(60));
324*0a6a1f1dSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc   verifyFormat(
326*0a6a1f1dSLionel Sambuc       "SomeLoooooooooooooooooooooongType name =\n"
327*0a6a1f1dSLionel Sambuc       "    SomeType.foo(someArgument)\n"
328*0a6a1f1dSLionel Sambuc       "        .<X>method()\n"
329*0a6a1f1dSLionel Sambuc       "        .aaaaaaaaaaaaaaaaaaa()\n"
330*0a6a1f1dSLionel Sambuc       "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
331*0a6a1f1dSLionel Sambuc }
332*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,StringConcatenation)333*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, StringConcatenation) {
334*0a6a1f1dSLionel Sambuc   verifyFormat("String someString = \"abc\"\n"
335*0a6a1f1dSLionel Sambuc                "    + \"cde\";");
336*0a6a1f1dSLionel Sambuc }
337*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,TryCatchFinally)338*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, TryCatchFinally) {
339*0a6a1f1dSLionel Sambuc   verifyFormat("try {\n"
340*0a6a1f1dSLionel Sambuc                "  Something();\n"
341*0a6a1f1dSLionel Sambuc                "} catch (SomeException e) {\n"
342*0a6a1f1dSLionel Sambuc                "  HandleException(e);\n"
343*0a6a1f1dSLionel Sambuc                "}");
344*0a6a1f1dSLionel Sambuc   verifyFormat("try {\n"
345*0a6a1f1dSLionel Sambuc                "  Something();\n"
346*0a6a1f1dSLionel Sambuc                "} finally {\n"
347*0a6a1f1dSLionel Sambuc                "  AlwaysDoThis();\n"
348*0a6a1f1dSLionel Sambuc                "}");
349*0a6a1f1dSLionel Sambuc   verifyFormat("try {\n"
350*0a6a1f1dSLionel Sambuc                "  Something();\n"
351*0a6a1f1dSLionel Sambuc                "} catch (SomeException e) {\n"
352*0a6a1f1dSLionel Sambuc                "  HandleException(e);\n"
353*0a6a1f1dSLionel Sambuc                "} finally {\n"
354*0a6a1f1dSLionel Sambuc                "  AlwaysDoThis();\n"
355*0a6a1f1dSLionel Sambuc                "}");
356*0a6a1f1dSLionel Sambuc 
357*0a6a1f1dSLionel Sambuc   verifyFormat("try {\n"
358*0a6a1f1dSLionel Sambuc                "  Something();\n"
359*0a6a1f1dSLionel Sambuc                "} catch (SomeException | OtherException e) {\n"
360*0a6a1f1dSLionel Sambuc                "  HandleException(e);\n"
361*0a6a1f1dSLionel Sambuc                "}");
362*0a6a1f1dSLionel Sambuc }
363*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,TryWithResources)364*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, TryWithResources) {
365*0a6a1f1dSLionel Sambuc   verifyFormat("try (SomeResource rs = someFunction()) {\n"
366*0a6a1f1dSLionel Sambuc                "  Something();\n"
367*0a6a1f1dSLionel Sambuc                "}");
368*0a6a1f1dSLionel Sambuc   verifyFormat("try (SomeResource rs = someFunction()) {\n"
369*0a6a1f1dSLionel Sambuc                "  Something();\n"
370*0a6a1f1dSLionel Sambuc                "} catch (SomeException e) {\n"
371*0a6a1f1dSLionel Sambuc                "  HandleException(e);\n"
372*0a6a1f1dSLionel Sambuc                "}");
373*0a6a1f1dSLionel Sambuc }
374*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,SynchronizedKeyword)375*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, SynchronizedKeyword) {
376*0a6a1f1dSLionel Sambuc   verifyFormat("synchronized (mData) {\n"
377*0a6a1f1dSLionel Sambuc                "  // ...\n"
378*0a6a1f1dSLionel Sambuc                "}");
379*0a6a1f1dSLionel Sambuc }
380*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,PackageDeclarations)381*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, PackageDeclarations) {
382*0a6a1f1dSLionel Sambuc   verifyFormat("package some.really.loooooooooooooooooooooong.package;",
383*0a6a1f1dSLionel Sambuc                getStyleWithColumns(50));
384*0a6a1f1dSLionel Sambuc }
385*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,ImportDeclarations)386*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, ImportDeclarations) {
387*0a6a1f1dSLionel Sambuc   verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
388*0a6a1f1dSLionel Sambuc                getStyleWithColumns(50));
389*0a6a1f1dSLionel Sambuc   verifyFormat("import static some.really.looooooooooooooooong.imported.Class;",
390*0a6a1f1dSLionel Sambuc                getStyleWithColumns(50));
391*0a6a1f1dSLionel Sambuc }
392*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,MethodDeclarations)393*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, MethodDeclarations) {
394*0a6a1f1dSLionel Sambuc   verifyFormat("void methodName(Object arg1,\n"
395*0a6a1f1dSLionel Sambuc                "    Object arg2, Object arg3) {}",
396*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
397*0a6a1f1dSLionel Sambuc   verifyFormat("void methodName(\n"
398*0a6a1f1dSLionel Sambuc                "    Object arg1, Object arg2) {}",
399*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
400*0a6a1f1dSLionel Sambuc }
401*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,CppKeywords)402*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, CppKeywords) {
403*0a6a1f1dSLionel Sambuc   verifyFormat("public void union(Type a, Type b);");
404*0a6a1f1dSLionel Sambuc   verifyFormat("public void struct(Object o);");
405*0a6a1f1dSLionel Sambuc   verifyFormat("public void delete(Object o);");
406*0a6a1f1dSLionel Sambuc }
407*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,NeverAlignAfterReturn)408*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, NeverAlignAfterReturn) {
409*0a6a1f1dSLionel Sambuc   verifyFormat("return aaaaaaaaaaaaaaaaaaa\n"
410*0a6a1f1dSLionel Sambuc                "    && bbbbbbbbbbbbbbbbbbb\n"
411*0a6a1f1dSLionel Sambuc                "    && ccccccccccccccccccc;",
412*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
413*0a6a1f1dSLionel Sambuc   verifyFormat("return (result == null)\n"
414*0a6a1f1dSLionel Sambuc                "    ? aaaaaaaaaaaaaaaaa\n"
415*0a6a1f1dSLionel Sambuc                "    : bbbbbbbbbbbbbbbbb;",
416*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
417*0a6a1f1dSLionel Sambuc   verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
418*0a6a1f1dSLionel Sambuc                "    .bbbbbbbbbbbbbbbbbbb()\n"
419*0a6a1f1dSLionel Sambuc                "    .ccccccccccccccccccc();",
420*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
421*0a6a1f1dSLionel Sambuc   verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
422*0a6a1f1dSLionel Sambuc                "    .bbbbbbbbbbbbbbbbbbb(\n"
423*0a6a1f1dSLionel Sambuc                "         ccccccccccccccc)\n"
424*0a6a1f1dSLionel Sambuc                "    .ccccccccccccccccccc();",
425*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
426*0a6a1f1dSLionel Sambuc }
427*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,FormatsInnerBlocks)428*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, FormatsInnerBlocks) {
429*0a6a1f1dSLionel Sambuc   verifyFormat("someObject.someFunction(new Runnable() {\n"
430*0a6a1f1dSLionel Sambuc                "  @Override\n"
431*0a6a1f1dSLionel Sambuc                "  public void run() {\n"
432*0a6a1f1dSLionel Sambuc                "    System.out.println(42);\n"
433*0a6a1f1dSLionel Sambuc                "  }\n"
434*0a6a1f1dSLionel Sambuc                "}, someOtherParameter);");
435*0a6a1f1dSLionel Sambuc   verifyFormat("someFunction(new Runnable() {\n"
436*0a6a1f1dSLionel Sambuc                "  public void run() {\n"
437*0a6a1f1dSLionel Sambuc                "    System.out.println(42);\n"
438*0a6a1f1dSLionel Sambuc                "  }\n"
439*0a6a1f1dSLionel Sambuc                "});");
440*0a6a1f1dSLionel Sambuc   verifyFormat("someObject.someFunction(\n"
441*0a6a1f1dSLionel Sambuc                "    new Runnable() {\n"
442*0a6a1f1dSLionel Sambuc                "      @Override\n"
443*0a6a1f1dSLionel Sambuc                "      public void run() {\n"
444*0a6a1f1dSLionel Sambuc                "        System.out.println(42);\n"
445*0a6a1f1dSLionel Sambuc                "      }\n"
446*0a6a1f1dSLionel Sambuc                "    },\n"
447*0a6a1f1dSLionel Sambuc                "    new Runnable() {\n"
448*0a6a1f1dSLionel Sambuc                "      @Override\n"
449*0a6a1f1dSLionel Sambuc                "      public void run() {\n"
450*0a6a1f1dSLionel Sambuc                "        System.out.println(43);\n"
451*0a6a1f1dSLionel Sambuc                "      }\n"
452*0a6a1f1dSLionel Sambuc                "    },\n"
453*0a6a1f1dSLionel Sambuc                "    someOtherParameter);");
454*0a6a1f1dSLionel Sambuc }
455*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,FormatsLambdas)456*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, FormatsLambdas) {
457*0a6a1f1dSLionel Sambuc   verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
458*0a6a1f1dSLionel Sambuc   verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
459*0a6a1f1dSLionel Sambuc                "    -> aaaaaaaaaa + bbbbbbbbbb;",
460*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
461*0a6a1f1dSLionel Sambuc   verifyFormat("Runnable someLambda = () -> DoSomething();");
462*0a6a1f1dSLionel Sambuc   verifyFormat("Runnable someLambda = () -> {\n"
463*0a6a1f1dSLionel Sambuc                "  DoSomething();\n"
464*0a6a1f1dSLionel Sambuc                "}");
465*0a6a1f1dSLionel Sambuc 
466*0a6a1f1dSLionel Sambuc   verifyFormat("Runnable someLambda =\n"
467*0a6a1f1dSLionel Sambuc                "    (int aaaaa) -> DoSomething(aaaaa);",
468*0a6a1f1dSLionel Sambuc                getStyleWithColumns(40));
469*0a6a1f1dSLionel Sambuc }
470*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,BreaksStringLiterals)471*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, BreaksStringLiterals) {
472*0a6a1f1dSLionel Sambuc   // FIXME: String literal breaking is currently disabled for Java and JS, as it
473*0a6a1f1dSLionel Sambuc   // requires strings to be merged using "+" which we don't support.
474*0a6a1f1dSLionel Sambuc   EXPECT_EQ("\"some text other\";",
475*0a6a1f1dSLionel Sambuc             format("\"some text other\";", getStyleWithColumns(14)));
476*0a6a1f1dSLionel Sambuc }
477*0a6a1f1dSLionel Sambuc 
TEST_F(FormatTestJava,AlignsBlockComments)478*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJava, AlignsBlockComments) {
479*0a6a1f1dSLionel Sambuc   EXPECT_EQ("/*\n"
480*0a6a1f1dSLionel Sambuc             " * Really multi-line\n"
481*0a6a1f1dSLionel Sambuc             " * comment.\n"
482*0a6a1f1dSLionel Sambuc             " */\n"
483*0a6a1f1dSLionel Sambuc             "void f() {}",
484*0a6a1f1dSLionel Sambuc             format("  /*\n"
485*0a6a1f1dSLionel Sambuc                    "   * Really multi-line\n"
486*0a6a1f1dSLionel Sambuc                    "   * comment.\n"
487*0a6a1f1dSLionel Sambuc                    "   */\n"
488*0a6a1f1dSLionel Sambuc                    "  void f() {}"));
489*0a6a1f1dSLionel Sambuc }
490*0a6a1f1dSLionel Sambuc 
491*0a6a1f1dSLionel Sambuc } // end namespace tooling
492*0a6a1f1dSLionel Sambuc } // end namespace clang
493