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, ReformatsShortRawStringsOnSingleLine) { 168 expect_eq( 169 R"test(P p = TP(R"pb()pb");)test", 170 format( 171 R"test(P p = TP(R"pb( )pb");)test", 172 getRawStringPbStyleWithColumns(40))); 173 expect_eq( 174 R"test(P p = TP(R"pb(item_1: 1)pb");)test", 175 format( 176 R"test(P p = TP(R"pb(item_1:1)pb");)test", 177 getRawStringPbStyleWithColumns(40))); 178 expect_eq( 179 R"test(P p = TP(R"pb(item_1: 1)pb");)test", 180 format( 181 R"test(P p = TP(R"pb( item_1 : 1 )pb");)test", 182 getRawStringPbStyleWithColumns(40))); 183 expect_eq( 184 R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test", 185 format( 186 R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test", 187 getRawStringPbStyleWithColumns(40))); 188 expect_eq( 189 R"test(P p = TP(R"pb(item_1 < 1 > item_2: { 2 })pb");)test", 190 format( 191 R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test", 192 getRawStringPbStyleWithColumns(40))); 193 194 // Merge two short lines into one. 195 expect_eq(R"test( 196 std::string s = R"pb( 197 item_1: 1 item_2: 2 198 )pb"; 199 )test", 200 format(R"test( 201 std::string s = R"pb( 202 item_1:1 203 item_2:2 204 )pb"; 205 )test", 206 getRawStringPbStyleWithColumns(40))); 207 } 208 209 TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) { 210 expect_eq(R"test( 211 P p = TPPPPPPPPPPPPPPP( 212 R"pb(item_1: 1, item_2: 2)pb");)test", 213 format(R"test( 214 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test", 215 getRawStringPbStyleWithColumns(40))); 216 217 expect_eq(R"test( 218 P p = 219 TPPPPPPPPPPPPPPP( 220 R"pb(item_1: 1, 221 item_2: 2, 222 item_3: 3)pb");)test", 223 format(R"test( 224 P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test", 225 getRawStringPbStyleWithColumns(40))); 226 227 expect_eq(R"test( 228 P p = TP(R"pb(item_1 < 1 > 229 item_2: < 2 > 230 item_3 {})pb");)test", 231 format(R"test( 232 P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test", 233 getRawStringPbStyleWithColumns(40))); 234 235 expect_eq( 236 R"test( 237 P p = TP(R"pb(item_1: 1, 238 item_2: 2, 239 item_3: 3, 240 item_4: 4)pb");)test", 241 format( 242 R"test( 243 P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test", 244 getRawStringPbStyleWithColumns(40))); 245 246 expect_eq(R"test( 247 P p = TPPPPPPPPPPPPPPP( 248 R"pb(item_1 < 1 >, 249 item_2: { 2 }, 250 item_3: < 3 >, 251 item_4: { 4 })pb");)test", 252 format(R"test( 253 P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test", 254 getRawStringPbStyleWithColumns(40))); 255 256 // Breaks before a short raw string exceeding the column limit. 257 expect_eq(R"test( 258 FFFFFFFFFFFFFFFFFFFFFFFFFFF( 259 R"pb(key: 1)pb"); 260 P p = TPPPPPPPPPPPPPPPPPPPP( 261 R"pb(key: 2)pb"); 262 auto TPPPPPPPPPPPPPPPPPPPP = 263 R"pb(key: 3)pb"; 264 P p = TPPPPPPPPPPPPPPPPPPPP( 265 R"pb(i: 1, j: 2)pb"); 266 267 int f(string s) { 268 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF( 269 R"pb(key: 1)pb"); 270 P p = TPPPPPPPPPPPPPPPPPPPP( 271 R"pb(key: 2)pb"); 272 auto TPPPPPPPPPPPPPPPPPPPP = 273 R"pb(key: 3)pb"; 274 if (s.empty()) 275 P p = TPPPPPPPPPPPPPPPPPPPP( 276 R"pb(i: 1, j: 2)pb"); 277 } 278 )test", 279 format(R"test( 280 FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 281 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 282 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 283 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 284 285 int f(string s) { 286 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb"); 287 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb"); 288 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb"; 289 if (s.empty()) 290 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb"); 291 } 292 )test", 293 getRawStringPbStyleWithColumns(40))); 294 } 295 296 TEST_F(FormatTestRawStrings, FormatsRawStringArguments) { 297 expect_eq(R"test( 298 P p = TP(R"pb(key { 1 })pb", param_2);)test", 299 format(R"test( 300 P p = TP(R"pb(key{1})pb",param_2);)test", 301 getRawStringPbStyleWithColumns(40))); 302 303 expect_eq(R"test( 304 PPPPPPPPPPPPP(R"pb(keykeyk)pb", 305 param_2);)test", 306 format(R"test( 307 PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test", 308 getRawStringPbStyleWithColumns(40))); 309 310 expect_eq(R"test( 311 P p = TP( 312 R"pb(item: { i: 1, s: 's' } 313 item: { i: 2, s: 't' })pb");)test", 314 format(R"test( 315 P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test", 316 getRawStringPbStyleWithColumns(40))); 317 expect_eq(R"test( 318 FFFFFFFFFFFFFFFFFFF( 319 R"pb(key: "value")pb", 320 R"pb(key2: "value")pb");)test", 321 format(R"test( 322 FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test", 323 getRawStringPbStyleWithColumns(40))); 324 325 // Formats the first out of two arguments. 326 expect_eq(R"test( 327 FFFFFFFF(R"pb(key: 1)pb", argument2); 328 struct S { 329 const s = 330 f(R"pb(key: 1)pb", argument2); 331 void f() { 332 if (gol) 333 return g(R"pb(key: 1)pb", 334 132789237); 335 return g(R"pb(key: 1)pb", "172893"); 336 } 337 };)test", 338 format(R"test( 339 FFFFFFFF(R"pb(key:1)pb", argument2); 340 struct S { 341 const s = f(R"pb(key:1)pb", argument2); 342 void f() { 343 if (gol) 344 return g(R"pb(key:1)pb", 132789237); 345 return g(R"pb(key:1)pb", "172893"); 346 } 347 };)test", 348 getRawStringPbStyleWithColumns(40))); 349 350 // Formats the second out of two arguments. 351 expect_eq(R"test( 352 FFFFFFFF(argument1, R"pb(key: 2)pb"); 353 struct S { 354 const s = 355 f(argument1, R"pb(key: 2)pb"); 356 void f() { 357 if (gol) 358 return g(12784137, 359 R"pb(key: 2)pb"); 360 return g(17283122, R"pb(key: 2)pb"); 361 } 362 };)test", 363 format(R"test( 364 FFFFFFFF(argument1, R"pb(key:2)pb"); 365 struct S { 366 const s = f(argument1, R"pb(key:2)pb"); 367 void f() { 368 if (gol) 369 return g(12784137, R"pb(key:2)pb"); 370 return g(17283122, R"pb(key:2)pb"); 371 } 372 };)test", 373 getRawStringPbStyleWithColumns(40))); 374 375 // Formats two short raw string arguments. 376 expect_eq(R"test( 377 FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 378 format(R"test( 379 FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 380 getRawStringPbStyleWithColumns(40))); 381 // TODO(krasimir): The original source code fits on one line, so the 382 // non-optimizing formatter is chosen. But after the formatting in protos is 383 // made, the code doesn't fit on one line anymore and further formatting 384 // splits it. 385 // 386 // Should we disable raw string formatting for the non-optimizing formatter? 387 expect_eq(R"test( 388 FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test", 389 format(R"test( 390 FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 391 getRawStringPbStyleWithColumns(40))); 392 393 // Formats two short raw string arguments, puts second on newline. 394 expect_eq(R"test( 395 FFFFFFFF(R"pb(key: 1)pb", 396 R"pb(key: 2)pb");)test", 397 format(R"test( 398 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test", 399 getRawStringPbStyleWithColumns(40))); 400 401 // Formats both arguments. 402 expect_eq(R"test( 403 FFFFFFFF(R"pb(key: 1)pb", 404 R"pb(key: 2)pb"); 405 struct S { 406 const s = f(R"pb(key: 1)pb", 407 R"pb(key: 2)pb"); 408 void f() { 409 if (gol) 410 return g(R"pb(key: 1)pb", 411 R"pb(key: 2)pb"); 412 return g(R"pb(k1)pb", R"pb(k2)pb"); 413 } 414 };)test", 415 format(R"test( 416 FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb"); 417 struct S { 418 const s = f(R"pb(key:1)pb", R"pb(key:2)pb"); 419 void f() { 420 if (gol) 421 return g(R"pb(key:1)pb", R"pb(key:2)pb"); 422 return g(R"pb( k1 )pb", R"pb( k2 )pb"); 423 } 424 };)test", 425 getRawStringPbStyleWithColumns(40))); 426 } 427 428 TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) { 429 expect_eq(R"test( 430 std::string s = R"pb( 431 item_1: 1 432 )pb"; 433 )test", 434 format(R"test( 435 std::string s = R"pb( 436 item_1:1 437 )pb"; 438 )test", 439 getRawStringPbStyleWithColumns(40))); 440 441 expect_eq(R"test( 442 std::string s = R"pb( 443 444 item_1: 1 445 )pb"; 446 )test", 447 format(R"test( 448 std::string s = R"pb( 449 450 item_1:1 451 )pb"; 452 )test", 453 getRawStringPbStyleWithColumns(40))); 454 455 expect_eq(R"test( 456 std::string s = R"pb( 457 item_1: 1 458 )pb"; 459 )test", 460 format(R"test( 461 std::string s = R"pb( 462 item_1:1 463 464 )pb"; 465 )test", 466 getRawStringPbStyleWithColumns(40))); 467 468 expect_eq(R"test( 469 std::string s = R"pb( 470 item_1: 1, 471 item_2: 2 472 )pb"; 473 )test", 474 format(R"test( 475 std::string s = R"pb( 476 item_1:1, item_2:2 477 )pb"; 478 )test", 479 getRawStringPbStyleWithColumns(40))); 480 481 expect_eq(R"test( 482 std::string s = R"pb( 483 book { 484 title: "Alice's Adventures" 485 author: "Lewis Caroll" 486 } 487 book { 488 title: "Peter Pan" 489 author: "J. M. Barrie" 490 } 491 )pb"; 492 )test", 493 format(R"test( 494 std::string s = R"pb( 495 book { title: "Alice's Adventures" author: "Lewis Caroll" } 496 book { title: "Peter Pan" author: "J. M. Barrie" } 497 )pb"; 498 )test", 499 getRawStringPbStyleWithColumns(40))); 500 } 501 502 TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) { 503 expect_eq(R"test( 504 ASSERT_TRUE( 505 ParseFromString(R"pb(item_1: 1)pb"), 506 ptr);)test", 507 format(R"test( 508 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 509 getRawStringPbStyleWithColumns(40))); 510 511 expect_eq(R"test( 512 ASSERT_TRUE(toolong::ParseFromString( 513 R"pb(item_1: 1)pb"), 514 ptr);)test", 515 format(R"test( 516 ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test", 517 getRawStringPbStyleWithColumns(40))); 518 519 expect_eq(R"test( 520 ASSERT_TRUE(ParseFromString( 521 R"pb(item_1: 1, 522 item_2: 2)pb"), 523 ptr);)test", 524 format(R"test( 525 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test", 526 getRawStringPbStyleWithColumns(40))); 527 528 expect_eq(R"test( 529 ASSERT_TRUE( 530 ParseFromString( 531 R"pb(item_1: 1 item_2: 2)pb"), 532 ptr);)test", 533 format(R"test( 534 ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test", 535 getRawStringPbStyleWithColumns(40))); 536 537 } 538 539 TEST_F(FormatTestRawStrings, RawStringsInOperands) { 540 // Formats the raw string first operand of a binary operator expression. 541 expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test", 542 format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test", 543 getRawStringPbStyleWithColumns(40))); 544 545 expect_eq(R"test( 546 auto S = R"pb(item_1: 1, item_2: 2)pb" + 547 rest;)test", 548 format(R"test( 549 auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test", 550 getRawStringPbStyleWithColumns(40))); 551 552 expect_eq(R"test( 553 auto S = 554 R"pb(item_1: 1 item_2: 2)pb" + rest;)test", 555 format(R"test( 556 auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test", 557 getRawStringPbStyleWithColumns(40))); 558 559 expect_eq(R"test( 560 auto S = R"pb(item_1: 1, 561 item_2: 2, 562 item_3: 3)pb" + rest;)test", 563 format(R"test( 564 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 565 getRawStringPbStyleWithColumns(40))); 566 567 expect_eq(R"test( 568 auto S = R"pb(item_1: 1, 569 item_2: 2, 570 item_3: 3)pb" + 571 longlongrest;)test", 572 format(R"test( 573 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 574 getRawStringPbStyleWithColumns(40))); 575 576 // Formats the raw string second operand of a binary operator expression. 577 expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test", 578 format(R"test(auto S = first + R"pb(item_1:1)pb";)test", 579 getRawStringPbStyleWithColumns(40))); 580 581 expect_eq(R"test( 582 auto S = first + R"pb(item_1: 1, 583 item_2: 2)pb";)test", 584 format(R"test( 585 auto S = first+R"pb(item_1:1,item_2:2)pb";)test", 586 getRawStringPbStyleWithColumns(40))); 587 588 expect_eq(R"test( 589 auto S = first + R"pb(item_1: 1 590 item_2: 2)pb";)test", 591 format(R"test( 592 auto S = first+R"pb(item_1:1 item_2:2)pb";)test", 593 getRawStringPbStyleWithColumns(40))); 594 595 expect_eq(R"test( 596 auto S = R"pb(item_1: 1, 597 item_2: 2, 598 item_3: 3)pb" + rest;)test", 599 format(R"test( 600 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test", 601 getRawStringPbStyleWithColumns(40))); 602 603 expect_eq(R"test( 604 auto S = R"pb(item_1: 1, 605 item_2: 2, 606 item_3: 3)pb" + 607 longlongrest;)test", 608 format(R"test( 609 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test", 610 getRawStringPbStyleWithColumns(40))); 611 612 // Formats the raw string operands in expressions. 613 expect_eq(R"test( 614 auto S = R"pb(item_1: 1)pb" + 615 R"pb(item_2: 2)pb"; 616 )test", 617 format(R"test( 618 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"; 619 )test", 620 getRawStringPbStyleWithColumns(40))); 621 622 expect_eq(R"test( 623 auto S = R"pb(item_1: 1)pb" + 624 R"pb(item_2: 2)pb" + 625 R"pb(item_3: 3)pb"; 626 )test", 627 format(R"test( 628 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb"; 629 )test", 630 getRawStringPbStyleWithColumns(40))); 631 632 expect_eq(R"test( 633 auto S = (count < 3) 634 ? R"pb(item_1: 1)pb" 635 : R"pb(item_2: 2)pb"; 636 )test", 637 format(R"test( 638 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb"; 639 )test", 640 getRawStringPbStyleWithColumns(40))); 641 642 expect_eq(R"test( 643 auto S = 644 (count < 3) 645 ? R"pb(item_1: 1, item_2: 2)pb" 646 : R"pb(item_3: 3)pb"; 647 )test", 648 format(R"test( 649 auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb"; 650 )test", 651 getRawStringPbStyleWithColumns(40))); 652 653 expect_eq(R"test( 654 auto S = 655 (count < 3) 656 ? R"pb(item_1: 1)pb" 657 : R"pb(item_2: 2, item_3: 3)pb"; 658 )test", 659 format(R"test( 660 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb"; 661 )test", 662 getRawStringPbStyleWithColumns(40))); 663 664 } 665 666 TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) { 667 // Keep the suffix at the end of line if not on newline. 668 expect_eq(R"test( 669 int s() { 670 auto S = PTP( 671 R"pb( 672 item_1: 1, 673 item_2: 2)pb"); 674 })test", 675 format(R"test( 676 int s() { 677 auto S = PTP( 678 R"pb( 679 item_1: 1, 680 item_2: 2)pb"); 681 })test", 682 getRawStringPbStyleWithColumns(20))); 683 684 // Align the suffix with the surrounding FirstIndent if the prefix is not on 685 // a line of its own. 686 expect_eq(R"test( 687 int s() { 688 auto S = PTP( 689 R"pb( 690 item_1: 1, 691 item_2: 2 692 )pb"); 693 })test", 694 format(R"test( 695 int s() { 696 auto S = PTP(R"pb( 697 item_1: 1, 698 item_2: 2 699 )pb"); 700 })test", 701 getRawStringPbStyleWithColumns(20))); 702 703 // Align the prefix with the suffix if both the prefix and suffix are on a 704 // line of their own. 705 expect_eq(R"test( 706 int s() { 707 auto S = PTP( 708 R"pb( 709 item_1: 1, 710 item_2: 2, 711 )pb"); 712 })test", 713 format(R"test( 714 int s() { 715 auto S = PTP( 716 R"pb( 717 item_1: 1, 718 item_2: 2, 719 )pb"); 720 })test", 721 getRawStringPbStyleWithColumns(20))); 722 } 723 724 TEST_F(FormatTestRawStrings, EstimatesPenalty) { 725 // The penalty for characters exceeding the column limit in the raw string 726 // forces 'hh' to be put on a newline. 727 expect_eq(R"test( 728 ff(gggggg, 729 hh(R"pb(key { 730 i1: k1 731 i2: k2 732 })pb")); 733 )test", 734 format(R"test( 735 ff(gggggg, hh(R"pb(key { 736 i1: k1 737 i2: k2 738 })pb")); 739 )test", 740 getRawStringPbStyleWithColumns(20))); 741 } 742 743 TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) { 744 expect_eq(R"test(a = R"pb(key:value)";)test", 745 format(R"test(a = R"pb(key:value)";)test", 746 getRawStringPbStyleWithColumns(20))); 747 } 748 749 TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) { 750 FormatStyle Style = getRawStringPbStyleWithColumns(40); 751 Style.RawStringFormats[0].EnclosingFunctions.push_back( 752 "PARSE_TEXT_PROTO"); 753 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto"); 754 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test", 755 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style)); 756 757 expect_eq(R"test( 758 a = PARSE_TEXT_PROTO /**/ ( 759 /**/ R"(key: value)");)test", 760 format(R"test( 761 a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test", 762 Style)); 763 764 expect_eq(R"test( 765 a = ParseTextProto<ProtoType>( 766 R"(key: value)");)test", 767 format(R"test( 768 a = ParseTextProto<ProtoType>(R"(key:value)");)test", 769 Style)); 770 } 771 772 TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) { 773 FormatStyle Style = getRawStringPbStyleWithColumns(25); 774 Style.RawStringFormats[0].CanonicalDelimiter = "proto"; 775 expect_eq(R"test(a = R"proto(key: value)proto";)test", 776 format(R"test(a = R"pb(key:value)pb";)test", Style)); 777 778 // Don't update to canonical delimiter if it occurs as a raw string suffix in 779 // the raw string content. 780 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test", 781 format(R"test(a = R"pb(key:")proto")pb";)test", Style)); 782 } 783 784 } // end namespace 785 } // end namespace format 786 } // end namespace clang 787