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, 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 "}"); 66 verifyFormat("public class A extends B.C {}"); 67 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)); 80 verifyFormat("@SomeAnnotation()\n" 81 "abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb\n" 82 " implements cccccccccccc {\n" 83 "}", 84 getStyleWithColumns(76)); 85 verifyFormat("@SomeAnnotation()\n" 86 "abstract class aaaaaaaaa<a> extends bbbbbbbbbbbb<b>\n" 87 " implements cccccccccccc {\n" 88 "}", 89 getStyleWithColumns(76)); 90 verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n" 91 " void doStuff(int theStuff);\n" 92 " void doMoreStuff(int moreStuff);\n" 93 "}"); 94 verifyFormat("public interface SomeInterface {\n" 95 " void doStuff(int theStuff);\n" 96 " void doMoreStuff(int moreStuff);\n" 97 "}"); 98 verifyFormat("@interface SomeInterface {\n" 99 " void doStuff(int theStuff);\n" 100 " void doMoreStuff(int moreStuff);\n" 101 "}"); 102 verifyFormat("public @interface SomeInterface {\n" 103 " void doStuff(int theStuff);\n" 104 " void doMoreStuff(int moreStuff);\n" 105 "}"); 106 } 107 108 TEST_F(FormatTestJava, EnumDeclarations) { 109 verifyFormat("enum SomeThing { ABC, CDE }"); 110 verifyFormat("enum SomeThing {\n" 111 " ABC,\n" 112 " CDE,\n" 113 "}"); 114 verifyFormat("public class SomeClass {\n" 115 " enum SomeThing { ABC, CDE }\n" 116 " void f() {\n" 117 " }\n" 118 "}"); 119 verifyFormat("public class SomeClass implements SomeInterface {\n" 120 " enum SomeThing { ABC, CDE }\n" 121 " void f() {\n" 122 " }\n" 123 "}"); 124 verifyFormat("enum SomeThing {\n" 125 " ABC,\n" 126 " CDE;\n" 127 " void f() {\n" 128 " }\n" 129 "}"); 130 verifyFormat("enum SomeThing {\n" 131 " ABC(1, \"ABC\"),\n" 132 " CDE(2, \"CDE\");\n" 133 " Something(int i, String s) {\n" 134 " }\n" 135 "}"); 136 verifyFormat("enum SomeThing {\n" 137 " ABC(new int[]{1, 2}),\n" 138 " CDE(new int[]{2, 3});\n" 139 " Something(int[] i) {\n" 140 " }\n" 141 "}"); 142 verifyFormat("public enum SomeThing {\n" 143 " ABC {\n" 144 " public String toString() {\n" 145 " return \"ABC\";\n" 146 " }\n" 147 " },\n" 148 " CDE {\n" 149 " @Override\n" 150 " public String toString() {\n" 151 " return \"CDE\";\n" 152 " }\n" 153 " };\n" 154 " public void f() {\n" 155 " }\n" 156 "}"); 157 } 158 159 TEST_F(FormatTestJava, ThrowsDeclarations) { 160 verifyFormat("public void doSooooooooooooooooooooooooooomething()\n" 161 " throws LooooooooooooooooooooooooooooongException {\n}"); 162 } 163 164 TEST_F(FormatTestJava, Annotations) { 165 verifyFormat("@Override\n" 166 "public String toString() {\n}"); 167 verifyFormat("@Override\n" 168 "@Nullable\n" 169 "public String getNameIfPresent() {\n}"); 170 171 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" 172 "public void doSomething() {\n}"); 173 verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" 174 "@Author(name = \"abc\")\n" 175 "public void doSomething() {\n}"); 176 177 verifyFormat("DoSomething(new A() {\n" 178 " @Override\n" 179 " public String toString() {\n" 180 " }\n" 181 "});"); 182 183 verifyFormat("void SomeFunction(@Nullable String something) {\n" 184 "}"); 185 186 verifyFormat("@Partial @Mock DataLoader loader;"); 187 verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n" 188 "public static int iiiiiiiiiiiiiiiiiiiiiiii;"); 189 190 verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n" 191 "private static final long something = 0L;"); 192 } 193 194 TEST_F(FormatTestJava, Generics) { 195 verifyFormat("Iterable<?> a;"); 196 verifyFormat("Iterable<?> a;"); 197 verifyFormat("Iterable<? extends SomeObject> a;"); 198 199 verifyFormat("A.<B>doSomething();"); 200 201 verifyFormat("@Override\n" 202 "public Map<String, ?> getAll() {\n}"); 203 204 verifyFormat("public static <R> ArrayList<R> get() {\n}"); 205 verifyFormat("<T extends B> T getInstance(Class<T> type);"); 206 verifyFormat("Function<F, ? extends T> function;"); 207 } 208 209 TEST_F(FormatTestJava, StringConcatenation) { 210 verifyFormat("String someString = \"abc\"\n" 211 " + \"cde\";"); 212 } 213 214 TEST_F(FormatTestJava, TryCatchFinally) { 215 verifyFormat("try {\n" 216 " Something();\n" 217 "} catch (SomeException e) {\n" 218 " HandleException(e);\n" 219 "}"); 220 verifyFormat("try {\n" 221 " Something();\n" 222 "} finally {\n" 223 " AlwaysDoThis();\n" 224 "}"); 225 verifyFormat("try {\n" 226 " Something();\n" 227 "} catch (SomeException e) {\n" 228 " HandleException(e);\n" 229 "} finally {\n" 230 " AlwaysDoThis();\n" 231 "}"); 232 233 verifyFormat("try {\n" 234 " Something();\n" 235 "} catch (SomeException | OtherException e) {\n" 236 " HandleException(e);\n" 237 "}"); 238 } 239 240 TEST_F(FormatTestJava, SynchronizedKeyword) { 241 verifyFormat("synchronized (mData) {\n" 242 " // ...\n" 243 "}"); 244 } 245 246 TEST_F(FormatTestJava, ImportDeclarations) { 247 verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;", 248 getStyleWithColumns(50)); 249 } 250 251 } // end namespace tooling 252 } // end namespace clang 253