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