1 //===- unittest/Format/FormatTestRawStrings.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 "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 class FormatTestRawStrings : public ::testing::Test { 30 protected: 31 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 32 33 std::string format(llvm::StringRef Code, 34 const FormatStyle &Style = getLLVMStyle(), 35 StatusCheck CheckComplete = SC_ExpectComplete) { 36 DEBUG(llvm::errs() << "---\n"); 37 DEBUG(llvm::errs() << Code << "\n\n"); 38 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 39 FormattingAttemptStatus Status; 40 tooling::Replacements Replaces = 41 reformat(Style, Code, Ranges, "<stdin>", &Status); 42 if (CheckComplete != SC_DoNotCheck) { 43 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 44 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 45 << Code << "\n\n"; 46 } 47 ReplacementCount = Replaces.size(); 48 auto Result = applyAllReplacements(Code, Replaces); 49 EXPECT_TRUE(static_cast<bool>(Result)); 50 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 51 return *Result; 52 } 53 54 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 55 Style.ColumnLimit = ColumnLimit; 56 return Style; 57 } 58 59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 60 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 61 } 62 63 int ReplacementCount; 64 65 FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) { 66 FormatStyle Style = getLLVMStyle(); 67 Style.ColumnLimit = ColumnLimit; 68 Style.RawStringFormats = { 69 { 70 /*Language=*/FormatStyle::LK_TextProto, 71 /*Delimiters=*/{"pb"}, 72 /*EnclosingFunctions=*/{}, 73 /*CanonicalDelimiter=*/"", 74 /*BasedOnStyle=*/"google", 75 }, 76 }; 77 return Style; 78 } 79 80 FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) { 81 FormatStyle Style = getLLVMStyle(); 82 Style.RawStringFormats = { 83 { 84 /*Language=*/FormatStyle::LK_Cpp, 85 /*Delimiters=*/{"cpp"}, 86 /*EnclosingFunctions=*/{}, 87 /*CanonicalDelimiter=*/"", 88 BasedOnStyle, 89 }, 90 }; 91 return Style; 92 } 93 94 FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) { 95 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 96 Style.RawStringFormats = { 97 { 98 /*Language=*/FormatStyle::LK_Cpp, 99 /*Delimiters=*/{"cpp"}, 100 /*EnclosingFunctions=*/{}, 101 /*CanonicalDelimiter=*/"", 102 BasedOnStyle, 103 }, 104 }; 105 return Style; 106 } 107 108 // Gcc 4.8 doesn't support raw string literals in macros, which breaks some 109 // build bots. We use this function instead. 110 void expect_eq(const std::string Expected, const std::string Actual) { 111 EXPECT_EQ(Expected, Actual); 112 } 113 }; 114 115 TEST_F(FormatTestRawStrings, ReformatsAccordingToBaseStyle) { 116 // llvm style puts '*' on the right. 117 // google style puts '*' on the left. 118 119 // Use the llvm style if the raw string style has no BasedOnStyle. 120 expect_eq(R"test(int *i = R"cpp(int *p = nullptr;)cpp")test", 121 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 122 getRawStringLLVMCppStyleBasedOn(""))); 123 124 // Use the google style if the raw string style has BasedOnStyle=google. 125 expect_eq(R"test(int *i = R"cpp(int* p = nullptr;)cpp")test", 126 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 127 getRawStringLLVMCppStyleBasedOn("google"))); 128 129 // Use the llvm style if the raw string style has no BasedOnStyle=llvm. 130 expect_eq(R"test(int* i = R"cpp(int *p = nullptr;)cpp")test", 131 format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test", 132 getRawStringGoogleCppStyleBasedOn("llvm"))); 133 } 134 135 TEST_F(FormatTestRawStrings, UsesConfigurationOverBaseStyle) { 136 // llvm style puts '*' on the right. 137 // google style puts '*' on the left. 138 139 // Uses the configured google style inside raw strings even if BasedOnStyle in 140 // the raw string format is llvm. 141 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 142 EXPECT_EQ(0, parseConfiguration("---\n" 143 "Language: Cpp\n" 144 "BasedOnStyle: Google", &Style).value()); 145 Style.RawStringFormats = {{ 146 FormatStyle::LK_Cpp, 147 {"cpp"}, 148 {}, 149 /*CanonicalDelimiter=*/"", 150 /*BasedOnStyle=*/"llvm", 151 }}; 152 expect_eq(R"test(int* i = R"cpp(int* j = 0;)cpp";)test", 153 format(R"test(int * i = R"cpp(int * j = 0;)cpp";)test", Style)); 154 } 155 156 TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) { 157 // Don't touch the 'PB' raw string, format the 'pb' raw string. 158 expect_eq(R"test( 159 s = R"PB(item:1)PB"; 160 t = R"pb(item: 1)pb";)test", 161 format(R"test( 162 s = R"PB(item:1)PB"; 163 t = R"pb(item:1)pb";)test", 164 getRawStringPbStyleWithColumns(40))); 165 } 166 167 TEST_F(FormatTestRawStrings, RespectsClangFormatOff) { 168 expect_eq(R"test( 169 // clang-format off 170 s = R"pb(item: 1)pb"; 171 // clang-format on 172 t = R"pb(item: 1)pb";)test", 173 format(R"test( 174 // clang-format off 175 s = R"pb(item: 1)pb"; 176 // clang-format on 177 t = R"pb(item: 1)pb";)test", 178 getRawStringPbStyleWithColumns(40))); 179 } 180 181 TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) { 182 expect_eq( 183 R"test(P p = TP(R"pb()pb");)test", 184 format( 185 R"test(P p = TP(R"pb( )pb");)test", 186 getRawStringPbStyleWithColumns(40))); 187 expect_eq( 188 R"test(P p = TP(R"pb(item_1: 1)pb");)test", 189 format( 190 R"test(P p = TP(R"pb(item_1:1)pb");)test", 191 getRawStringPbStyleWithColumns(40))); 192 expect_eq( 193 R"test(P p = TP(R"pb(item_1: 1)pb");)test", 194 format( 195 R"test(P p = TP(R"pb( item_1 : 1 )pb");)test", 196 getRawStringPbStyleWithColumns(40))); 197 expect_eq( 198 R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test", 199 format( 200 R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test", 201 getRawStringPbStyleWithColumns(40))); 202 expect_eq( 203 R"test(P p = TP(R"pb(item_1 < 1 > item_2: { 2 })pb");)test", 204 format( 205 R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test", 206 getRawStringPbStyleWithColumns(40))); 207 208 // Merge two short lines into one. 209 expect_eq(R"test( 210 std::string s = R"pb( 211 item_1: 1 item_2: 2 212 )pb"; 213 )test", 214 format(R"test( 215 std::string s = R"pb( 216 item_1:1 217 item_2:2 218 )pb"; 219 )test", 220 getRawStringPbStyleWithColumns(40))); 221 } 222 223 TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) { 224 expect_eq(R"test( 225 P p = TPPPPPPPPPPPPPPP( 226 R"pb(item_1: 1, item_2: 2)pb");)test", 227 format(R"test( 228 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test", 229 getRawStringPbStyleWithColumns(40))); 230 231 expect_eq(R"test( 232 P p = 233 TPPPPPPPPPPPPPPP( 234 R"pb(item_1: 1, 235 item_2: 2, 236 item_3: 3)pb");)test", 237 format(R"test( 238 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test", 239 getRawStringPbStyleWithColumns(40))); 240 241 expect_eq(R"test( 242 P p = TP(R"pb(item_1 < 1 > 243 item_2: < 2 > 244 item_3 {})pb");)test", 245 format(R"test( 246 P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test", 247 getRawStringPbStyleWithColumns(40))); 248 249 expect_eq( 250 R"test( 251 P p = TP(R"pb(item_1: 1, 252 item_2: 2, 253 item_3: 3, 254 item_4: 4)pb");)test", 255 format( 256 R"test( 257 P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test", 258 getRawStringPbStyleWithColumns(40))); 259 260 expect_eq(R"test( 261 P p = TPPPPPPPPPPPPPPP( 262 R"pb(item_1 < 1 >, 263 item_2: { 2 }, 264 item_3: < 3 >, 265 item_4: { 4 })pb");)test", 266 format(R"test( 267 P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test", 268 getRawStringPbStyleWithColumns(40))); 269 270 // Breaks before a short raw string exceeding the column limit. 271 expect_eq(R"test( 272 FFFFFFFFFFFFFFFFFFFFFFFFFFF( 273 R"pb(key: 1)pb"); 274 P p = TPPPPPPPPPPPPPPPPPPPP( 275 R"pb(key: 2)pb"); 276 auto TPPPPPPPPPPPPPPPPPPPP = 277 R"pb(key: 3)pb"; 278 P p = TPPPPPPPPPPPPPPPPPPPP( 279 R"pb(i: 1, j: 2)pb"); 280 281 int f(string s) { 282 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF( 283 R"pb(key: 1)pb"); 284 P p = TPPPPPPPPPPPPPPPPPPPP( 285 R"pb(key: 2)pb"); 286 auto TPPPPPPPPPPPPPPPPPPPP = 287 R"pb(key: 3)pb"; 288 if (s.empty()) 289 P p = TPPPPPPPPPPPPPPPPPPPP( 290 R"pb(i: 1, j: 2)pb"); 291 } 292 )test", 293 format(R"test( 294 FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 295 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 296 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 297 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 298 299 int f(string s) { 300 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 301 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 302 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 303 if (s.empty()) 304 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 305 } 306 )test", 307 getRawStringPbStyleWithColumns(40))); 308 } 309 310 TEST_F(FormatTestRawStrings, FormatsRawStringArguments) { 311 expect_eq(R"test( 312 P p = TP(R"pb(key { 1 })pb", param_2);)test", 313 format(R"test( 314 P p = TP(R"pb(key{1})pb",param_2);)test", 315 getRawStringPbStyleWithColumns(40))); 316 317 expect_eq(R"test( 318 PPPPPPPPPPPPP(R"pb(keykeyk)pb", 319 param_2);)test", 320 format(R"test( 321 PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test", 322 getRawStringPbStyleWithColumns(40))); 323 324 expect_eq(R"test( 325 P p = TP( 326 R"pb(item: { i: 1, s: 's' } 327 item: { i: 2, s: 't' })pb");)test", 328 format(R"test( 329 P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test", 330 getRawStringPbStyleWithColumns(40))); 331 expect_eq(R"test( 332 FFFFFFFFFFFFFFFFFFF( 333 R"pb(key: "value")pb", 334 R"pb(key2: "value")pb");)test", 335 format(R"test( 336 FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test", 337 getRawStringPbStyleWithColumns(40))); 338 339 // Formats the first out of two arguments. 340 expect_eq(R"test( 341 FFFFFFFF(R"pb(key: 1)pb", argument2); 342 struct S { 343 const s = 344 f(R"pb(key: 1)pb", argument2); 345 void f() { 346 if (gol) 347 return g(R"pb(key: 1)pb", 348 132789237); 349 return g(R"pb(key: 1)pb", "172893"); 350 } 351 };)test", 352 format(R"test( 353 FFFFFFFF(R"pb(key:1)pb", argument2); 354 struct S { 355 const s = f(R"pb(key:1)pb", argument2); 356 void f() { 357 if (gol) 358 return g(R"pb(key:1)pb", 132789237); 359 return g(R"pb(key:1)pb", "172893"); 360 } 361 };)test", 362 getRawStringPbStyleWithColumns(40))); 363 364 // Formats the second out of two arguments. 365 expect_eq(R"test( 366 FFFFFFFF(argument1, R"pb(key: 2)pb"); 367 struct S { 368 const s = 369 f(argument1, R"pb(key: 2)pb"); 370 void f() { 371 if (gol) 372 return g(12784137, 373 R"pb(key: 2)pb"); 374 return g(17283122, R"pb(key: 2)pb"); 375 } 376 };)test", 377 format(R"test( 378 FFFFFFFF(argument1, R"pb(key:2)pb"); 379 struct S { 380 const s = f(argument1, R"pb(key:2)pb"); 381 void f() { 382 if (gol) 383 return g(12784137, R"pb(key:2)pb"); 384 return g(17283122, R"pb(key:2)pb"); 385 } 386 };)test", 387 getRawStringPbStyleWithColumns(40))); 388 389 // Formats two short raw string arguments. 390 expect_eq(R"test( 391 FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 392 format(R"test( 393 FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 394 getRawStringPbStyleWithColumns(40))); 395 // TODO(krasimir): The original source code fits on one line, so the 396 // non-optimizing formatter is chosen. But after the formatting in protos is 397 // made, the code doesn't fit on one line anymore and further formatting 398 // splits it. 399 // 400 // Should we disable raw string formatting for the non-optimizing formatter? 401 expect_eq(R"test( 402 FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 403 format(R"test( 404 FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 405 getRawStringPbStyleWithColumns(40))); 406 407 // Formats two short raw string arguments, puts second on newline. 408 expect_eq(R"test( 409 FFFFFFFF(R"pb(key: 1)pb", 410 R"pb(key: 2)pb");)test", 411 format(R"test( 412 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 413 getRawStringPbStyleWithColumns(40))); 414 415 // Formats both arguments. 416 expect_eq(R"test( 417 FFFFFFFF(R"pb(key: 1)pb", 418 R"pb(key: 2)pb"); 419 struct S { 420 const s = f(R"pb(key: 1)pb", 421 R"pb(key: 2)pb"); 422 void f() { 423 if (gol) 424 return g(R"pb(key: 1)pb", 425 R"pb(key: 2)pb"); 426 return g(R"pb(k1)pb", R"pb(k2)pb"); 427 } 428 };)test", 429 format(R"test( 430 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb"); 431 struct S { 432 const s = f(R"pb(key:1)pb", R"pb(key:2)pb"); 433 void f() { 434 if (gol) 435 return g(R"pb(key:1)pb", R"pb(key:2)pb"); 436 return g(R"pb( k1 )pb", R"pb( k2 )pb"); 437 } 438 };)test", 439 getRawStringPbStyleWithColumns(40))); 440 } 441 442 TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) { 443 expect_eq(R"test( 444 std::string s = R"pb( 445 item_1: 1 446 )pb"; 447 )test", 448 format(R"test( 449 std::string s = R"pb( 450 item_1:1 451 )pb"; 452 )test", 453 getRawStringPbStyleWithColumns(40))); 454 455 expect_eq(R"test( 456 std::string s = R"pb( 457 458 item_1: 1 459 )pb"; 460 )test", 461 format(R"test( 462 std::string s = R"pb( 463 464 item_1:1 465 )pb"; 466 )test", 467 getRawStringPbStyleWithColumns(40))); 468 469 expect_eq(R"test( 470 std::string s = R"pb( 471 item_1: 1 472 )pb"; 473 )test", 474 format(R"test( 475 std::string s = R"pb( 476 item_1:1 477 478 )pb"; 479 )test", 480 getRawStringPbStyleWithColumns(40))); 481 482 expect_eq(R"test( 483 std::string s = R"pb( 484 item_1: 1, 485 item_2: 2 486 )pb"; 487 )test", 488 format(R"test( 489 std::string s = R"pb( 490 item_1:1, item_2:2 491 )pb"; 492 )test", 493 getRawStringPbStyleWithColumns(40))); 494 495 expect_eq(R"test( 496 std::string s = R"pb( 497 book { 498 title: "Alice's Adventures" 499 author: "Lewis Caroll" 500 } 501 book { 502 title: "Peter Pan" 503 author: "J. M. Barrie" 504 } 505 )pb"; 506 )test", 507 format(R"test( 508 std::string s = R"pb( 509 book { title: "Alice's Adventures" author: "Lewis Caroll" } 510 book { title: "Peter Pan" author: "J. M. Barrie" } 511 )pb"; 512 )test", 513 getRawStringPbStyleWithColumns(40))); 514 } 515 516 TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) { 517 expect_eq(R"test( 518 ASSERT_TRUE( 519 ParseFromString(R"pb(item_1: 1)pb"), 520 ptr);)test", 521 format(R"test( 522 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 523 getRawStringPbStyleWithColumns(40))); 524 525 expect_eq(R"test( 526 ASSERT_TRUE(toolong::ParseFromString( 527 R"pb(item_1: 1)pb"), 528 ptr);)test", 529 format(R"test( 530 ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 531 getRawStringPbStyleWithColumns(40))); 532 533 expect_eq(R"test( 534 ASSERT_TRUE(ParseFromString( 535 R"pb(item_1: 1, 536 item_2: 2)pb"), 537 ptr);)test", 538 format(R"test( 539 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test", 540 getRawStringPbStyleWithColumns(40))); 541 542 expect_eq(R"test( 543 ASSERT_TRUE( 544 ParseFromString( 545 R"pb(item_1: 1 item_2: 2)pb"), 546 ptr);)test", 547 format(R"test( 548 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test", 549 getRawStringPbStyleWithColumns(40))); 550 551 } 552 553 TEST_F(FormatTestRawStrings, RawStringsInOperands) { 554 // Formats the raw string first operand of a binary operator expression. 555 expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test", 556 format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test", 557 getRawStringPbStyleWithColumns(40))); 558 559 expect_eq(R"test( 560 auto S = R"pb(item_1: 1, item_2: 2)pb" + 561 rest;)test", 562 format(R"test( 563 auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test", 564 getRawStringPbStyleWithColumns(40))); 565 566 expect_eq(R"test( 567 auto S = 568 R"pb(item_1: 1 item_2: 2)pb" + rest;)test", 569 format(R"test( 570 auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test", 571 getRawStringPbStyleWithColumns(40))); 572 573 expect_eq(R"test( 574 auto S = R"pb(item_1: 1, 575 item_2: 2, 576 item_3: 3)pb" + rest;)test", 577 format(R"test( 578 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 579 getRawStringPbStyleWithColumns(40))); 580 581 expect_eq(R"test( 582 auto S = R"pb(item_1: 1, 583 item_2: 2, 584 item_3: 3)pb" + 585 longlongrest;)test", 586 format(R"test( 587 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 588 getRawStringPbStyleWithColumns(40))); 589 590 // Formats the raw string second operand of a binary operator expression. 591 expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test", 592 format(R"test(auto S = first + R"pb(item_1:1)pb";)test", 593 getRawStringPbStyleWithColumns(40))); 594 595 expect_eq(R"test( 596 auto S = first + R"pb(item_1: 1, 597 item_2: 2)pb";)test", 598 format(R"test( 599 auto S = first+R"pb(item_1:1,item_2:2)pb";)test", 600 getRawStringPbStyleWithColumns(40))); 601 602 expect_eq(R"test( 603 auto S = first + R"pb(item_1: 1 604 item_2: 2)pb";)test", 605 format(R"test( 606 auto S = first+R"pb(item_1:1 item_2:2)pb";)test", 607 getRawStringPbStyleWithColumns(40))); 608 609 expect_eq(R"test( 610 auto S = R"pb(item_1: 1, 611 item_2: 2, 612 item_3: 3)pb" + rest;)test", 613 format(R"test( 614 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 615 getRawStringPbStyleWithColumns(40))); 616 617 expect_eq(R"test( 618 auto S = R"pb(item_1: 1, 619 item_2: 2, 620 item_3: 3)pb" + 621 longlongrest;)test", 622 format(R"test( 623 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 624 getRawStringPbStyleWithColumns(40))); 625 626 // Formats the raw string operands in expressions. 627 expect_eq(R"test( 628 auto S = R"pb(item_1: 1)pb" + 629 R"pb(item_2: 2)pb"; 630 )test", 631 format(R"test( 632 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"; 633 )test", 634 getRawStringPbStyleWithColumns(40))); 635 636 expect_eq(R"test( 637 auto S = R"pb(item_1: 1)pb" + 638 R"pb(item_2: 2)pb" + 639 R"pb(item_3: 3)pb"; 640 )test", 641 format(R"test( 642 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb"; 643 )test", 644 getRawStringPbStyleWithColumns(40))); 645 646 expect_eq(R"test( 647 auto S = (count < 3) 648 ? R"pb(item_1: 1)pb" 649 : R"pb(item_2: 2)pb"; 650 )test", 651 format(R"test( 652 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb"; 653 )test", 654 getRawStringPbStyleWithColumns(40))); 655 656 expect_eq(R"test( 657 auto S = 658 (count < 3) 659 ? R"pb(item_1: 1, item_2: 2)pb" 660 : R"pb(item_3: 3)pb"; 661 )test", 662 format(R"test( 663 auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb"; 664 )test", 665 getRawStringPbStyleWithColumns(40))); 666 667 expect_eq(R"test( 668 auto S = 669 (count < 3) 670 ? R"pb(item_1: 1)pb" 671 : R"pb(item_2: 2, item_3: 3)pb"; 672 )test", 673 format(R"test( 674 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb"; 675 )test", 676 getRawStringPbStyleWithColumns(40))); 677 678 } 679 680 TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) { 681 // Keep the suffix at the end of line if not on newline. 682 expect_eq(R"test( 683 int s() { 684 auto S = PTP( 685 R"pb( 686 item_1: 1, 687 item_2: 2)pb"); 688 })test", 689 format(R"test( 690 int s() { 691 auto S = PTP( 692 R"pb( 693 item_1: 1, 694 item_2: 2)pb"); 695 })test", 696 getRawStringPbStyleWithColumns(20))); 697 698 // Align the suffix with the surrounding indent if the prefix is not on 699 // a line of its own. 700 expect_eq(R"test( 701 int s() { 702 auto S = PTP(R"pb( 703 item_1: 1, 704 item_2: 2 705 )pb"); 706 })test", 707 format(R"test( 708 int s() { 709 auto S = PTP(R"pb( 710 item_1: 1, 711 item_2: 2 712 )pb"); 713 })test", 714 getRawStringPbStyleWithColumns(20))); 715 716 // Align the prefix with the suffix if both the prefix and suffix are on a 717 // line of their own. 718 expect_eq(R"test( 719 int s() { 720 auto S = PTP( 721 R"pb( 722 item_1: 1, 723 item_2: 2, 724 )pb"); 725 })test", 726 format(R"test( 727 int s() { 728 auto S = PTP( 729 R"pb( 730 item_1: 1, 731 item_2: 2, 732 )pb"); 733 })test", 734 getRawStringPbStyleWithColumns(20))); 735 } 736 737 TEST_F(FormatTestRawStrings, EstimatesPenalty) { 738 // The penalty for characters exceeding the column limit in the raw string 739 // forces 'hh' to be put on a newline. 740 expect_eq(R"test( 741 ff(gggggg, 742 hh(R"pb(key { 743 i1: k1 744 i2: k2 745 })pb")); 746 )test", 747 format(R"test( 748 ff(gggggg, hh(R"pb(key { 749 i1: k1 750 i2: k2 751 })pb")); 752 )test", 753 getRawStringPbStyleWithColumns(20))); 754 } 755 756 TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) { 757 expect_eq(R"test(a = R"pb(key:value)";)test", 758 format(R"test(a = R"pb(key:value)";)test", 759 getRawStringPbStyleWithColumns(20))); 760 } 761 762 TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) { 763 FormatStyle Style = getRawStringPbStyleWithColumns(40); 764 Style.RawStringFormats[0].EnclosingFunctions.push_back( 765 "PARSE_TEXT_PROTO"); 766 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto"); 767 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test", 768 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style)); 769 770 expect_eq(R"test( 771 a = PARSE_TEXT_PROTO /**/ ( 772 /**/ R"(key: value)");)test", 773 format(R"test( 774 a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test", 775 Style)); 776 777 expect_eq(R"test( 778 a = ParseTextProto<ProtoType>( 779 R"(key: value)");)test", 780 format(R"test( 781 a = ParseTextProto<ProtoType>(R"(key:value)");)test", 782 Style)); 783 } 784 785 TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) { 786 FormatStyle Style = getRawStringPbStyleWithColumns(25); 787 Style.RawStringFormats[0].CanonicalDelimiter = "proto"; 788 expect_eq(R"test(a = R"proto(key: value)proto";)test", 789 format(R"test(a = R"pb(key:value)pb";)test", Style)); 790 791 // Don't update to canonical delimiter if it occurs as a raw string suffix in 792 // the raw string content. 793 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test", 794 format(R"test(a = R"pb(key:")proto")pb";)test", Style)); 795 } 796 797 TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) { 798 FormatStyle Style = getRawStringPbStyleWithColumns(60); 799 800 // The '(' in R"pb is at column 60, no break. 801 expect_eq(R"test( 802 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 803 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 804 )pb")); 805 )test", 806 format(R"test( 807 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 808 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 809 )pb")); 810 )test", Style)); 811 // The '(' in R"pb is at column 61, break. 812 expect_eq(R"test( 813 xxxxxxxaaaaax wwwwwww = 814 _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 815 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 816 )pb")); 817 )test", 818 format(R"test( 819 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( 820 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa 821 )pb")); 822 )test", Style)); 823 } 824 825 } // end namespace 826 } // end namespace format 827 } // end namespace clang 828