Clang Project

clang_source_code/unittests/Format/FormatTestRawStrings.cpp
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
21using clang::tooling::ReplacementTest;
22using clang::tooling::toReplacements;
23
24namespace clang {
25namespace format {
26namespace {
27
28class FormatTestRawStrings : public ::testing::Test {
29protected:
30  enum StatusCheck { SC_ExpectCompleteSC_ExpectIncompleteSC_DoNotCheck };
31
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::RangeRanges(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
53  FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
54    Style.ColumnLimit = ColumnLimit;
55    return Style;
56  }
57
58  FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
59    return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
60  }
61
62  int ReplacementCount;
63
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
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
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.
109  void expect_eq(const std::string Expectedconst std::string Actual) {
110    EXPECT_EQ(ExpectedActual);
111  }
112};
113
114TEST_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
134TEST_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
155TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
156  // Don't touch the 'PB' raw string, format the 'pb' raw string.
157  expect_eq(R"test(
158s = R"PB(item:1)PB";
159t = R"pb(item: 1)pb";)test",
160            format(R"test(
161s = R"PB(item:1)PB";
162t = R"pb(item:1)pb";)test",
163                   getRawStringPbStyleWithColumns(40)));
164}
165
166TEST_F(FormatTestRawStrings, RespectsClangFormatOff) {
167  expect_eq(R"test(
168// clang-format off
169s = R"pb(item:      1)pb";
170// clang-format on
171t = R"pb(item: 1)pb";)test",
172            format(R"test(
173// clang-format off
174s = R"pb(item:      1)pb";
175// clang-format on
176t = R"pb(item:      1)pb";)test",
177                   getRawStringPbStyleWithColumns(40)));
178}
179
180TEST_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(
203std::string s = R"pb(
204  item_1: 1 item_2: 2
205)pb";
206)test",
207            format(R"test(
208std::string s = R"pb(
209  item_1:1
210  item_2:2
211)pb";
212)test",
213                   getRawStringPbStyleWithColumns(40)));
214}
215
216TEST_F(FormatTestRawStrings, BreaksShortRawStringsWhenNeeded) {
217  // The raw string contains multiple submessage entries, so break for
218  // readability.
219  expect_eq(R"test(
220P p = TP(R"pb(item_1 < 1 >
221              item_2: { 2 })pb");)test",
222      format(
223          R"test(
224P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
225          getRawStringPbStyleWithColumns(40)));
226}
227
228TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
229  expect_eq(R"test(
230P p = TPPPPPPPPPPPPPPP(
231    R"pb(item_1: 1, item_2: 2)pb");)test",
232            format(R"test(
233P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
234                   getRawStringPbStyleWithColumns(40)));
235
236  expect_eq(R"test(
237P p =
238    TPPPPPPPPPPPPPPP(
239        R"pb(item_1: 1,
240             item_2: 2,
241             item_3: 3)pb");)test",
242            format(R"test(
243P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
244                   getRawStringPbStyleWithColumns(40)));
245
246  expect_eq(R"test(
247P p = TP(R"pb(item_1 < 1 >
248              item_2: < 2 >
249              item_3 {})pb");)test",
250      format(R"test(
251P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
252          getRawStringPbStyleWithColumns(40)));
253
254  expect_eq(
255      R"test(
256P 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(
262P 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(
266P 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(
272P 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(
277FFFFFFFFFFFFFFFFFFFFFFFFFFF(
278    R"pb(key: 1)pb");
279P p = TPPPPPPPPPPPPPPPPPPPP(
280    R"pb(key: 2)pb");
281auto TPPPPPPPPPPPPPPPPPPPP =
282    R"pb(key: 3)pb";
283P p = TPPPPPPPPPPPPPPPPPPPP(
284    R"pb(i: 1, j: 2)pb");
285
286int 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(
299FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
300P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
301auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
302P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
303
304int 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
315TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
316  expect_eq(R"test(
317P p = TP(R"pb(key { 1 })pb", param_2);)test",
318            format(R"test(
319P p = TP(R"pb(key{1})pb",param_2);)test",
320                   getRawStringPbStyleWithColumns(40)));
321
322  expect_eq(R"test(
323PPPPPPPPPPPPP(R"pb(keykeyk)pb",
324              param_2);)test",
325            format(R"test(
326PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
327                   getRawStringPbStyleWithColumns(40)));
328
329  expect_eq(R"test(
330P p = TP(
331    R"pb(item: { i: 1, s: 's' }
332         item: { i: 2, s: 't' })pb");)test",
333            format(R"test(
334P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
335                   getRawStringPbStyleWithColumns(40)));
336  expect_eq(R"test(
337FFFFFFFFFFFFFFFFFFF(
338    R"pb(key: "value")pb",
339    R"pb(key2: "value")pb");)test",
340            format(R"test(
341FFFFFFFFFFFFFFFFFFF(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(
346FFFFFFFF(R"pb(key: 1)pb", argument2);
347struct 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(
358FFFFFFFF(R"pb(key:1)pb", argument2);
359struct S {
360const s = f(R"pb(key:1)pb", argument2);
361void 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(
371FFFFFFFF(argument1, R"pb(key: 2)pb");
372struct 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(
383FFFFFFFF(argument1, R"pb(key:2)pb");
384struct S {
385const s = f(argument1, R"pb(key:2)pb");
386void 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(
396FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
397            format(R"test(
398FFFFF(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(
407FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
408            format(R"test(
409FFFFFFF(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(
414FFFFFFFF(R"pb(key: 1)pb",
415         R"pb(key: 2)pb");)test",
416            format(R"test(
417FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
418                   getRawStringPbStyleWithColumns(40)));
419
420  // Formats both arguments.
421  expect_eq(R"test(
422FFFFFFFF(R"pb(key: 1)pb",
423         R"pb(key: 2)pb");
424struct 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(
435FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
436struct S {
437const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
438void 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
447TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
448  expect_eq(R"test(
449std::string s = R"pb(
450  item_1: 1
451)pb";
452)test",
453            format(R"test(
454std::string s = R"pb(
455    item_1:1
456)pb";
457)test",
458                   getRawStringPbStyleWithColumns(40)));
459
460  expect_eq(R"test(
461std::string s = R"pb(
462
463  item_1: 1
464)pb";
465)test",
466            format(R"test(
467std::string s = R"pb(
468
469    item_1:1
470)pb";
471)test",
472                   getRawStringPbStyleWithColumns(40)));
473
474  expect_eq(R"test(
475std::string s = R"pb(
476  item_1: 1
477)pb";
478)test",
479            format(R"test(
480std::string s = R"pb(
481    item_1:1
482
483)pb";
484)test",
485                   getRawStringPbStyleWithColumns(40)));
486
487  expect_eq(R"test(
488std::string s = R"pb(
489  item_1: 1,
490  item_2: 2
491)pb";
492)test",
493            format(R"test(
494std::string s = R"pb(
495  item_1:1, item_2:2
496)pb";
497)test",
498                   getRawStringPbStyleWithColumns(40)));
499
500  expect_eq(R"test(
501std::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(
513std::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
521TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
522  expect_eq(R"test(
523ASSERT_TRUE(
524    ParseFromString(R"pb(item_1: 1)pb"),
525    ptr);)test",
526            format(R"test(
527ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
528                   getRawStringPbStyleWithColumns(40)));
529
530  expect_eq(R"test(
531ASSERT_TRUE(toolong::ParseFromString(
532                R"pb(item_1: 1)pb"),
533            ptr);)test",
534            format(R"test(
535ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
536                   getRawStringPbStyleWithColumns(40)));
537
538  expect_eq(R"test(
539ASSERT_TRUE(ParseFromString(
540                R"pb(item_1: 1,
541                     item_2: 2)pb"),
542            ptr);)test",
543            format(R"test(
544ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
545                   getRawStringPbStyleWithColumns(40)));
546
547  expect_eq(R"test(
548ASSERT_TRUE(
549    ParseFromString(
550        R"pb(item_1: 1 item_2: 2)pb"),
551    ptr);)test",
552            format(R"test(
553ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
554                   getRawStringPbStyleWithColumns(40)));
555
556}
557
558TEST_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(
565auto S = R"pb(item_1: 1, item_2: 2)pb" +
566         rest;)test",
567            format(R"test(
568auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
569                   getRawStringPbStyleWithColumns(40)));
570
571  expect_eq(R"test(
572auto S =
573    R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
574            format(R"test(
575auto 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(
581auto S = R"pb(item_1: 1,
582              item_2: 2,
583              item_3: 3)pb" +
584         rest;)test",
585            format(R"test(
586auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
587                   getRawStringPbStyleWithColumns(40)));
588
589  expect_eq(R"test(
590auto S = R"pb(item_1: 1,
591              item_2: 2,
592              item_3: 3)pb" +
593         longlongrest;)test",
594            format(R"test(
595auto 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(
604auto S = first + R"pb(item_1: 1,
605                      item_2: 2)pb";)test",
606            format(R"test(
607auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
608                   getRawStringPbStyleWithColumns(40)));
609
610  expect_eq(R"test(
611auto S = first + R"pb(item_1: 1
612                      item_2: 2)pb";)test",
613            format(R"test(
614auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
615                   getRawStringPbStyleWithColumns(40)));
616
617  expect_eq(R"test(
618auto S = R"pb(item_1: 1,
619              item_2: 2,
620              item_3: 3)pb" +
621         rest;)test",
622            format(R"test(
623auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
624                   getRawStringPbStyleWithColumns(40)));
625
626  expect_eq(R"test(
627auto S = R"pb(item_1: 1,
628              item_2: 2,
629              item_3: 3)pb" +
630         longlongrest;)test",
631            format(R"test(
632auto 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(
637auto S = R"pb(item_1: 1)pb" +
638         R"pb(item_2: 2)pb";
639)test",
640            format(R"test(
641auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
642)test",
643                   getRawStringPbStyleWithColumns(40)));
644
645  expect_eq(R"test(
646auto 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(
651auto 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(
656auto S = (count < 3)
657             ? R"pb(item_1: 1)pb"
658             : R"pb(item_2: 2)pb";
659)test",
660            format(R"test(
661auto 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(
666auto 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(
672auto 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(
677auto 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(
683auto 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
689TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
690  // Keep the suffix at the end of line if not on newline.
691  expect_eq(R"test(
692int s() {
693  auto S = PTP(
694      R"pb(
695        item_1: 1,
696        item_2: 2)pb");
697})test",
698            format(R"test(
699int 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(
710int s() {
711  auto S = PTP(R"pb(
712    item_1: 1,
713    item_2: 2
714  )pb");
715})test",
716            format(R"test(
717int 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(
728int s() {
729  auto S = PTP(
730      R"pb(
731        item_1: 1,
732        item_2: 2,
733      )pb");
734})test",
735            format(R"test(
736int 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
746TEST_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(
750ff(gggggg,
751   hh(R"pb(key {
752             i1: k1
753             i2: k2
754           })pb"));
755)test",
756            format(R"test(
757ff(gggggg, hh(R"pb(key {
758    i1: k1
759    i2: k2
760    })pb"));
761)test",
762                   getRawStringPbStyleWithColumns(20)));
763}
764
765TEST_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
771TEST_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(
780a = PARSE_TEXT_PROTO /**/ (
781    /**/ R"(key: value)");)test",
782            format(R"test(
783a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
784                   Style));
785
786  expect_eq(R"test(
787a = ParseTextProto<ProtoType>(
788    R"(key: value)");)test",
789            format(R"test(
790a = ParseTextProto<ProtoType>(R"(key:value)");)test",
791                   Style));
792}
793
794TEST_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
806TEST_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(
811xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
812  Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
813)pb"));
814)test",
815            format(R"test(
816xxxxxxxaaaaax 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(
822xxxxxxxaaaaax wwwwwww =
823    _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
824      Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
825    )pb"));
826)test",
827            format(R"test(
828xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
829      Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
830)pb"));
831)test", Style));
832}
833
834TEST_F(FormatTestRawStrings, KeepsRBraceFolloedByMoreLBracesOnSameLine) {
835  FormatStyle Style = getRawStringPbStyleWithColumns(80);
836
837  expect_eq(
838                    R"test(
839int 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(
854int 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
868TEST_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(
877void f() {
878  aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
879item {
880  key: value
881}
882)Raw"));
883})test",
884            format(R"test(
885void f() {
886  aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
887item {
888  key: value
889}
890)Raw"));
891})test",
892                   Style));
893}
894
895TEST_F(FormatTestRawStrings,
896       BreaksBeforeNextParamAfterMultilineRawStringParam) {
897  FormatStyle Style = getRawStringPbStyleWithColumns(60);
898  expect_eq(R"test(
899int 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(
908int 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(
919int 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(
928int 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(
938int 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(
949int 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(
959int f() {
960  int a = g(x, R"pb(key1: value111111111
961                    key2: value2222222222)pb",
962            3, 4);
963}
964)test",
965            format(R"test(
966int 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(
974int f() {
975  int a = g(R"pb(key: 1)pb", 2);
976})test",
977            format(R"test(
978int f() {
979  int a = g(R"pb(key:
980                 1)pb", 2);
981})test", Style));
982}
983
984// end namespace
985// end namespace format
986// end namespace clang
987