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