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