blob: a47bfcaab84bbbf1648fad74c552d500ea6be5e3 [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
53TEST_F(FormatTestJava, ClassDeclarations) {
54 verifyFormat("public class SomeClass {\n"
55 " private int a;\n"
56 " private int b;\n"
57 "}");
58 verifyFormat("public class A {\n"
59 " class B {\n"
60 " int i;\n"
61 " }\n"
62 " class C {\n"
63 " int j;\n"
64 " }\n"
65 "}");
Daniel Jasper4bf9d472014-10-21 09:31:29 +000066 verifyFormat("public class A extends B.C {}");
Daniel Jasper50b4bd72014-11-02 19:16:41 +000067
68 verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
69 " implements SomeInterface {}",
70 getStyleWithColumns(60));
71 verifyFormat("abstract class SomeClass\n"
72 " extends SomeOtherClass\n"
73 " implements SomeInterface {}",
74 getStyleWithColumns(40));
75 verifyFormat("abstract class SomeClass\n"
76 " extends SomeOtherClass\n"
77 " implements SomeInterface,\n"
78 " AnotherInterface {}",
79 getStyleWithColumns(40));
Daniel Jasper39af6cd2014-11-03 02:27:28 +000080 verifyFormat("@SomeAnnotation()\n"
81 "abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb\n"
82 " implements cccccccccccc {\n"
83 "}",
84 getStyleWithColumns(76));
Daniel Jasperc58c70e2014-09-15 11:21:46 +000085}
86
Daniel Jasperdf2ff002014-11-02 22:31:39 +000087TEST_F(FormatTestJava, EnumDeclarations) {
88 verifyFormat("enum SomeThing { ABC, CDE }");
89 verifyFormat("enum SomeThing {\n"
90 " ABC,\n"
91 " CDE,\n"
92 "}");
93 verifyFormat("public class SomeClass {\n"
94 " enum SomeThing { ABC, CDE }\n"
95 " void f() {\n"
96 " }\n"
97 "}");
98}
99
Daniel Jasperf26c7552014-10-17 13:36:14 +0000100TEST_F(FormatTestJava, ThrowsDeclarations) {
101 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
Daniel Jaspere003b782014-10-28 16:29:56 +0000102 " throws LooooooooooooooooooooooooooooongException {\n}");
Daniel Jasperf26c7552014-10-17 13:36:14 +0000103}
104
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000105TEST_F(FormatTestJava, Annotations) {
106 verifyFormat("@Override\n"
107 "public String toString() {\n}");
108 verifyFormat("@Override\n"
109 "@Nullable\n"
110 "public String getNameIfPresent() {\n}");
Daniel Jasperf1f0c352014-10-21 09:25:39 +0000111
112 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
113 "public void doSomething() {\n}");
Daniel Jasperd78c4222014-10-21 11:17:56 +0000114 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
115 "@Author(name = \"abc\")\n"
116 "public void doSomething() {\n}");
Daniel Jasperf1f0c352014-10-21 09:25:39 +0000117
Daniel Jasperc7d024a2014-10-21 10:02:03 +0000118 verifyFormat("DoSomething(new A() {\n"
119 " @Override\n"
120 " public String toString() {\n"
121 " }\n"
122 "});");
123
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000124 verifyFormat("void SomeFunction(@Nullable String something) {\n"
125 "}");
126
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000127 verifyFormat("@Partial @Mock DataLoader loader;");
Daniel Jasperfd681912014-10-21 10:58:14 +0000128 verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
129 "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000130
131 verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
132 "private static final long something = 0L;");
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000133}
134
Daniel Jasper16b107e2014-10-21 09:57:09 +0000135TEST_F(FormatTestJava, Generics) {
136 verifyFormat("Iterable<?> a;");
137 verifyFormat("Iterable<?> a;");
138 verifyFormat("Iterable<? extends SomeObject> a;");
Daniel Jasper5ffcb7f2014-10-21 11:13:31 +0000139
140 verifyFormat("A.<B>doSomething();");
Daniel Jasper7bd618f2014-11-02 21:52:57 +0000141
142 verifyFormat("@Override\n"
Daniel Jasperdb9a7a22014-11-03 02:35:14 +0000143 "public Map<String, ?> getAll() {\n}");
144
145 verifyFormat("public static <R> ArrayList<R> get() {\n}");
Daniel Jasper16b107e2014-10-21 09:57:09 +0000146}
147
Daniel Jasperc0126862014-10-21 11:34:53 +0000148TEST_F(FormatTestJava, StringConcatenation) {
149 verifyFormat("String someString = \"abc\"\n"
150 " + \"cde\";");
151}
152
Daniel Jaspera3ddf862014-11-02 19:21:48 +0000153TEST_F(FormatTestJava, TryCatchFinally) {
154 verifyFormat("try {\n"
155 " Something();\n"
156 "} catch (SomeException e) {\n"
157 " HandleException(e);\n"
158 "}");
159 verifyFormat("try {\n"
160 " Something();\n"
161 "} finally {\n"
162 " AlwaysDoThis();\n"
163 "}");
164 verifyFormat("try {\n"
165 " Something();\n"
166 "} catch (SomeException e) {\n"
167 " HandleException(e);\n"
168 "} finally {\n"
169 " AlwaysDoThis();\n"
170 "}");
171
172 verifyFormat("try {\n"
173 " Something();\n"
174 "} catch (SomeException | OtherException e) {\n"
175 " HandleException(e);\n"
176 "}");
177}
178
Daniel Jasperb9d3db62014-11-02 22:00:57 +0000179TEST_F(FormatTestJava, SynchronizedKeyword) {
180 verifyFormat("synchronized (mData) {\n"
181 " // ...\n"
182 "}");
183}
184
Daniel Jasper5e7be1d2014-11-02 22:13:03 +0000185TEST_F(FormatTestJava, ImportDeclarations) {
186 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
187 getStyleWithColumns(50));
188}
189
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000190} // end namespace tooling
191} // end namespace clang