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