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