Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
| 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 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 11 | #include "../Tooling/RewriterTestContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace format { |
| 17 | |
| 18 | class FormatTest : public ::testing::Test { |
| 19 | protected: |
| 20 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 21 | const FormatStyle &Style) { |
| 22 | RewriterTestContext Context; |
| 23 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 24 | SourceLocation Start = |
| 25 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 26 | std::vector<CharSourceRange> Ranges( |
| 27 | 1, |
| 28 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
| 29 | LangOptions LangOpts; |
| 30 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 31 | LangOpts.CPlusPlus11 = 1; |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 32 | LangOpts.ObjC1 = 1; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 33 | LangOpts.ObjC2 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 35 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
| 36 | Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 37 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 38 | return Context.getRewrittenText(ID); |
| 39 | } |
| 40 | |
| 41 | std::string format(llvm::StringRef Code, |
| 42 | const FormatStyle &Style = getLLVMStyle()) { |
| 43 | return format(Code, 0, Code.size(), Style); |
| 44 | } |
| 45 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 46 | std::string messUp(llvm::StringRef Code) { |
| 47 | std::string MessedUp(Code.str()); |
| 48 | bool InComment = false; |
| 49 | bool JustReplacedNewline = false; |
| 50 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 51 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 52 | if (JustReplacedNewline) |
| 53 | MessedUp[i - 1] = '\n'; |
| 54 | InComment = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 55 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 56 | MessedUp[i] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 57 | } else if (MessedUp[i] == '\n') { |
| 58 | if (InComment) { |
| 59 | InComment = false; |
| 60 | } else { |
| 61 | JustReplacedNewline = true; |
| 62 | MessedUp[i] = ' '; |
| 63 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | } else if (MessedUp[i] != ' ') { |
| 65 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 66 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 67 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 68 | return MessedUp; |
| 69 | } |
| 70 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 71 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 72 | FormatStyle Style = getLLVMStyle(); |
| 73 | Style.ColumnLimit = ColumnLimit; |
| 74 | return Style; |
| 75 | } |
| 76 | |
| 77 | void verifyFormat(llvm::StringRef Code, |
| 78 | const FormatStyle &Style = getLLVMStyle()) { |
| 79 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 83 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 87 | //===----------------------------------------------------------------------===// |
| 88 | // Basic function tests. |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 91 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 92 | EXPECT_EQ(";", format(";")); |
| 93 | } |
| 94 | |
| 95 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 96 | EXPECT_EQ("int i;", format(" int i;")); |
| 97 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 98 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 99 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 100 | } |
| 101 | |
| 102 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 103 | EXPECT_EQ("int i;", format("int\ni;")); |
| 104 | } |
| 105 | |
| 106 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 107 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 108 | } |
| 109 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 110 | TEST_F(FormatTest, FormatsNestedCall) { |
| 111 | verifyFormat("Method(f1, f2(f3));"); |
| 112 | verifyFormat("Method(f1(f2, f3()));"); |
| 113 | } |
| 114 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | // Tests for control statements. |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
| 119 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 120 | verifyFormat("if (true)\n f();\ng();"); |
| 121 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 122 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 123 | verifyFormat("if (a)\n" |
| 124 | " // comment\n" |
| 125 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(FormatTest, ParseIfElse) { |
| 129 | verifyFormat("if (true)\n" |
| 130 | " if (true)\n" |
| 131 | " if (true)\n" |
| 132 | " f();\n" |
| 133 | " else\n" |
| 134 | " g();\n" |
| 135 | " else\n" |
| 136 | " h();\n" |
| 137 | "else\n" |
| 138 | " i();"); |
| 139 | verifyFormat("if (true)\n" |
| 140 | " if (true)\n" |
| 141 | " if (true) {\n" |
| 142 | " if (true)\n" |
| 143 | " f();\n" |
| 144 | " } else {\n" |
| 145 | " g();\n" |
| 146 | " }\n" |
| 147 | " else\n" |
| 148 | " h();\n" |
| 149 | "else {\n" |
| 150 | " i();\n" |
| 151 | "}"); |
| 152 | } |
| 153 | |
| 154 | TEST_F(FormatTest, ElseIf) { |
| 155 | verifyFormat("if (a) {\n" |
| 156 | "} else if (b) {\n" |
| 157 | "}"); |
| 158 | verifyFormat("if (a)\n" |
| 159 | " f();\n" |
| 160 | "else if (b)\n" |
| 161 | " g();\n" |
| 162 | "else\n" |
| 163 | " h();"); |
| 164 | } |
| 165 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 166 | TEST_F(FormatTest, FormatsForLoop) { |
| 167 | verifyFormat( |
| 168 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 169 | " ++VeryVeryLongLoopVariable)\n" |
| 170 | " ;"); |
| 171 | verifyFormat("for (;;)\n" |
| 172 | " f();"); |
| 173 | verifyFormat("for (;;) {\n" |
| 174 | "}"); |
| 175 | verifyFormat("for (;;) {\n" |
| 176 | " f();\n" |
| 177 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 178 | |
| 179 | verifyFormat( |
| 180 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 181 | " E = UnwrappedLines.end();\n" |
| 182 | " I != E; ++I) {\n}"); |
| 183 | |
| 184 | verifyFormat( |
| 185 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 186 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 190 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 191 | verifyFormat("while (true)\n" |
| 192 | " f();"); |
| 193 | verifyFormat("while () {\n" |
| 194 | "}"); |
| 195 | verifyFormat("while () {\n" |
| 196 | " f();\n" |
| 197 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 200 | TEST_F(FormatTest, FormatsDoWhile) { |
| 201 | verifyFormat("do {\n" |
| 202 | " do_something();\n" |
| 203 | "} while (something());"); |
| 204 | verifyFormat("do\n" |
| 205 | " do_something();\n" |
| 206 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 209 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 210 | verifyFormat("switch (x) {\n" |
| 211 | "case 1:\n" |
| 212 | " f();\n" |
| 213 | " break;\n" |
| 214 | "case kFoo:\n" |
| 215 | "case ns::kBar:\n" |
| 216 | "case kBaz:\n" |
| 217 | " break;\n" |
| 218 | "default:\n" |
| 219 | " g();\n" |
| 220 | " break;\n" |
| 221 | "}"); |
| 222 | verifyFormat("switch (x) {\n" |
| 223 | "case 1: {\n" |
| 224 | " f();\n" |
| 225 | " break;\n" |
| 226 | "}\n" |
| 227 | "}"); |
| 228 | verifyFormat("switch (test)\n" |
| 229 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 230 | verifyGoogleFormat("switch (x) {\n" |
| 231 | " case 1:\n" |
| 232 | " f();\n" |
| 233 | " break;\n" |
| 234 | " case kFoo:\n" |
| 235 | " case ns::kBar:\n" |
| 236 | " case kBaz:\n" |
| 237 | " break;\n" |
| 238 | " default:\n" |
| 239 | " g();\n" |
| 240 | " break;\n" |
| 241 | "}"); |
| 242 | verifyGoogleFormat("switch (x) {\n" |
| 243 | " case 1: {\n" |
| 244 | " f();\n" |
| 245 | " break;\n" |
| 246 | " }\n" |
| 247 | "}"); |
| 248 | verifyGoogleFormat("switch (test)\n" |
| 249 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 252 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 253 | verifyFormat("void f() {\n" |
| 254 | " some_code();\n" |
| 255 | "test_label:\n" |
| 256 | " some_other_code();\n" |
| 257 | " {\n" |
| 258 | " some_more_code();\n" |
| 259 | " another_label:\n" |
| 260 | " some_more_code();\n" |
| 261 | " }\n" |
| 262 | "}"); |
| 263 | verifyFormat("some_code();\n" |
| 264 | "test_label:\n" |
| 265 | "some_other_code();"); |
| 266 | } |
| 267 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // Tests for comments. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 273 | verifyFormat("// line 1\n" |
| 274 | "// line 2\n" |
| 275 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 276 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 277 | verifyFormat("void f() {\n" |
| 278 | " // Doesn't do anything\n" |
| 279 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 280 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 281 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 282 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 283 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 284 | verifyFormat("enum E {\n" |
| 285 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 286 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 287 | " VAL_B\n" |
| 288 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 289 | |
| 290 | verifyFormat( |
| 291 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 292 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 294 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 295 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 296 | |
| 297 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 301 | verifyFormat("f(/*test=*/ true);"); |
| 302 | } |
| 303 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 304 | //===----------------------------------------------------------------------===// |
| 305 | // Tests for classes, namespaces, etc. |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 309 | verifyFormat("class A {\n};"); |
| 310 | } |
| 311 | |
| 312 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 313 | verifyFormat("class A {\n" |
| 314 | "public:\n" |
| 315 | "protected:\n" |
| 316 | "private:\n" |
| 317 | " void f() {\n" |
| 318 | " }\n" |
| 319 | "};"); |
| 320 | verifyGoogleFormat("class A {\n" |
| 321 | " public:\n" |
| 322 | " protected:\n" |
| 323 | " private:\n" |
| 324 | " void f() {\n" |
| 325 | " }\n" |
| 326 | "};"); |
| 327 | } |
| 328 | |
| 329 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 330 | verifyFormat("class A : public B {\n" |
| 331 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 332 | verifyFormat("class A : public ::B {\n" |
| 333 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 336 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
| 337 | verifyFormat("class A {\n" |
| 338 | "} a, b;"); |
| 339 | verifyFormat("struct A {\n" |
| 340 | "} a, b;"); |
| 341 | } |
| 342 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 343 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 344 | verifyFormat("enum {\n" |
| 345 | " Zero,\n" |
| 346 | " One = 1,\n" |
| 347 | " Two = One + 1,\n" |
| 348 | " Three = (One + Two),\n" |
| 349 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 350 | " Five = (One, Two, Three, Four, 5)\n" |
| 351 | "};"); |
| 352 | verifyFormat("enum Enum {\n" |
| 353 | "};"); |
| 354 | verifyFormat("enum {\n" |
| 355 | "};"); |
| 356 | } |
| 357 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 358 | TEST_F(FormatTest, FormatsNamespaces) { |
| 359 | verifyFormat("namespace some_namespace {\n" |
| 360 | "class A {\n" |
| 361 | "};\n" |
| 362 | "void f() {\n" |
| 363 | " f();\n" |
| 364 | "}\n" |
| 365 | "}"); |
| 366 | verifyFormat("namespace {\n" |
| 367 | "class A {\n" |
| 368 | "};\n" |
| 369 | "void f() {\n" |
| 370 | " f();\n" |
| 371 | "}\n" |
| 372 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 373 | verifyFormat("inline namespace X {\n" |
| 374 | "class A {\n" |
| 375 | "};\n" |
| 376 | "void f() {\n" |
| 377 | " f();\n" |
| 378 | "}\n" |
| 379 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 380 | verifyFormat("using namespace some_namespace;\n" |
| 381 | "class A {\n" |
| 382 | "};\n" |
| 383 | "void f() {\n" |
| 384 | " f();\n" |
| 385 | "}"); |
| 386 | } |
| 387 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 388 | TEST_F(FormatTest, FormatTryCatch) { |
| 389 | verifyFormat("try {\n" |
| 390 | " throw a * b;\n" |
| 391 | "}\n" |
| 392 | "catch (int a) {\n" |
| 393 | " // Do nothing.\n" |
| 394 | "}\n" |
| 395 | "catch (...) {\n" |
| 396 | " exit(42);\n" |
| 397 | "}"); |
| 398 | |
| 399 | // Function-level try statements. |
| 400 | verifyFormat("int f() try {\n" |
| 401 | " return 4;\n" |
| 402 | "}\n" |
| 403 | "catch (...) {\n" |
| 404 | " return 5;\n" |
| 405 | "}"); |
| 406 | verifyFormat("class A {\n" |
| 407 | " int a;\n" |
| 408 | " A() try : a(0) {\n" |
| 409 | " }\n" |
| 410 | " catch (...) {\n" |
| 411 | " throw;\n" |
| 412 | " }\n" |
| 413 | "};\n"); |
| 414 | } |
| 415 | |
| 416 | TEST_F(FormatTest, FormatObjCTryCatch) { |
| 417 | verifyFormat("@try {\n" |
| 418 | " f();\n" |
| 419 | "}\n" |
| 420 | "@catch (NSException e) {\n" |
| 421 | " @throw;\n" |
| 422 | "}\n" |
| 423 | "@finally {\n" |
| 424 | " exit(42);\n" |
| 425 | "}"); |
| 426 | } |
| 427 | |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 428 | TEST_F(FormatTest, FormatObjCInterface) { |
| 429 | verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 430 | "@public\n" |
| 431 | " int field1;\n" |
| 432 | "@protected\n" |
| 433 | " int field2;\n" |
| 434 | "@private\n" |
| 435 | " int field3;\n" |
| 436 | "@package\n" |
| 437 | " int field4;\n" |
| 438 | "}\n" |
| 439 | "+ (id)init;\n" |
| 440 | "@end"); |
| 441 | |
| 442 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 443 | " @public\n" |
| 444 | " int field1;\n" |
| 445 | " @protected\n" |
| 446 | " int field2;\n" |
| 447 | " @private\n" |
| 448 | " int field3;\n" |
| 449 | " @package\n" |
| 450 | " int field4;\n" |
| 451 | "}\n" |
| 452 | "+ (id)init;\n" |
| 453 | "@end"); |
| 454 | } |
| 455 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 456 | TEST_F(FormatTest, StaticInitializers) { |
| 457 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 458 | |
| 459 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 460 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 461 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
| 462 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 465 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 466 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 467 | " \\\n" |
| 468 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 469 | } |
| 470 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 471 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 472 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 473 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 476 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 477 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 478 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 482 | EXPECT_EQ("#line 42 \"test\"\n", |
| 483 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 484 | EXPECT_EQ("#define A \\\n B\n", |
| 485 | format("# \\\n define \\\n A \\\n B\n", |
| 486 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 490 | EXPECT_EQ("#line 42 \"test\"", |
| 491 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 492 | EXPECT_EQ("#define A \\\n B", |
| 493 | format("# \\\n define \\\n A \\\n B", |
| 494 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 497 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 498 | // If the macro fits in one line, we still do not get the full |
| 499 | // line, as only the next line decides whether we need an escaped newline and |
| 500 | // thus use the last column. |
| 501 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 502 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 503 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 504 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 505 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 506 | } |
| 507 | |
| 508 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 509 | EXPECT_EQ("// some comment\n" |
| 510 | "#include \"a.h\"\n" |
| 511 | "#define A(A,\\\n" |
| 512 | " B)\n" |
| 513 | "#include \"b.h\"\n" |
| 514 | "// some comment\n", |
| 515 | format(" // some comment\n" |
| 516 | " #include \"a.h\"\n" |
| 517 | "#define A(A,\\\n" |
| 518 | " B)\n" |
| 519 | " #include \"b.h\"\n" |
| 520 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 523 | TEST_F(FormatTest, LayoutSingleHash) { |
| 524 | EXPECT_EQ("#\na;", format("#\na;")); |
| 525 | } |
| 526 | |
| 527 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 528 | EXPECT_EQ("#define A \\\n" |
| 529 | " c; \\\n" |
| 530 | " e;\n" |
| 531 | "f;", format("#define A c; e;\n" |
| 532 | "f;", getLLVMStyleWithColumns(14))); |
| 533 | } |
| 534 | |
| 535 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 536 | EXPECT_EQ("{\n}", format("{}")); |
| 537 | } |
| 538 | |
| 539 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 540 | EXPECT_EQ("# define A\\\n b;", |
| 541 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
| 545 | EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); |
| 546 | } |
| 547 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 548 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 549 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 550 | verifyFormat("#define A \\\n" |
| 551 | " { \\\n" |
| 552 | " f(#c);\\\n" |
| 553 | " }", getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 554 | |
| 555 | verifyFormat("#define A(X) \\\n" |
| 556 | " void function##X()", getLLVMStyleWithColumns(22)); |
| 557 | |
| 558 | verifyFormat("#define A(a, b, c) \\\n" |
| 559 | " void a##b##c()", getLLVMStyleWithColumns(22)); |
| 560 | |
| 561 | verifyFormat("#define A \\\n" |
| 562 | " void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 565 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 566 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 567 | } |
| 568 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 569 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 570 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 571 | } |
| 572 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 573 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 574 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 575 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 576 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 577 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 578 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 579 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 580 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 581 | EXPECT_EQ( |
| 582 | "#define A \\\n int i; \\\n int j;", |
| 583 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 586 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 587 | verifyFormat("#define A \\\n" |
| 588 | " int v( \\\n" |
| 589 | " a); \\\n" |
| 590 | " int i;", getLLVMStyleWithColumns(11)); |
| 591 | } |
| 592 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 593 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 594 | EXPECT_EQ( |
| 595 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 596 | " \\\n" |
| 597 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 598 | "\n" |
| 599 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 600 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 601 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 602 | "\\\n" |
| 603 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 604 | " \n" |
| 605 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 606 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 609 | //===----------------------------------------------------------------------===// |
| 610 | // Line break tests. |
| 611 | //===----------------------------------------------------------------------===// |
| 612 | |
| 613 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 614 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 615 | " int h, int j, int f,\n" |
| 616 | " int c, int ddddddddddddd) {\n" |
| 617 | "}"); |
| 618 | } |
| 619 | |
| 620 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 621 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 622 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 623 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 624 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 627 | TEST_F(FormatTest, ConstructorInitializers) { |
| 628 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 629 | |
| 630 | verifyFormat( |
| 631 | "SomeClass::Constructor()\n" |
| 632 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 633 | "}"); |
| 634 | |
| 635 | verifyFormat( |
| 636 | "SomeClass::Constructor()\n" |
| 637 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 638 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 639 | "}"); |
| 640 | |
| 641 | verifyFormat("Constructor()\n" |
| 642 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 643 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 644 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 645 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 646 | "}"); |
| 647 | |
| 648 | // Here a line could be saved by splitting the second initializer onto two |
| 649 | // lines, but that is not desireable. |
| 650 | verifyFormat("Constructor()\n" |
| 651 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 652 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 653 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 654 | "}"); |
| 655 | |
| 656 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 657 | " : some_var_(var), // 4 space indent\n" |
| 658 | " some_other_var_(var + 1) { // lined up\n" |
| 659 | "}"); |
| 660 | } |
| 661 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 662 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 663 | verifyFormat( |
| 664 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 665 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 666 | " f();"); |
| 667 | } |
| 668 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 669 | TEST_F(FormatTest, BreaksDesireably) { |
| 670 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 671 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 672 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 673 | |
| 674 | verifyFormat( |
| 675 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 676 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 677 | |
| 678 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 679 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 680 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 681 | |
| 682 | verifyFormat( |
| 683 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 684 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 685 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 686 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 687 | |
| 688 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 689 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 690 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 691 | verifyFormat( |
| 692 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 693 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 694 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 695 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 696 | // taking into account the StopAt value. |
| 697 | verifyFormat( |
| 698 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 699 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 700 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 701 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 702 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 703 | verifyFormat("{\n {\n {\n" |
| 704 | " Annotation.SpaceRequiredBefore =\n" |
| 705 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 706 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 707 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 710 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 711 | verifyFormat( |
| 712 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 713 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 714 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 715 | " ccccccccccccccccccccccccc) {\n}"); |
| 716 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 717 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 718 | verifyFormat( |
| 719 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 720 | " ccccccccccccccccccccccccc) {\n}"); |
| 721 | } |
| 722 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 723 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 724 | verifyFormat( |
| 725 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 726 | " SI->getAlignment(),\n" |
| 727 | " SI->getPointerAddressSpaceee());\n"); |
| 728 | verifyFormat( |
| 729 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 730 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 731 | " Line.Tokens.back().Tok.getLocation());"); |
| 732 | } |
| 733 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 734 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 735 | verifyFormat( |
| 736 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 737 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 738 | verifyFormat( |
| 739 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 740 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 741 | verifyFormat( |
| 742 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 743 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 744 | verifyFormat( |
| 745 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 746 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 747 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 748 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 749 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 750 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 754 | verifyFormat( |
| 755 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 756 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 757 | verifyFormat( |
| 758 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 759 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 760 | } |
| 761 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 762 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 763 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 764 | " \"short literal\");"); |
| 765 | verifyFormat( |
| 766 | "looooooooooooooooooooooooongFunction(\n" |
| 767 | " \"short literal\"\n" |
| 768 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 769 | } |
| 770 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 771 | TEST_F(FormatTest, AlignsPipes) { |
| 772 | verifyFormat( |
| 773 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 774 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 775 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 776 | verifyFormat( |
| 777 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 778 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 779 | verifyFormat( |
| 780 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 781 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 782 | verifyFormat( |
| 783 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 784 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 785 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 786 | verifyFormat( |
| 787 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 788 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 789 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 790 | } |
| 791 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 792 | TEST_F(FormatTest, UnderstandsEquals) { |
| 793 | verifyFormat( |
| 794 | "aaaaaaaaaaaaaaaaa =\n" |
| 795 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 796 | verifyFormat( |
| 797 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 798 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 799 | "}"); |
| 800 | verifyFormat( |
| 801 | "if (a) {\n" |
| 802 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 803 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 804 | "}"); |
| 805 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 806 | verifyFormat( |
| 807 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
| 808 | " 10000000) {\n" |
| 809 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 812 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 813 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 814 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 815 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 816 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 817 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 818 | |
| 819 | verifyFormat( |
| 820 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 821 | " Parameter2);"); |
| 822 | |
| 823 | verifyFormat( |
| 824 | "ShortObject->shortFunction(\n" |
| 825 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 826 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 827 | |
| 828 | verifyFormat("loooooooooooooongFunction(\n" |
| 829 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 830 | |
| 831 | verifyFormat( |
| 832 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 833 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 834 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 835 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 836 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 837 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 838 | "}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 839 | verifyFormat( |
| 840 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 841 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 842 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 845 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 846 | verifyFormat("template <typename T>\n" |
| 847 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 848 | verifyFormat( |
| 849 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 850 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 851 | verifyFormat( |
| 852 | "template <typename T>\n" |
| 853 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 854 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 855 | verifyFormat( |
| 856 | "template <typename T>\n" |
| 857 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 858 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 859 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 860 | |
| 861 | } |
| 862 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 863 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 864 | verifyFormat("A<int> a;"); |
| 865 | verifyFormat("A<A<A<int> > > a;"); |
| 866 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 867 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 868 | verifyFormat("bool x = 5 < f<int>();"); |
| 869 | verifyFormat("bool x = f<int>() > 5;"); |
| 870 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 871 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 872 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 873 | |
| 874 | verifyGoogleFormat("A<A<int>> a;"); |
| 875 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 876 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 877 | |
| 878 | verifyFormat("test >> a >> b;"); |
| 879 | verifyFormat("test << a >> b;"); |
| 880 | |
| 881 | verifyFormat("f<int>();"); |
| 882 | verifyFormat("template <typename T> void f() {\n}"); |
| 883 | } |
| 884 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 885 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 886 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 887 | verifyFormat("f(-1, -2, -3);"); |
| 888 | verifyFormat("a[-1] = 5;"); |
| 889 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 890 | verifyFormat("if (i == -1) {\n}"); |
| 891 | verifyFormat("if (i != -1) {\n}"); |
| 892 | verifyFormat("if (i > -1) {\n}"); |
| 893 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 894 | verifyFormat("++(a->f());"); |
| 895 | verifyFormat("--(a->f());"); |
| 896 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 897 | |
| 898 | verifyFormat("a-- > b;"); |
| 899 | verifyFormat("b ? -a : c;"); |
| 900 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 901 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 902 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 903 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 904 | |
| 905 | verifyFormat("return -1;"); |
| 906 | verifyFormat("switch (a) {\n" |
| 907 | "case -1:\n" |
| 908 | " break;\n" |
| 909 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 913 | verifyFormat("bool operator<();"); |
| 914 | verifyFormat("bool operator>();"); |
| 915 | verifyFormat("bool operator=();"); |
| 916 | verifyFormat("bool operator==();"); |
| 917 | verifyFormat("bool operator!=();"); |
| 918 | verifyFormat("int operator+();"); |
| 919 | verifyFormat("int operator++();"); |
| 920 | verifyFormat("bool operator();"); |
| 921 | verifyFormat("bool operator()();"); |
| 922 | verifyFormat("bool operator[]();"); |
| 923 | verifyFormat("operator bool();"); |
| 924 | verifyFormat("operator SomeType<int>();"); |
| 925 | verifyFormat("void *operator new(std::size_t size);"); |
| 926 | verifyFormat("void *operator new[](std::size_t size);"); |
| 927 | verifyFormat("void operator delete(void *ptr);"); |
| 928 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 931 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 932 | verifyFormat("int *f(int *a) {\n}"); |
| 933 | verifyFormat("f(a, *a);"); |
| 934 | verifyFormat("f(*a);"); |
| 935 | verifyFormat("int a = b * 10;"); |
| 936 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 937 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 938 | verifyFormat("int a += b * c;"); |
| 939 | verifyFormat("int a -= b * c;"); |
| 940 | verifyFormat("int a *= b * c;"); |
| 941 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 942 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 943 | verifyFormat("int a = *b * c;"); |
| 944 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 945 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 946 | verifyFormat("return 10 * b;"); |
| 947 | verifyFormat("return *b * *c;"); |
| 948 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 949 | verifyFormat("f(b ? *c : *d);"); |
| 950 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 951 | verifyFormat("*b = a;"); |
| 952 | verifyFormat("a * ~b;"); |
| 953 | verifyFormat("a * !b;"); |
| 954 | verifyFormat("a * +b;"); |
| 955 | verifyFormat("a * -b;"); |
| 956 | verifyFormat("a * ++b;"); |
| 957 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 958 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 959 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 960 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 961 | // FIXME: Is this desired for LLVM? Fix if not. |
| 962 | verifyFormat("A<int *> a;"); |
| 963 | verifyFormat("A<int **> a;"); |
| 964 | verifyFormat("A<int *, int *> a;"); |
| 965 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 966 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 967 | verifyFormat("Type *A = (Type *) P;"); |
| 968 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 969 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 970 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 971 | verifyGoogleFormat("A<int*> a;"); |
| 972 | verifyGoogleFormat("A<int**> a;"); |
| 973 | verifyGoogleFormat("A<int*, int*> a;"); |
| 974 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 975 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 976 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 979 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 980 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
| 981 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 982 | verifyFormat( |
| 983 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 984 | " SourceLocation L, IdentifierIn *II,\n" |
| 985 | " Type *T) {\n}"); |
| 986 | } |
| 987 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 988 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 989 | verifyFormat("(a)->b();"); |
| 990 | verifyFormat("--a;"); |
| 991 | } |
| 992 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 993 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 994 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 995 | EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 996 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 997 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 998 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 999 | |
| 1000 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 1001 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 1002 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 1003 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 1004 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1007 | //===----------------------------------------------------------------------===// |
| 1008 | // Error recovery tests. |
| 1009 | //===----------------------------------------------------------------------===// |
| 1010 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1011 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1012 | verifyFormat("public:"); |
| 1013 | verifyFormat("class A {\n" |
| 1014 | "public\n" |
| 1015 | " void f() {\n" |
| 1016 | " }\n" |
| 1017 | "};"); |
| 1018 | verifyFormat("public\n" |
| 1019 | "int qwerty;"); |
| 1020 | verifyFormat("public\n" |
| 1021 | "B {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1022 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1023 | verifyFormat("public\n" |
| 1024 | "{\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1025 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1026 | verifyFormat("public\n" |
| 1027 | "B {\n" |
| 1028 | " int x;\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1029 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1030 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1031 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1032 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1033 | verifyFormat("{"); |
| 1034 | } |
| 1035 | |
| 1036 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 1037 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1038 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1039 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1040 | "}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1041 | "f();"); |
| 1042 | verifyFormat("do {\n" |
| 1043 | "}\n" |
| 1044 | "wheeee(fun);"); |
| 1045 | verifyFormat("do {\n" |
| 1046 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1047 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1050 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1051 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1052 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1055 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 1056 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 1057 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 1058 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 1059 | EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1060 | |
| 1061 | FormatStyle Style = getLLVMStyle(); |
| 1062 | Style.ColumnLimit = 10; |
| 1063 | EXPECT_EQ("{\n" |
| 1064 | " {\n" |
| 1065 | " breakme(\n" |
| 1066 | " qwe);\n" |
| 1067 | "}\n", format("{\n" |
| 1068 | " {\n" |
| 1069 | " breakme(qwe);\n" |
| 1070 | "}\n", Style)); |
| 1071 | |
| 1072 | } |
| 1073 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1074 | //===----------------------------------------------------------------------===// |
| 1075 | // Objective-C tests. |
| 1076 | //===----------------------------------------------------------------------===// |
| 1077 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1078 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1079 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1080 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1081 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1082 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1083 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1084 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1085 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1086 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1087 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1088 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1089 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1090 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1091 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1092 | EXPECT_EQ( |
| 1093 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1094 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1097 | EXPECT_EQ( |
| 1098 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1099 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1100 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1101 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1102 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1103 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1104 | format( |
| 1105 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1106 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1107 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1108 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1109 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1110 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1113 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1114 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1115 | verifyFormat("@catch"); |
| 1116 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1117 | verifyFormat("@compatibility_alias"); |
| 1118 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1119 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1120 | verifyFormat("@encode"); |
| 1121 | verifyFormat("@end"); |
| 1122 | verifyFormat("@finally"); |
| 1123 | verifyFormat("@implementation"); |
| 1124 | verifyFormat("@import"); |
| 1125 | verifyFormat("@interface"); |
| 1126 | verifyFormat("@optional"); |
| 1127 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1128 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1129 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1130 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1131 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1132 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1133 | verifyFormat("@required"); |
| 1134 | verifyFormat("@selector"); |
| 1135 | verifyFormat("@synchronized"); |
| 1136 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1137 | verifyFormat("@throw"); |
| 1138 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1139 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame^] | 1140 | // FIXME: Make the uncommented lines below pass. |
| 1141 | verifyFormat("@\"String\""); |
| 1142 | verifyFormat("@1"); |
| 1143 | //verifyFormat("@+4.8"); |
| 1144 | //verifyFormat("@-4"); |
| 1145 | verifyFormat("@1LL"); |
| 1146 | verifyFormat("@.5"); |
| 1147 | verifyFormat("@'c'"); |
| 1148 | verifyFormat("@true"); |
| 1149 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 1150 | verifyFormat("@["); |
| 1151 | verifyFormat("@{"); |
| 1152 | |
| 1153 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1154 | EXPECT_EQ("@interface", format("@ interface")); |
| 1155 | |
| 1156 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1157 | // this. |
| 1158 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1161 | TEST_F(FormatTest, ObjCSnippets) { |
| 1162 | // FIXME: Make the uncommented lines below pass. |
| 1163 | verifyFormat("@autoreleasepool {\n" |
| 1164 | " foo();\n" |
| 1165 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1166 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1167 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1168 | verifyFormat("@dynamic textColor;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1169 | //verifyFormat("char *buf1 = @encode(int **);"); |
| 1170 | //verifyFormat("SEL s = @selector(foo:);"); |
| 1171 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1172 | |
| 1173 | // FIXME: "getter=bar" should not be surround by spaces in @property. |
| 1174 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
| 1175 | } |
| 1176 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1177 | } // end namespace tooling |
| 1178 | } // end namespace clang |