Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTestSelective.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 | |
| 10 | #include "FormatTestUtils.h" |
| 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | #define DEBUG_TYPE "format-test" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace format { |
| 19 | namespace { |
| 20 | |
| 21 | class FormatTestSelective : public ::testing::Test { |
| 22 | protected: |
| 23 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) { |
| 24 | DEBUG(llvm::errs() << "---\n"); |
| 25 | DEBUG(llvm::errs() << Code << "\n\n"); |
| 26 | std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); |
| 27 | bool IncompleteFormat = false; |
| 28 | tooling::Replacements Replaces = |
| 29 | reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); |
| 30 | EXPECT_FALSE(IncompleteFormat) << Code << "\n\n"; |
| 31 | std::string Result = applyAllReplacements(Code, Replaces); |
| 32 | EXPECT_NE("", Result); |
| 33 | DEBUG(llvm::errs() << "\n" << Result << "\n\n"); |
| 34 | return Result; |
| 35 | } |
| 36 | |
| 37 | FormatStyle Style = getLLVMStyle(); |
| 38 | }; |
| 39 | |
| 40 | TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { |
| 41 | EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0)); |
| 42 | EXPECT_EQ("int a;", format("int a; ", 0, 0)); |
| 43 | EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0)); |
| 44 | EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0)); |
| 45 | } |
| 46 | |
| 47 | TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 48 | EXPECT_EQ("{int b;\n" |
| 49 | " int a;\n" |
| 50 | "}", |
| 51 | format("{int b;\n int a;}", 8, 0)); |
| 52 | EXPECT_EQ("{\n" |
| 53 | " int b;\n" |
| 54 | " int a;}", |
| 55 | format("{int b;\n int a;}", 7, 0)); |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 56 | |
| 57 | Style.ColumnLimit = 12; |
| 58 | EXPECT_EQ("#define A \\\n" |
| 59 | " int a; \\\n" |
| 60 | " int b;", |
| 61 | format("#define A \\\n" |
| 62 | " int a; \\\n" |
| 63 | " int b;", |
| 64 | 26, 0)); |
| 65 | EXPECT_EQ("#define A \\\n" |
| 66 | " int a; \\\n" |
| 67 | " int b;", |
| 68 | format("#define A \\\n" |
| 69 | " int a; \\\n" |
| 70 | " int b;", |
| 71 | 25, 0)); |
| 72 | } |
| 73 | |
| 74 | TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) { |
| 75 | EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0)); |
| 76 | EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0)); |
| 77 | |
| 78 | // This might not strictly be correct, but is likely good in all practical |
| 79 | // cases. |
| 80 | EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0)); |
| 81 | } |
| 82 | |
| 83 | TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) { |
| 84 | EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0)); |
| 85 | EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0)); |
| 86 | } |
| 87 | |
| 88 | TEST_F(FormatTestSelective, ReformatsMovedLines) { |
| 89 | EXPECT_EQ( |
| 90 | "template <typename T> T *getFETokenInfo() const {\n" |
| 91 | " return static_cast<T *>(FETokenInfo);\n" |
| 92 | "}\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 93 | "int a; // <- Should not be formatted", |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 94 | format( |
| 95 | "template<typename T>\n" |
| 96 | "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 97 | "int a; // <- Should not be formatted", |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 98 | 9, 5)); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) { |
| 102 | Style.AllowShortIfStatementsOnASingleLine = true; |
| 103 | EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1)); |
| 104 | EXPECT_EQ("if (a) return; // comment", |
| 105 | format("if(a)\nreturn; // comment", 20, 1)); |
| 106 | } |
| 107 | |
| 108 | TEST_F(FormatTestSelective, FormatsCommentsLocally) { |
| 109 | EXPECT_EQ("int a; // comment\n" |
| 110 | "int b; // comment", |
| 111 | format("int a; // comment\n" |
| 112 | "int b; // comment", |
| 113 | 0, 0)); |
| 114 | EXPECT_EQ("int a; // comment\n" |
| 115 | " // line 2\n" |
| 116 | "int b;", |
| 117 | format("int a; // comment\n" |
| 118 | " // line 2\n" |
| 119 | "int b;", |
| 120 | 28, 0)); |
| 121 | EXPECT_EQ("int aaaaaa; // comment\n" |
| 122 | "int b;\n" |
| 123 | "int c; // unrelated comment", |
| 124 | format("int aaaaaa; // comment\n" |
| 125 | "int b;\n" |
| 126 | "int c; // unrelated comment", |
| 127 | 31, 0)); |
| 128 | |
| 129 | EXPECT_EQ("int a; // This\n" |
| 130 | " // is\n" |
| 131 | " // a", |
| 132 | format("int a; // This\n" |
| 133 | " // is\n" |
| 134 | " // a", |
| 135 | 0, 0)); |
| 136 | EXPECT_EQ("int a; // This\n" |
| 137 | " // is\n" |
| 138 | " // a\n" |
| 139 | "// This is b\n" |
| 140 | "int b;", |
| 141 | format("int a; // This\n" |
| 142 | " // is\n" |
| 143 | " // a\n" |
| 144 | "// This is b\n" |
| 145 | "int b;", |
| 146 | 0, 0)); |
| 147 | EXPECT_EQ("int a; // This\n" |
| 148 | " // is\n" |
| 149 | " // a\n" |
| 150 | "\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 151 | "//This is unrelated", |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 152 | format("int a; // This\n" |
| 153 | " // is\n" |
| 154 | " // a\n" |
| 155 | "\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 156 | "//This is unrelated", |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 157 | 0, 0)); |
| 158 | EXPECT_EQ("int a;\n" |
| 159 | "// This is\n" |
| 160 | "// not formatted. ", |
| 161 | format("int a;\n" |
| 162 | "// This is\n" |
| 163 | "// not formatted. ", |
| 164 | 0, 0)); |
Daniel Jasper | 417fc81 | 2016-01-09 15:56:53 +0000 | [diff] [blame] | 165 | EXPECT_EQ("int x; // Format this line.\n" |
| 166 | "int xx; //\n" |
| 167 | "int xxxxx; //", |
| 168 | format("int x; // Format this line.\n" |
| 169 | "int xx; //\n" |
| 170 | "int xxxxx; //", |
| 171 | 0, 0)); |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { |
| 175 | EXPECT_EQ("DEBUG({\n" |
| 176 | " int i;\n" |
| 177 | " int j;\n" |
| 178 | "});", |
| 179 | format("DEBUG( {\n" |
| 180 | " int i;\n" |
| 181 | " int j;\n" |
| 182 | "} ) ;", |
| 183 | 20, 1)); |
| 184 | EXPECT_EQ("DEBUG( {\n" |
| 185 | " int i;\n" |
| 186 | " int j;\n" |
| 187 | "} ) ;", |
| 188 | format("DEBUG( {\n" |
| 189 | " int i;\n" |
| 190 | " int j;\n" |
| 191 | "} ) ;", |
| 192 | 41, 1)); |
| 193 | EXPECT_EQ("DEBUG( {\n" |
| 194 | " int i;\n" |
| 195 | " int j;\n" |
| 196 | "} ) ;", |
| 197 | format("DEBUG( {\n" |
| 198 | " int i;\n" |
| 199 | " int j;\n" |
| 200 | "} ) ;", |
| 201 | 41, 1)); |
| 202 | EXPECT_EQ("DEBUG({\n" |
| 203 | " int i;\n" |
| 204 | " int j;\n" |
| 205 | "});", |
| 206 | format("DEBUG( {\n" |
| 207 | " int i;\n" |
| 208 | " int j;\n" |
| 209 | "} ) ;", |
| 210 | 20, 1)); |
| 211 | |
| 212 | EXPECT_EQ("Debug({\n" |
| 213 | " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 214 | " return;\n" |
| 215 | " },\n" |
| 216 | " a);", |
| 217 | format("Debug({\n" |
| 218 | " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 219 | " return;\n" |
| 220 | " },\n" |
| 221 | " a);", |
| 222 | 50, 1)); |
| 223 | EXPECT_EQ("DEBUG({\n" |
| 224 | " DEBUG({\n" |
| 225 | " int a;\n" |
| 226 | " int b;\n" |
| 227 | " }) ;\n" |
| 228 | "});", |
| 229 | format("DEBUG({\n" |
| 230 | " DEBUG({\n" |
| 231 | " int a;\n" |
| 232 | " int b;\n" // Format this line only. |
| 233 | " }) ;\n" // Don't touch this line. |
| 234 | "});", |
| 235 | 35, 0)); |
| 236 | EXPECT_EQ("DEBUG({\n" |
| 237 | " int a; //\n" |
| 238 | "});", |
| 239 | format("DEBUG({\n" |
| 240 | " int a; //\n" |
| 241 | "});", |
| 242 | 0, 0)); |
| 243 | EXPECT_EQ("someFunction(\n" |
| 244 | " [] {\n" |
| 245 | " // Only with this comment.\n" |
| 246 | " int i; // invoke formatting here.\n" |
| 247 | " }, // force line break\n" |
| 248 | " aaa);", |
| 249 | format("someFunction(\n" |
| 250 | " [] {\n" |
| 251 | " // Only with this comment.\n" |
| 252 | " int i; // invoke formatting here.\n" |
| 253 | " }, // force line break\n" |
| 254 | " aaa);", |
| 255 | 63, 1)); |
| 256 | |
| 257 | EXPECT_EQ("int longlongname; // comment\n" |
| 258 | "int x = f({\n" |
| 259 | " int x; // comment\n" |
| 260 | " int y; // comment\n" |
| 261 | "});", |
| 262 | format("int longlongname; // comment\n" |
| 263 | "int x = f({\n" |
| 264 | " int x; // comment\n" |
| 265 | " int y; // comment\n" |
| 266 | "});", |
| 267 | 65, 0)); |
| 268 | EXPECT_EQ("int s = f({\n" |
| 269 | " class X {\n" |
| 270 | " public:\n" |
| 271 | " void f();\n" |
| 272 | " };\n" |
| 273 | "});", |
| 274 | format("int s = f({\n" |
| 275 | " class X {\n" |
| 276 | " public:\n" |
| 277 | " void f();\n" |
| 278 | " };\n" |
| 279 | "});", |
| 280 | 0, 0)); |
Daniel Jasper | 35ca66d | 2016-02-29 12:26:20 +0000 | [diff] [blame] | 281 | EXPECT_EQ("SomeFunction(\n" |
| 282 | " [] {\n" |
| 283 | " int i;\n" |
| 284 | " return i;\n" // Format this line. |
| 285 | " },\n" |
| 286 | " [] {\n" |
| 287 | " return 2;\n" // Don't fix this. |
| 288 | " });", |
| 289 | format("SomeFunction(\n" |
| 290 | " [] {\n" |
| 291 | " int i;\n" |
| 292 | " return i;\n" // Format this line. |
| 293 | " },\n" |
| 294 | " [] {\n" |
| 295 | " return 2;\n" // Don't fix this. |
| 296 | " });", |
| 297 | 40, 0)); |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Daniel Jasper | f83834f | 2015-11-02 20:02:49 +0000 | [diff] [blame] | 300 | TEST_F(FormatTestSelective, WrongIndent) { |
| 301 | EXPECT_EQ("namespace {\n" |
| 302 | "int i;\n" |
| 303 | "int j;\n" |
| 304 | "}", |
| 305 | format("namespace {\n" |
| 306 | " int i;\n" // Format here. |
| 307 | " int j;\n" |
| 308 | "}", |
| 309 | 15, 0)); |
| 310 | EXPECT_EQ("namespace {\n" |
| 311 | " int i;\n" |
| 312 | " int j;\n" |
| 313 | "}", |
| 314 | format("namespace {\n" |
| 315 | " int i;\n" |
| 316 | " int j;\n" // Format here. |
| 317 | "}", |
| 318 | 24, 0)); |
| 319 | } |
| 320 | |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 321 | TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) { |
| 322 | Style.AlignEscapedNewlinesLeft = true; |
| 323 | EXPECT_EQ("int i;\n" |
| 324 | "#define A \\\n" |
| 325 | " int i; \\\n" |
| 326 | " int j\n" |
| 327 | "int k;", |
| 328 | format("int i;\n" |
| 329 | "#define A \\\n" |
| 330 | " int i ; \\\n" |
| 331 | " int j\n" |
| 332 | "int k;", |
| 333 | 8, 0)); // 8: position of "#define". |
| 334 | EXPECT_EQ("int i;\n" |
| 335 | "#define A \\\n" |
| 336 | " int i; \\\n" |
| 337 | " int j\n" |
| 338 | "int k;", |
| 339 | format("int i;\n" |
| 340 | "#define A \\\n" |
| 341 | " int i ; \\\n" |
| 342 | " int j\n" |
| 343 | "int k;", |
| 344 | 45, 0)); // 45: position of "j". |
| 345 | } |
| 346 | |
| 347 | TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { |
| 348 | EXPECT_EQ("{\n" |
| 349 | "{\n" |
| 350 | "a;\n" |
| 351 | "b;\n" |
| 352 | "}\n" |
| 353 | "}", |
| 354 | format("{\n" |
| 355 | "{\n" |
| 356 | "a;\n" |
| 357 | " b;\n" |
| 358 | "}\n" |
| 359 | "}", |
| 360 | 13, 2)); |
| 361 | EXPECT_EQ("{\n" |
| 362 | "{\n" |
| 363 | " a;\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 364 | " b;\n" |
| 365 | " c;\n" |
| 366 | " d;\n" |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 367 | "}\n" |
| 368 | "}", |
| 369 | format("{\n" |
| 370 | "{\n" |
| 371 | " a;\n" |
Daniel Jasper | a1036e5 | 2015-10-28 01:08:22 +0000 | [diff] [blame] | 372 | " b;\n" |
| 373 | " c;\n" |
| 374 | " d;\n" |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 375 | "}\n" |
| 376 | "}", |
| 377 | 9, 2)); |
| 378 | EXPECT_EQ("{\n" |
| 379 | "{\n" |
| 380 | "public:\n" |
| 381 | " b;\n" |
| 382 | "}\n" |
| 383 | "}", |
| 384 | format("{\n" |
| 385 | "{\n" |
| 386 | "public:\n" |
| 387 | " b;\n" |
| 388 | "}\n" |
| 389 | "}", |
| 390 | 17, 2)); |
| 391 | EXPECT_EQ("{\n" |
| 392 | "{\n" |
| 393 | "a;\n" |
| 394 | "}\n" |
| 395 | "{\n" |
| 396 | " b; //\n" |
| 397 | "}\n" |
| 398 | "}", |
| 399 | format("{\n" |
| 400 | "{\n" |
| 401 | "a;\n" |
| 402 | "}\n" |
| 403 | "{\n" |
| 404 | " b; //\n" |
| 405 | "}\n" |
| 406 | "}", |
| 407 | 22, 2)); |
| 408 | EXPECT_EQ(" {\n" |
| 409 | " a; //\n" |
| 410 | " }", |
| 411 | format(" {\n" |
| 412 | "a; //\n" |
| 413 | " }", |
| 414 | 4, 2)); |
| 415 | EXPECT_EQ("void f() {}\n" |
| 416 | "void g() {}", |
| 417 | format("void f() {}\n" |
| 418 | "void g() {}", |
| 419 | 13, 0)); |
| 420 | EXPECT_EQ("int a; // comment\n" |
| 421 | " // line 2\n" |
| 422 | "int b;", |
| 423 | format("int a; // comment\n" |
| 424 | " // line 2\n" |
| 425 | " int b;", |
| 426 | 35, 0)); |
| 427 | |
| 428 | EXPECT_EQ(" void f() {\n" |
| 429 | "#define A 1\n" |
| 430 | " }", |
| 431 | format(" void f() {\n" |
| 432 | " #define A 1\n" // Format this line. |
| 433 | " }", |
| 434 | 20, 0)); |
| 435 | EXPECT_EQ(" void f() {\n" |
| 436 | " int i;\n" |
| 437 | "#define A \\\n" |
| 438 | " int i; \\\n" |
| 439 | " int j;\n" |
| 440 | " int k;\n" |
| 441 | " }", |
| 442 | format(" void f() {\n" |
| 443 | " int i;\n" |
| 444 | "#define A \\\n" |
| 445 | " int i; \\\n" |
| 446 | " int j;\n" |
| 447 | " int k;\n" // Format this line. |
| 448 | " }", |
| 449 | 67, 0)); |
| 450 | |
| 451 | Style.ColumnLimit = 11; |
| 452 | EXPECT_EQ(" int a;\n" |
| 453 | " void\n" |
| 454 | " ffffff() {\n" |
| 455 | " }", |
| 456 | format(" int a;\n" |
| 457 | "void ffffff() {}", |
| 458 | 11, 0)); |
| 459 | } |
| 460 | |
| 461 | TEST_F(FormatTestSelective, UnderstandsTabs) { |
| 462 | Style.IndentWidth = 8; |
| 463 | Style.UseTab = FormatStyle::UT_Always; |
| 464 | Style.AlignEscapedNewlinesLeft = true; |
| 465 | EXPECT_EQ("void f() {\n" |
| 466 | "\tf();\n" |
| 467 | "\tg();\n" |
| 468 | "}", |
| 469 | format("void f() {\n" |
| 470 | "\tf();\n" |
| 471 | "\tg();\n" |
| 472 | "}", |
| 473 | 0, 0)); |
| 474 | EXPECT_EQ("void f() {\n" |
| 475 | "\tf();\n" |
| 476 | "\tg();\n" |
| 477 | "}", |
| 478 | format("void f() {\n" |
| 479 | "\tf();\n" |
| 480 | "\tg();\n" |
| 481 | "}", |
| 482 | 16, 0)); |
| 483 | EXPECT_EQ("void f() {\n" |
| 484 | " \tf();\n" |
| 485 | "\tg();\n" |
| 486 | "}", |
| 487 | format("void f() {\n" |
| 488 | " \tf();\n" |
| 489 | " \tg();\n" |
| 490 | "}", |
| 491 | 21, 0)); |
| 492 | } |
| 493 | |
Daniel Jasper | f67c324 | 2015-11-01 00:27:35 +0000 | [diff] [blame] | 494 | TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) { |
| 495 | EXPECT_EQ( |
| 496 | "void f() {\n" |
| 497 | " if (a) {\n" |
| 498 | " g();\n" |
| 499 | " h();\n" |
| 500 | "}\n" |
| 501 | "\n" |
| 502 | "void g() {\n" |
| 503 | "}", |
| 504 | format("void f() {\n" |
| 505 | " if (a) {\n" // Assume this was added without the closing brace. |
| 506 | " g();\n" |
| 507 | " h();\n" |
| 508 | "}\n" |
| 509 | "\n" |
| 510 | "void g() {\n" // Make sure not to format this. |
| 511 | "}", |
| 512 | 15, 0)); |
| 513 | } |
| 514 | |
Daniel Jasper | 9743992 | 2016-03-17 13:03:41 +0000 | [diff] [blame] | 515 | TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) { |
| 516 | Style = getGoogleStyle(FormatStyle::LK_JavaScript); |
| 517 | EXPECT_EQ( |
| 518 | "var x = \"a\";\n" |
| 519 | "var x = 'a';\n" |
| 520 | "var x = \"a\";", |
| 521 | format("var x = \"a\";\n" |
| 522 | "var x = \"a\";\n" |
| 523 | "var x = \"a\";", |
| 524 | 20, 0)); |
| 525 | } |
| 526 | |
Daniel Jasper | d246a5a | 2015-06-15 15:25:11 +0000 | [diff] [blame] | 527 | } // end namespace |
| 528 | } // end namespace format |
| 529 | } // end namespace clang |