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 | |
| 43 | void verifyFormat(llvm::StringRef Code) { |
| 44 | std::string WithoutFormat(Code.str()); |
| 45 | for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) { |
| 46 | if (WithoutFormat[i] == '\n') |
| 47 | WithoutFormat[i] = ' '; |
| 48 | } |
| 49 | EXPECT_EQ(Code.str(), format(WithoutFormat)); |
| 50 | } |
| 51 | |
| 52 | void verifyGoogleFormat(llvm::StringRef Code) { |
| 53 | std::string WithoutFormat(Code.str()); |
| 54 | for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) { |
| 55 | if (WithoutFormat[i] == '\n') |
| 56 | WithoutFormat[i] = ' '; |
| 57 | } |
| 58 | EXPECT_EQ(Code.str(), format(WithoutFormat, getGoogleStyle())); |
| 59 | } |
| 60 | }; |
| 61 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 62 | //===----------------------------------------------------------------------===// |
| 63 | // Basic function tests. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 66 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 67 | EXPECT_EQ(";", format(";")); |
| 68 | } |
| 69 | |
| 70 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 71 | EXPECT_EQ("int i;", format(" int i;")); |
| 72 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 73 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 74 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 75 | } |
| 76 | |
| 77 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 78 | EXPECT_EQ("int i;", format("int\ni;")); |
| 79 | } |
| 80 | |
| 81 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 82 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 83 | } |
| 84 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 85 | TEST_F(FormatTest, FormatsNestedCall) { |
| 86 | verifyFormat("Method(f1, f2(f3));"); |
| 87 | verifyFormat("Method(f1(f2, f3()));"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // Tests for control statements. |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | |
| 95 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 96 | verifyFormat("if (true)\n f();\ng();"); |
| 97 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 98 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
| 99 | EXPECT_EQ("if (a)\n // comment\n f();", format("if(a)\n// comment\nf();")); |
| 100 | } |
| 101 | |
| 102 | TEST_F(FormatTest, ParseIfElse) { |
| 103 | verifyFormat("if (true)\n" |
| 104 | " if (true)\n" |
| 105 | " if (true)\n" |
| 106 | " f();\n" |
| 107 | " else\n" |
| 108 | " g();\n" |
| 109 | " else\n" |
| 110 | " h();\n" |
| 111 | "else\n" |
| 112 | " i();"); |
| 113 | verifyFormat("if (true)\n" |
| 114 | " if (true)\n" |
| 115 | " if (true) {\n" |
| 116 | " if (true)\n" |
| 117 | " f();\n" |
| 118 | " } else {\n" |
| 119 | " g();\n" |
| 120 | " }\n" |
| 121 | " else\n" |
| 122 | " h();\n" |
| 123 | "else {\n" |
| 124 | " i();\n" |
| 125 | "}"); |
| 126 | } |
| 127 | |
| 128 | TEST_F(FormatTest, ElseIf) { |
| 129 | verifyFormat("if (a) {\n" |
| 130 | "} else if (b) {\n" |
| 131 | "}"); |
| 132 | verifyFormat("if (a)\n" |
| 133 | " f();\n" |
| 134 | "else if (b)\n" |
| 135 | " g();\n" |
| 136 | "else\n" |
| 137 | " h();"); |
| 138 | } |
| 139 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 140 | TEST_F(FormatTest, FormatsForLoop) { |
| 141 | verifyFormat( |
| 142 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 143 | " ++VeryVeryLongLoopVariable)\n" |
| 144 | " ;"); |
| 145 | verifyFormat("for (;;)\n" |
| 146 | " f();"); |
| 147 | verifyFormat("for (;;) {\n" |
| 148 | "}"); |
| 149 | verifyFormat("for (;;) {\n" |
| 150 | " f();\n" |
| 151 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 155 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 156 | verifyFormat("while (true)\n" |
| 157 | " f();"); |
| 158 | verifyFormat("while () {\n" |
| 159 | "}"); |
| 160 | verifyFormat("while () {\n" |
| 161 | " f();\n" |
| 162 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 165 | TEST_F(FormatTest, FormatsDoWhile) { |
| 166 | verifyFormat("do {\n" |
| 167 | " do_something();\n" |
| 168 | "} while (something());"); |
| 169 | verifyFormat("do\n" |
| 170 | " do_something();\n" |
| 171 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 174 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 175 | verifyFormat("switch (x) {\n" |
| 176 | "case 1:\n" |
| 177 | " f();\n" |
| 178 | " break;\n" |
| 179 | "case kFoo:\n" |
| 180 | "case ns::kBar:\n" |
| 181 | "case kBaz:\n" |
| 182 | " break;\n" |
| 183 | "default:\n" |
| 184 | " g();\n" |
| 185 | " break;\n" |
| 186 | "}"); |
| 187 | verifyFormat("switch (x) {\n" |
| 188 | "case 1: {\n" |
| 189 | " f();\n" |
| 190 | " break;\n" |
| 191 | "}\n" |
| 192 | "}"); |
| 193 | verifyFormat("switch (test)\n" |
| 194 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 195 | verifyGoogleFormat("switch (x) {\n" |
| 196 | " case 1:\n" |
| 197 | " f();\n" |
| 198 | " break;\n" |
| 199 | " case kFoo:\n" |
| 200 | " case ns::kBar:\n" |
| 201 | " case kBaz:\n" |
| 202 | " break;\n" |
| 203 | " default:\n" |
| 204 | " g();\n" |
| 205 | " break;\n" |
| 206 | "}"); |
| 207 | verifyGoogleFormat("switch (x) {\n" |
| 208 | " case 1: {\n" |
| 209 | " f();\n" |
| 210 | " break;\n" |
| 211 | " }\n" |
| 212 | "}"); |
| 213 | verifyGoogleFormat("switch (test)\n" |
| 214 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 217 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 218 | verifyFormat("void f() {\n" |
| 219 | " some_code();\n" |
| 220 | "test_label:\n" |
| 221 | " some_other_code();\n" |
| 222 | " {\n" |
| 223 | " some_more_code();\n" |
| 224 | " another_label:\n" |
| 225 | " some_more_code();\n" |
| 226 | " }\n" |
| 227 | "}"); |
| 228 | verifyFormat("some_code();\n" |
| 229 | "test_label:\n" |
| 230 | "some_other_code();"); |
| 231 | } |
| 232 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 233 | |
| 234 | //===----------------------------------------------------------------------===// |
| 235 | // Tests for comments. |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | |
| 238 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
| 239 | EXPECT_EQ("// line 1\n// line 2\nvoid f() {\n}\n", |
| 240 | format("// line 1\n// line 2\nvoid f() {}\n")); |
| 241 | |
| 242 | EXPECT_EQ("void f() {\n // Doesn't do anything\n}", |
| 243 | format("void f() {\n// Doesn't do anything\n}")); |
| 244 | |
| 245 | EXPECT_EQ("int i // This is a fancy variable\n = 5;", |
| 246 | format("int i // This is a fancy variable\n= 5;")); |
| 247 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 248 | EXPECT_EQ("enum E {\n" |
| 249 | " // comment\n" |
| 250 | " VAL_A, // comment\n" |
| 251 | " VAL_B\n" |
| 252 | "};", |
| 253 | format("enum E{\n// comment\nVAL_A,// comment\nVAL_B};")); |
| 254 | |
| 255 | verifyFormat( |
| 256 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 257 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
| 258 | } |
| 259 | |
| 260 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 261 | verifyFormat("f(/*test=*/ true);"); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | // Tests for classes, namespaces, etc. |
| 267 | //===----------------------------------------------------------------------===// |
| 268 | |
| 269 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 270 | verifyFormat("class A {\n};"); |
| 271 | } |
| 272 | |
| 273 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 274 | verifyFormat("class A {\n" |
| 275 | "public:\n" |
| 276 | "protected:\n" |
| 277 | "private:\n" |
| 278 | " void f() {\n" |
| 279 | " }\n" |
| 280 | "};"); |
| 281 | verifyGoogleFormat("class A {\n" |
| 282 | " public:\n" |
| 283 | " protected:\n" |
| 284 | " private:\n" |
| 285 | " void f() {\n" |
| 286 | " }\n" |
| 287 | "};"); |
| 288 | } |
| 289 | |
| 290 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 291 | verifyFormat("class A : public B {\n" |
| 292 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 293 | verifyFormat("class A : public ::B {\n" |
| 294 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 297 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 298 | verifyFormat("enum {\n" |
| 299 | " Zero,\n" |
| 300 | " One = 1,\n" |
| 301 | " Two = One + 1,\n" |
| 302 | " Three = (One + Two),\n" |
| 303 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 304 | " Five = (One, Two, Three, Four, 5)\n" |
| 305 | "};"); |
| 306 | verifyFormat("enum Enum {\n" |
| 307 | "};"); |
| 308 | verifyFormat("enum {\n" |
| 309 | "};"); |
| 310 | } |
| 311 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 312 | TEST_F(FormatTest, FormatsNamespaces) { |
| 313 | verifyFormat("namespace some_namespace {\n" |
| 314 | "class A {\n" |
| 315 | "};\n" |
| 316 | "void f() {\n" |
| 317 | " f();\n" |
| 318 | "}\n" |
| 319 | "}"); |
| 320 | verifyFormat("namespace {\n" |
| 321 | "class A {\n" |
| 322 | "};\n" |
| 323 | "void f() {\n" |
| 324 | " f();\n" |
| 325 | "}\n" |
| 326 | "}"); |
| 327 | verifyFormat("using namespace some_namespace;\n" |
| 328 | "class A {\n" |
| 329 | "};\n" |
| 330 | "void f() {\n" |
| 331 | " f();\n" |
| 332 | "}"); |
| 333 | } |
| 334 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 335 | TEST_F(FormatTest, StaticInitializers) { |
| 336 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 337 | |
| 338 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 339 | verifyFormat( |
| 340 | "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n" |
| 341 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
| 342 | } |
| 343 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 344 | //===----------------------------------------------------------------------===// |
| 345 | // Line break tests. |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | |
| 348 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 349 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 350 | " int h, int j, int f,\n" |
| 351 | " int c, int ddddddddddddd) {\n" |
| 352 | "}"); |
| 353 | } |
| 354 | |
| 355 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 356 | verifyFormat( |
| 357 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 358 | " parameter, parameter, parameter)), SecondLongCall(parameter));"); |
| 359 | } |
| 360 | |
| 361 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 362 | verifyFormat( |
| 363 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 364 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 365 | " f();"); |
| 366 | } |
| 367 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 368 | TEST_F(FormatTest, BreaksDesireably) { |
| 369 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 370 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 371 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 372 | |
| 373 | verifyFormat( |
| 374 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 375 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 376 | |
| 377 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 378 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 379 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 380 | |
| 381 | verifyFormat( |
| 382 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 383 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 384 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 385 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 386 | |
| 387 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 388 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 389 | |
| 390 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 391 | // taking into account the StopAt value. |
| 392 | verifyFormat( |
| 393 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 394 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 395 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 396 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 400 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 401 | " \"short literal\");"); |
| 402 | verifyFormat( |
| 403 | "looooooooooooooooooooooooongFunction(\n" |
| 404 | " \"short literal\"\n" |
| 405 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 406 | } |
| 407 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 408 | TEST_F(FormatTest, AlignsPipes) { |
| 409 | verifyFormat( |
| 410 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 411 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 412 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 413 | verifyFormat( |
| 414 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 415 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 416 | verifyFormat( |
| 417 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 418 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 419 | verifyFormat( |
| 420 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 421 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 422 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 423 | verifyFormat( |
| 424 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 425 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 426 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 427 | } |
| 428 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 429 | TEST_F(FormatTest, UnderstandsEquals) { |
| 430 | verifyFormat( |
| 431 | "aaaaaaaaaaaaaaaaa =\n" |
| 432 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 433 | verifyFormat( |
| 434 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 435 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 436 | "}"); |
| 437 | verifyFormat( |
| 438 | "if (a) {\n" |
| 439 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 440 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 441 | "}"); |
| 442 | |
| 443 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 444 | " 100000000 + 100000000) {\n}"); |
| 445 | } |
| 446 | |
| 447 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 448 | verifyFormat("A<int> a;"); |
| 449 | verifyFormat("A<A<A<int> > > a;"); |
| 450 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 451 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 452 | verifyFormat("bool x = 5 < f<int>();"); |
| 453 | verifyFormat("bool x = f<int>() > 5;"); |
| 454 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 455 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 456 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 457 | |
| 458 | verifyGoogleFormat("A<A<int>> a;"); |
| 459 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 460 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 461 | |
| 462 | verifyFormat("test >> a >> b;"); |
| 463 | verifyFormat("test << a >> b;"); |
| 464 | |
| 465 | verifyFormat("f<int>();"); |
| 466 | verifyFormat("template <typename T> void f() {\n}"); |
| 467 | } |
| 468 | |
| 469 | TEST_F(FormatTest, UndestandsUnaryOperators) { |
| 470 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 471 | verifyFormat("f(-1, -2, -3);"); |
| 472 | verifyFormat("a[-1] = 5;"); |
| 473 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 474 | verifyFormat("if (i == -1) {\n}"); |
| 475 | verifyFormat("if (i != -1) {\n}"); |
| 476 | verifyFormat("if (i > -1) {\n}"); |
| 477 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 478 | verifyFormat("++(a->f());"); |
| 479 | verifyFormat("--(a->f());"); |
| 480 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
| 484 | verifyFormat("bool operator<() {\n}"); |
| 485 | } |
| 486 | |
| 487 | TEST_F(FormatTest, UnderstandsUsesOfStar) { |
| 488 | verifyFormat("int *f(int *a) {\n}"); |
| 489 | verifyFormat("f(a, *a);"); |
| 490 | verifyFormat("f(*a);"); |
| 491 | verifyFormat("int a = b * 10;"); |
| 492 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 493 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 494 | verifyFormat("int a += b * c;"); |
| 495 | verifyFormat("int a -= b * c;"); |
| 496 | verifyFormat("int a *= b * c;"); |
| 497 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 498 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 499 | verifyFormat("int a = *b * c;"); |
| 500 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 501 | verifyFormat("int main(int argc, char **argv) {\n}"); |
| 502 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 503 | // FIXME: Is this desired for LLVM? Fix if not. |
| 504 | verifyFormat("A<int *> a;"); |
| 505 | verifyFormat("A<int **> a;"); |
| 506 | verifyFormat("A<int *, int *> a;"); |
| 507 | verifyFormat("A<int **, int **> a;"); |
| 508 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 509 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 510 | verifyGoogleFormat("A<int*> a;"); |
| 511 | verifyGoogleFormat("A<int**> a;"); |
| 512 | verifyGoogleFormat("A<int*, int*> a;"); |
| 513 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 516 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 517 | verifyFormat("(a)->b();"); |
| 518 | verifyFormat("--a;"); |
| 519 | } |
| 520 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 521 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 522 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
| 523 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 524 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 525 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 526 | } |
| 527 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 528 | |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | // Error recovery tests. |
| 531 | //===----------------------------------------------------------------------===// |
| 532 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 533 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 534 | verifyFormat("public:"); |
| 535 | verifyFormat("class A {\n" |
| 536 | "public\n" |
| 537 | " void f() {\n" |
| 538 | " }\n" |
| 539 | "};"); |
| 540 | verifyFormat("public\n" |
| 541 | "int qwerty;"); |
| 542 | verifyFormat("public\n" |
| 543 | "B {\n" |
| 544 | "};"); |
| 545 | verifyFormat("public\n" |
| 546 | "{\n" |
| 547 | "};"); |
| 548 | verifyFormat("public\n" |
| 549 | "B {\n" |
| 550 | " int x;\n" |
| 551 | "};"); |
| 552 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 553 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 554 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 555 | verifyFormat("{"); |
| 556 | } |
| 557 | |
| 558 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 559 | verifyFormat("do {\n" |
| 560 | "};"); |
| 561 | verifyFormat("do {\n" |
| 562 | "};\n" |
| 563 | "f();"); |
| 564 | verifyFormat("do {\n" |
| 565 | "}\n" |
| 566 | "wheeee(fun);"); |
| 567 | verifyFormat("do {\n" |
| 568 | " f();\n" |
| 569 | "};"); |
| 570 | } |
| 571 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 572 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 573 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 574 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 575 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 576 | 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] | 577 | |
| 578 | FormatStyle Style = getLLVMStyle(); |
| 579 | Style.ColumnLimit = 10; |
| 580 | EXPECT_EQ("{\n" |
| 581 | " {\n" |
| 582 | " breakme(\n" |
| 583 | " qwe);\n" |
| 584 | "}\n", format("{\n" |
| 585 | " {\n" |
| 586 | " breakme(qwe);\n" |
| 587 | "}\n", Style)); |
| 588 | |
| 589 | } |
| 590 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 591 | } // end namespace tooling |
| 592 | } // end namespace clang |