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 | |
| 248 | verifyFormat("f(/*test=*/ true);"); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | // Tests for classes, namespaces, etc. |
| 254 | //===----------------------------------------------------------------------===// |
| 255 | |
| 256 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 257 | verifyFormat("class A {\n};"); |
| 258 | } |
| 259 | |
| 260 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 261 | verifyFormat("class A {\n" |
| 262 | "public:\n" |
| 263 | "protected:\n" |
| 264 | "private:\n" |
| 265 | " void f() {\n" |
| 266 | " }\n" |
| 267 | "};"); |
| 268 | verifyGoogleFormat("class A {\n" |
| 269 | " public:\n" |
| 270 | " protected:\n" |
| 271 | " private:\n" |
| 272 | " void f() {\n" |
| 273 | " }\n" |
| 274 | "};"); |
| 275 | } |
| 276 | |
| 277 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 278 | verifyFormat("class A : public B {\n" |
| 279 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 280 | verifyFormat("class A : public ::B {\n" |
| 281 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 284 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 285 | verifyFormat("enum {\n" |
| 286 | " Zero,\n" |
| 287 | " One = 1,\n" |
| 288 | " Two = One + 1,\n" |
| 289 | " Three = (One + Two),\n" |
| 290 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 291 | " Five = (One, Two, Three, Four, 5)\n" |
| 292 | "};"); |
| 293 | verifyFormat("enum Enum {\n" |
| 294 | "};"); |
| 295 | verifyFormat("enum {\n" |
| 296 | "};"); |
| 297 | } |
| 298 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 299 | TEST_F(FormatTest, FormatsNamespaces) { |
| 300 | verifyFormat("namespace some_namespace {\n" |
| 301 | "class A {\n" |
| 302 | "};\n" |
| 303 | "void f() {\n" |
| 304 | " f();\n" |
| 305 | "}\n" |
| 306 | "}"); |
| 307 | verifyFormat("namespace {\n" |
| 308 | "class A {\n" |
| 309 | "};\n" |
| 310 | "void f() {\n" |
| 311 | " f();\n" |
| 312 | "}\n" |
| 313 | "}"); |
| 314 | verifyFormat("using namespace some_namespace;\n" |
| 315 | "class A {\n" |
| 316 | "};\n" |
| 317 | "void f() {\n" |
| 318 | " f();\n" |
| 319 | "}"); |
| 320 | } |
| 321 | |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | // Line break tests. |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 327 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 328 | " int h, int j, int f,\n" |
| 329 | " int c, int ddddddddddddd) {\n" |
| 330 | "}"); |
| 331 | } |
| 332 | |
| 333 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 334 | verifyFormat( |
| 335 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 336 | " parameter, parameter, parameter)), SecondLongCall(parameter));"); |
| 337 | } |
| 338 | |
| 339 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 340 | verifyFormat( |
| 341 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 342 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 343 | " f();"); |
| 344 | } |
| 345 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 346 | TEST_F(FormatTest, BreaksDesireably) { |
| 347 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 348 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 349 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 350 | |
| 351 | verifyFormat( |
| 352 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 353 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 354 | |
| 355 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 356 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 357 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 358 | |
| 359 | verifyFormat( |
| 360 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 361 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 362 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 363 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 364 | |
| 365 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 366 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 367 | |
| 368 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 369 | // taking into account the StopAt value. |
| 370 | verifyFormat( |
| 371 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 372 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 373 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 374 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 378 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 379 | " \"short literal\");"); |
| 380 | verifyFormat( |
| 381 | "looooooooooooooooooooooooongFunction(\n" |
| 382 | " \"short literal\"\n" |
| 383 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 384 | } |
| 385 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 386 | TEST_F(FormatTest, AlignsPipes) { |
| 387 | verifyFormat( |
| 388 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 389 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 390 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 391 | verifyFormat( |
| 392 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 393 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 394 | verifyFormat( |
| 395 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 396 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 397 | verifyFormat( |
| 398 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 399 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 400 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 401 | verifyFormat( |
| 402 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 403 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 404 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 405 | } |
| 406 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 407 | TEST_F(FormatTest, UnderstandsEquals) { |
| 408 | verifyFormat( |
| 409 | "aaaaaaaaaaaaaaaaa =\n" |
| 410 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 411 | verifyFormat( |
| 412 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 413 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 414 | "}"); |
| 415 | verifyFormat( |
| 416 | "if (a) {\n" |
| 417 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 418 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 419 | "}"); |
| 420 | |
| 421 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 422 | " 100000000 + 100000000) {\n}"); |
| 423 | } |
| 424 | |
| 425 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 426 | verifyFormat("A<int> a;"); |
| 427 | verifyFormat("A<A<A<int> > > a;"); |
| 428 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 429 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 430 | verifyFormat("bool x = 5 < f<int>();"); |
| 431 | verifyFormat("bool x = f<int>() > 5;"); |
| 432 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 433 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 434 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 435 | |
| 436 | verifyGoogleFormat("A<A<int>> a;"); |
| 437 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 438 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 439 | |
| 440 | verifyFormat("test >> a >> b;"); |
| 441 | verifyFormat("test << a >> b;"); |
| 442 | |
| 443 | verifyFormat("f<int>();"); |
| 444 | verifyFormat("template <typename T> void f() {\n}"); |
| 445 | } |
| 446 | |
| 447 | TEST_F(FormatTest, UndestandsUnaryOperators) { |
| 448 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 449 | verifyFormat("f(-1, -2, -3);"); |
| 450 | verifyFormat("a[-1] = 5;"); |
| 451 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 452 | verifyFormat("if (i == -1) {\n}"); |
| 453 | verifyFormat("if (i != -1) {\n}"); |
| 454 | verifyFormat("if (i > -1) {\n}"); |
| 455 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 456 | verifyFormat("++(a->f());"); |
| 457 | verifyFormat("--(a->f());"); |
| 458 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
| 462 | verifyFormat("bool operator<() {\n}"); |
| 463 | } |
| 464 | |
| 465 | TEST_F(FormatTest, UnderstandsUsesOfStar) { |
| 466 | verifyFormat("int *f(int *a) {\n}"); |
| 467 | verifyFormat("f(a, *a);"); |
| 468 | verifyFormat("f(*a);"); |
| 469 | verifyFormat("int a = b * 10;"); |
| 470 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 471 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 472 | verifyFormat("int a += b * c;"); |
| 473 | verifyFormat("int a -= b * c;"); |
| 474 | verifyFormat("int a *= b * c;"); |
| 475 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 476 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 477 | verifyFormat("int a = *b * c;"); |
| 478 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 479 | verifyFormat("int main(int argc, char **argv) {\n}"); |
| 480 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 481 | // FIXME: Is this desired for LLVM? Fix if not. |
| 482 | verifyFormat("A<int *> a;"); |
| 483 | verifyFormat("A<int **> a;"); |
| 484 | verifyFormat("A<int *, int *> a;"); |
| 485 | verifyFormat("A<int **, int **> a;"); |
| 486 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 487 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 488 | verifyGoogleFormat("A<int*> a;"); |
| 489 | verifyGoogleFormat("A<int**> a;"); |
| 490 | verifyGoogleFormat("A<int*, int*> a;"); |
| 491 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 494 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 495 | verifyFormat("(a)->b();"); |
| 496 | verifyFormat("--a;"); |
| 497 | } |
| 498 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 499 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 500 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
| 501 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 502 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 503 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 504 | } |
| 505 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 506 | |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | // Error recovery tests. |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 511 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 512 | verifyFormat("public:"); |
| 513 | verifyFormat("class A {\n" |
| 514 | "public\n" |
| 515 | " void f() {\n" |
| 516 | " }\n" |
| 517 | "};"); |
| 518 | verifyFormat("public\n" |
| 519 | "int qwerty;"); |
| 520 | verifyFormat("public\n" |
| 521 | "B {\n" |
| 522 | "};"); |
| 523 | verifyFormat("public\n" |
| 524 | "{\n" |
| 525 | "};"); |
| 526 | verifyFormat("public\n" |
| 527 | "B {\n" |
| 528 | " int x;\n" |
| 529 | "};"); |
| 530 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 531 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 532 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 533 | verifyFormat("{"); |
| 534 | } |
| 535 | |
| 536 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 537 | verifyFormat("do {\n" |
| 538 | "};"); |
| 539 | verifyFormat("do {\n" |
| 540 | "};\n" |
| 541 | "f();"); |
| 542 | verifyFormat("do {\n" |
| 543 | "}\n" |
| 544 | "wheeee(fun);"); |
| 545 | verifyFormat("do {\n" |
| 546 | " f();\n" |
| 547 | "};"); |
| 548 | } |
| 549 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 550 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 551 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 552 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 553 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 554 | 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] | 555 | |
| 556 | FormatStyle Style = getLLVMStyle(); |
| 557 | Style.ColumnLimit = 10; |
| 558 | EXPECT_EQ("{\n" |
| 559 | " {\n" |
| 560 | " breakme(\n" |
| 561 | " qwe);\n" |
| 562 | "}\n", format("{\n" |
| 563 | " {\n" |
| 564 | " breakme(qwe);\n" |
| 565 | "}\n", Style)); |
| 566 | |
| 567 | } |
| 568 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 569 | } // end namespace tooling |
| 570 | } // end namespace clang |