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