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; |
| 31 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
| 32 | tooling::Replacements Replace = |
| 33 | reformat(Style, Lex, Context.Sources, Ranges); |
| 34 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 35 | return Context.getRewrittenText(ID); |
| 36 | } |
| 37 | |
| 38 | std::string format(llvm::StringRef Code, |
| 39 | const FormatStyle &Style = getLLVMStyle()) { |
| 40 | return format(Code, 0, Code.size(), Style); |
| 41 | } |
| 42 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 43 | std::string messUp(llvm::StringRef Code) { |
| 44 | std::string MessedUp(Code.str()); |
| 45 | bool InComment = false; |
| 46 | bool JustReplacedNewline = false; |
| 47 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 48 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 49 | if (JustReplacedNewline) |
| 50 | MessedUp[i - 1] = '\n'; |
| 51 | InComment = true; |
| 52 | } else if (MessedUp[i] != ' ') { |
| 53 | JustReplacedNewline = false; |
| 54 | } else if (MessedUp[i] == '\n') { |
| 55 | if (InComment) { |
| 56 | InComment = false; |
| 57 | } else { |
| 58 | JustReplacedNewline = true; |
| 59 | MessedUp[i] = ' '; |
| 60 | } |
| 61 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 62 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 63 | return MessedUp; |
| 64 | } |
| 65 | |
| 66 | void verifyFormat(llvm::StringRef Code) { |
| 67 | EXPECT_EQ(Code.str(), format(messUp(Code))); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void verifyGoogleFormat(llvm::StringRef Code) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 71 | EXPECT_EQ(Code.str(), format(messUp(Code), getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | // Basic function tests. |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 79 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 80 | EXPECT_EQ(";", format(";")); |
| 81 | } |
| 82 | |
| 83 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 84 | EXPECT_EQ("int i;", format(" int i;")); |
| 85 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 86 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 87 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 88 | } |
| 89 | |
| 90 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 91 | EXPECT_EQ("int i;", format("int\ni;")); |
| 92 | } |
| 93 | |
| 94 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 95 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 96 | } |
| 97 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 98 | TEST_F(FormatTest, FormatsNestedCall) { |
| 99 | verifyFormat("Method(f1, f2(f3));"); |
| 100 | verifyFormat("Method(f1(f2, f3()));"); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | // Tests for control statements. |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 109 | verifyFormat("if (true)\n f();\ng();"); |
| 110 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 111 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 112 | verifyFormat("if (a)\n" |
| 113 | " // comment\n" |
| 114 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(FormatTest, ParseIfElse) { |
| 118 | verifyFormat("if (true)\n" |
| 119 | " if (true)\n" |
| 120 | " if (true)\n" |
| 121 | " f();\n" |
| 122 | " else\n" |
| 123 | " g();\n" |
| 124 | " else\n" |
| 125 | " h();\n" |
| 126 | "else\n" |
| 127 | " i();"); |
| 128 | verifyFormat("if (true)\n" |
| 129 | " if (true)\n" |
| 130 | " if (true) {\n" |
| 131 | " if (true)\n" |
| 132 | " f();\n" |
| 133 | " } else {\n" |
| 134 | " g();\n" |
| 135 | " }\n" |
| 136 | " else\n" |
| 137 | " h();\n" |
| 138 | "else {\n" |
| 139 | " i();\n" |
| 140 | "}"); |
| 141 | } |
| 142 | |
| 143 | TEST_F(FormatTest, ElseIf) { |
| 144 | verifyFormat("if (a) {\n" |
| 145 | "} else if (b) {\n" |
| 146 | "}"); |
| 147 | verifyFormat("if (a)\n" |
| 148 | " f();\n" |
| 149 | "else if (b)\n" |
| 150 | " g();\n" |
| 151 | "else\n" |
| 152 | " h();"); |
| 153 | } |
| 154 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 155 | TEST_F(FormatTest, FormatsForLoop) { |
| 156 | verifyFormat( |
| 157 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 158 | " ++VeryVeryLongLoopVariable)\n" |
| 159 | " ;"); |
| 160 | verifyFormat("for (;;)\n" |
| 161 | " f();"); |
| 162 | verifyFormat("for (;;) {\n" |
| 163 | "}"); |
| 164 | verifyFormat("for (;;) {\n" |
| 165 | " f();\n" |
| 166 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 167 | |
| 168 | verifyFormat( |
| 169 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 170 | " E = UnwrappedLines.end();\n" |
| 171 | " I != E; ++I) {\n}"); |
| 172 | |
| 173 | verifyFormat( |
| 174 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 175 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 179 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 180 | verifyFormat("while (true)\n" |
| 181 | " f();"); |
| 182 | verifyFormat("while () {\n" |
| 183 | "}"); |
| 184 | verifyFormat("while () {\n" |
| 185 | " f();\n" |
| 186 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 189 | TEST_F(FormatTest, FormatsDoWhile) { |
| 190 | verifyFormat("do {\n" |
| 191 | " do_something();\n" |
| 192 | "} while (something());"); |
| 193 | verifyFormat("do\n" |
| 194 | " do_something();\n" |
| 195 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 198 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 199 | verifyFormat("switch (x) {\n" |
| 200 | "case 1:\n" |
| 201 | " f();\n" |
| 202 | " break;\n" |
| 203 | "case kFoo:\n" |
| 204 | "case ns::kBar:\n" |
| 205 | "case kBaz:\n" |
| 206 | " break;\n" |
| 207 | "default:\n" |
| 208 | " g();\n" |
| 209 | " break;\n" |
| 210 | "}"); |
| 211 | verifyFormat("switch (x) {\n" |
| 212 | "case 1: {\n" |
| 213 | " f();\n" |
| 214 | " break;\n" |
| 215 | "}\n" |
| 216 | "}"); |
| 217 | verifyFormat("switch (test)\n" |
| 218 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 219 | verifyGoogleFormat("switch (x) {\n" |
| 220 | " case 1:\n" |
| 221 | " f();\n" |
| 222 | " break;\n" |
| 223 | " case kFoo:\n" |
| 224 | " case ns::kBar:\n" |
| 225 | " case kBaz:\n" |
| 226 | " break;\n" |
| 227 | " default:\n" |
| 228 | " g();\n" |
| 229 | " break;\n" |
| 230 | "}"); |
| 231 | verifyGoogleFormat("switch (x) {\n" |
| 232 | " case 1: {\n" |
| 233 | " f();\n" |
| 234 | " break;\n" |
| 235 | " }\n" |
| 236 | "}"); |
| 237 | verifyGoogleFormat("switch (test)\n" |
| 238 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 241 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 242 | verifyFormat("void f() {\n" |
| 243 | " some_code();\n" |
| 244 | "test_label:\n" |
| 245 | " some_other_code();\n" |
| 246 | " {\n" |
| 247 | " some_more_code();\n" |
| 248 | " another_label:\n" |
| 249 | " some_more_code();\n" |
| 250 | " }\n" |
| 251 | "}"); |
| 252 | verifyFormat("some_code();\n" |
| 253 | "test_label:\n" |
| 254 | "some_other_code();"); |
| 255 | } |
| 256 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 257 | |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | // Tests for comments. |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
| 262 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 263 | verifyFormat("// line 1\n" |
| 264 | "// line 2\n" |
| 265 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 266 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 267 | verifyFormat("void f() {\n" |
| 268 | " // Doesn't do anything\n" |
| 269 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 270 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 271 | verifyFormat("int i // This is a fancy variable\n" |
| 272 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 273 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 274 | verifyFormat("enum E {\n" |
| 275 | " // comment\n" |
| 276 | " VAL_A, // comment\n" |
| 277 | " VAL_B\n" |
| 278 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 279 | |
| 280 | verifyFormat( |
| 281 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 282 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
| 283 | } |
| 284 | |
| 285 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 286 | verifyFormat("f(/*test=*/ true);"); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | // Tests for classes, namespaces, etc. |
| 292 | //===----------------------------------------------------------------------===// |
| 293 | |
| 294 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 295 | verifyFormat("class A {\n};"); |
| 296 | } |
| 297 | |
| 298 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 299 | verifyFormat("class A {\n" |
| 300 | "public:\n" |
| 301 | "protected:\n" |
| 302 | "private:\n" |
| 303 | " void f() {\n" |
| 304 | " }\n" |
| 305 | "};"); |
| 306 | verifyGoogleFormat("class A {\n" |
| 307 | " public:\n" |
| 308 | " protected:\n" |
| 309 | " private:\n" |
| 310 | " void f() {\n" |
| 311 | " }\n" |
| 312 | "};"); |
| 313 | } |
| 314 | |
| 315 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 316 | verifyFormat("class A : public B {\n" |
| 317 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 318 | verifyFormat("class A : public ::B {\n" |
| 319 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 322 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 323 | verifyFormat("enum {\n" |
| 324 | " Zero,\n" |
| 325 | " One = 1,\n" |
| 326 | " Two = One + 1,\n" |
| 327 | " Three = (One + Two),\n" |
| 328 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 329 | " Five = (One, Two, Three, Four, 5)\n" |
| 330 | "};"); |
| 331 | verifyFormat("enum Enum {\n" |
| 332 | "};"); |
| 333 | verifyFormat("enum {\n" |
| 334 | "};"); |
| 335 | } |
| 336 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 337 | TEST_F(FormatTest, FormatsNamespaces) { |
| 338 | verifyFormat("namespace some_namespace {\n" |
| 339 | "class A {\n" |
| 340 | "};\n" |
| 341 | "void f() {\n" |
| 342 | " f();\n" |
| 343 | "}\n" |
| 344 | "}"); |
| 345 | verifyFormat("namespace {\n" |
| 346 | "class A {\n" |
| 347 | "};\n" |
| 348 | "void f() {\n" |
| 349 | " f();\n" |
| 350 | "}\n" |
| 351 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 352 | verifyFormat("inline namespace X {\n" |
| 353 | "class A {\n" |
| 354 | "};\n" |
| 355 | "void f() {\n" |
| 356 | " f();\n" |
| 357 | "}\n" |
| 358 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 359 | verifyFormat("using namespace some_namespace;\n" |
| 360 | "class A {\n" |
| 361 | "};\n" |
| 362 | "void f() {\n" |
| 363 | " f();\n" |
| 364 | "}"); |
| 365 | } |
| 366 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 367 | TEST_F(FormatTest, StaticInitializers) { |
| 368 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 369 | |
| 370 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 371 | verifyFormat( |
| 372 | "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n" |
| 373 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
| 374 | } |
| 375 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 376 | //===----------------------------------------------------------------------===// |
| 377 | // Line break tests. |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | |
| 380 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 381 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 382 | " int h, int j, int f,\n" |
| 383 | " int c, int ddddddddddddd) {\n" |
| 384 | "}"); |
| 385 | } |
| 386 | |
| 387 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 388 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 389 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 390 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 391 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 394 | TEST_F(FormatTest, ConstructorInitializers) { |
| 395 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 396 | |
| 397 | verifyFormat( |
| 398 | "SomeClass::Constructor()\n" |
| 399 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 400 | "}"); |
| 401 | |
| 402 | verifyFormat( |
| 403 | "SomeClass::Constructor()\n" |
| 404 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 405 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 406 | "}"); |
| 407 | |
| 408 | verifyFormat("Constructor()\n" |
| 409 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 410 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 411 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 412 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 413 | "}"); |
| 414 | |
| 415 | // Here a line could be saved by splitting the second initializer onto two |
| 416 | // lines, but that is not desireable. |
| 417 | verifyFormat("Constructor()\n" |
| 418 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 419 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 420 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 421 | "}"); |
| 422 | |
| 423 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 424 | " : some_var_(var), // 4 space indent\n" |
| 425 | " some_other_var_(var + 1) { // lined up\n" |
| 426 | "}"); |
| 427 | } |
| 428 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 429 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 430 | verifyFormat( |
| 431 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 432 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 433 | " f();"); |
| 434 | } |
| 435 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 436 | TEST_F(FormatTest, BreaksDesireably) { |
| 437 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 438 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 439 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 440 | |
| 441 | verifyFormat( |
| 442 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 443 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 444 | |
| 445 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 446 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 447 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 448 | |
| 449 | verifyFormat( |
| 450 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 451 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 452 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 453 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 454 | |
| 455 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 456 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 457 | |
| 458 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 459 | // taking into account the StopAt value. |
| 460 | verifyFormat( |
| 461 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 462 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 463 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 464 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 465 | |
| 466 | verifyFormat( |
| 467 | "{\n {\n {\n" |
| 468 | " Annotation.SpaceRequiredBefore =\n" |
| 469 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 470 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 471 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 474 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 475 | verifyFormat( |
| 476 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 477 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
| 478 | verifyFormat( |
| 479 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 480 | " ccccccccccccccccccccccccc) {\n}"); |
| 481 | verifyFormat( |
| 482 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 483 | " ccccccccccccccccccccccccc) {\n}"); |
| 484 | verifyFormat( |
| 485 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 486 | " ccccccccccccccccccccccccc) {\n}"); |
| 487 | } |
| 488 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 489 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 490 | verifyFormat( |
| 491 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 492 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 493 | verifyFormat( |
| 494 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 495 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 496 | verifyFormat( |
| 497 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 498 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 499 | verifyFormat( |
| 500 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 501 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 502 | verifyFormat( |
| 503 | "double LooooooooooooooooooooooooongResult =\n" |
| 504 | " aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 505 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 506 | } |
| 507 | |
| 508 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 509 | verifyFormat( |
| 510 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 511 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 512 | verifyFormat( |
| 513 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 514 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 515 | } |
| 516 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 517 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 518 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 519 | " \"short literal\");"); |
| 520 | verifyFormat( |
| 521 | "looooooooooooooooooooooooongFunction(\n" |
| 522 | " \"short literal\"\n" |
| 523 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 524 | } |
| 525 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 526 | TEST_F(FormatTest, AlignsPipes) { |
| 527 | verifyFormat( |
| 528 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 529 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 530 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 531 | verifyFormat( |
| 532 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 533 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 534 | verifyFormat( |
| 535 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 536 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 537 | verifyFormat( |
| 538 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 539 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 540 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 541 | verifyFormat( |
| 542 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 543 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 544 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 545 | } |
| 546 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 547 | TEST_F(FormatTest, UnderstandsEquals) { |
| 548 | verifyFormat( |
| 549 | "aaaaaaaaaaaaaaaaa =\n" |
| 550 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 551 | verifyFormat( |
| 552 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 553 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 554 | "}"); |
| 555 | verifyFormat( |
| 556 | "if (a) {\n" |
| 557 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 558 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 559 | "}"); |
| 560 | |
| 561 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 562 | " 100000000 + 100000000) {\n}"); |
| 563 | } |
| 564 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 565 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 566 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 567 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 568 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 569 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 570 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 571 | |
| 572 | verifyFormat( |
| 573 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 574 | " Parameter2);"); |
| 575 | |
| 576 | verifyFormat( |
| 577 | "ShortObject->shortFunction(\n" |
| 578 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 579 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 580 | |
| 581 | verifyFormat("loooooooooooooongFunction(\n" |
| 582 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 583 | |
| 584 | verifyFormat( |
| 585 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 586 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 587 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 588 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 589 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 590 | "}"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 593 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 594 | verifyFormat("A<int> a;"); |
| 595 | verifyFormat("A<A<A<int> > > a;"); |
| 596 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 597 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 598 | verifyFormat("bool x = 5 < f<int>();"); |
| 599 | verifyFormat("bool x = f<int>() > 5;"); |
| 600 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 601 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 602 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 603 | |
| 604 | verifyGoogleFormat("A<A<int>> a;"); |
| 605 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 606 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 607 | |
| 608 | verifyFormat("test >> a >> b;"); |
| 609 | verifyFormat("test << a >> b;"); |
| 610 | |
| 611 | verifyFormat("f<int>();"); |
| 612 | verifyFormat("template <typename T> void f() {\n}"); |
| 613 | } |
| 614 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 615 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 616 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 617 | verifyFormat("f(-1, -2, -3);"); |
| 618 | verifyFormat("a[-1] = 5;"); |
| 619 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 620 | verifyFormat("if (i == -1) {\n}"); |
| 621 | verifyFormat("if (i != -1) {\n}"); |
| 622 | verifyFormat("if (i > -1) {\n}"); |
| 623 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 624 | verifyFormat("++(a->f());"); |
| 625 | verifyFormat("--(a->f());"); |
| 626 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 627 | |
| 628 | verifyFormat("a-- > b;"); |
| 629 | verifyFormat("b ? -a : c;"); |
| 630 | verifyFormat("n * sizeof char16;"); |
| 631 | verifyFormat("sizeof(char);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 635 | verifyFormat("bool operator<();"); |
| 636 | verifyFormat("bool operator>();"); |
| 637 | verifyFormat("bool operator=();"); |
| 638 | verifyFormat("bool operator==();"); |
| 639 | verifyFormat("bool operator!=();"); |
| 640 | verifyFormat("int operator+();"); |
| 641 | verifyFormat("int operator++();"); |
| 642 | verifyFormat("bool operator();"); |
| 643 | verifyFormat("bool operator()();"); |
| 644 | verifyFormat("bool operator[]();"); |
| 645 | verifyFormat("operator bool();"); |
| 646 | verifyFormat("operator SomeType<int>();"); |
| 647 | verifyFormat("void *operator new(std::size_t size);"); |
| 648 | verifyFormat("void *operator new[](std::size_t size);"); |
| 649 | verifyFormat("void operator delete(void *ptr);"); |
| 650 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | TEST_F(FormatTest, UnderstandsUsesOfStar) { |
| 654 | verifyFormat("int *f(int *a) {\n}"); |
| 655 | verifyFormat("f(a, *a);"); |
| 656 | verifyFormat("f(*a);"); |
| 657 | verifyFormat("int a = b * 10;"); |
| 658 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 659 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 660 | verifyFormat("int a += b * c;"); |
| 661 | verifyFormat("int a -= b * c;"); |
| 662 | verifyFormat("int a *= b * c;"); |
| 663 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 664 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 665 | verifyFormat("int a = *b * c;"); |
| 666 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 667 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 668 | verifyFormat("return 10 * b;"); |
| 669 | verifyFormat("return *b * *c;"); |
| 670 | verifyFormat("return a & ~b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 671 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 672 | // FIXME: Is this desired for LLVM? Fix if not. |
| 673 | verifyFormat("A<int *> a;"); |
| 674 | verifyFormat("A<int **> a;"); |
| 675 | verifyFormat("A<int *, int *> a;"); |
| 676 | verifyFormat("A<int **, int **> a;"); |
| 677 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 678 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 679 | verifyGoogleFormat("A<int*> a;"); |
| 680 | verifyGoogleFormat("A<int**> a;"); |
| 681 | verifyGoogleFormat("A<int*, int*> a;"); |
| 682 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 685 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 686 | verifyFormat("(a)->b();"); |
| 687 | verifyFormat("--a;"); |
| 688 | } |
| 689 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 690 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 691 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 692 | 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] | 693 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 694 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 695 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 696 | |
| 697 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 698 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 699 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 700 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 701 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 704 | |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | // Error recovery tests. |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 709 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 710 | verifyFormat("public:"); |
| 711 | verifyFormat("class A {\n" |
| 712 | "public\n" |
| 713 | " void f() {\n" |
| 714 | " }\n" |
| 715 | "};"); |
| 716 | verifyFormat("public\n" |
| 717 | "int qwerty;"); |
| 718 | verifyFormat("public\n" |
| 719 | "B {\n" |
| 720 | "};"); |
| 721 | verifyFormat("public\n" |
| 722 | "{\n" |
| 723 | "};"); |
| 724 | verifyFormat("public\n" |
| 725 | "B {\n" |
| 726 | " int x;\n" |
| 727 | "};"); |
| 728 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 729 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 730 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 731 | verifyFormat("{"); |
| 732 | } |
| 733 | |
| 734 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 735 | verifyFormat("do {\n" |
| 736 | "};"); |
| 737 | verifyFormat("do {\n" |
| 738 | "};\n" |
| 739 | "f();"); |
| 740 | verifyFormat("do {\n" |
| 741 | "}\n" |
| 742 | "wheeee(fun);"); |
| 743 | verifyFormat("do {\n" |
| 744 | " f();\n" |
| 745 | "};"); |
| 746 | } |
| 747 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 748 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 749 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 750 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 751 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 752 | 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] | 753 | |
| 754 | FormatStyle Style = getLLVMStyle(); |
| 755 | Style.ColumnLimit = 10; |
| 756 | EXPECT_EQ("{\n" |
| 757 | " {\n" |
| 758 | " breakme(\n" |
| 759 | " qwe);\n" |
| 760 | "}\n", format("{\n" |
| 761 | " {\n" |
| 762 | " breakme(qwe);\n" |
| 763 | "}\n", Style)); |
| 764 | |
| 765 | } |
| 766 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 767 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 768 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 769 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 770 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
| 771 | EXPECT_EQ("- (NSInteger)Mthod1;", |
| 772 | format("-(NSInteger)Mthod1;")); |
| 773 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 774 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 775 | format("-(NSInteger)Method3:(id)anObject;")); |
| 776 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 777 | format("-(NSInteger)Method4:(id)anObject;")); |
| 778 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 779 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 780 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 781 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
| 782 | EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 783 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 784 | |
| 785 | // Very long objectiveC method declaration. |
| 786 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 787 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 788 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 789 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 790 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 791 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 792 | |
| 793 | format("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 794 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 795 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 796 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 797 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 798 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 801 | } // end namespace tooling |
| 802 | } // end namespace clang |