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