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
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle (),StatusCheck CheckComplete=SC_ExpectComplete)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
getStyleWithColumns(FormatStyle Style,unsigned ColumnLimit)54 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
55 Style.ColumnLimit = ColumnLimit;
56 return Style;
57 }
58
getLLVMStyleWithColumns(unsigned ColumnLimit)59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60 return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
61 }
62
63 int ReplacementCount;
64
getRawStringPbStyleWithColumns(unsigned ColumnLimit)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
getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle)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
getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle)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.
expect_eq(const std::string Expected,const std::string Actual)110 void expect_eq(const std::string Expected, const std::string Actual) {
111 EXPECT_EQ(Expected, Actual);
112 }
113 };
114
TEST_F(FormatTestRawStrings,ReformatsAccordingToBaseStyle)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
TEST_F(FormatTestRawStrings,UsesConfigurationOverBaseStyle)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
TEST_F(FormatTestRawStrings,MatchesDelimitersCaseSensitively)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
TEST_F(FormatTestRawStrings,RespectsClangFormatOff)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
TEST_F(FormatTestRawStrings,ReformatsShortRawStringsOnSingleLine)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
TEST_F(FormatTestRawStrings,BreaksShortRawStringsWhenNeeded)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
TEST_F(FormatTestRawStrings,BreaksRawStringsExceedingColumnLimit)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
TEST_F(FormatTestRawStrings,FormatsRawStringArguments)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
TEST_F(FormatTestRawStrings,RawStringStartingWithNewlines)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
TEST_F(FormatTestRawStrings,BreaksBeforeRawStrings)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
TEST_F(FormatTestRawStrings,RawStringsInOperands)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 expect_eq(R"test(
580 auto S = R"pb(item_1: 1,
581 item_2: 2,
582 item_3: 3)pb" + rest;)test",
583 format(R"test(
584 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
585 getRawStringPbStyleWithColumns(40)));
586
587 expect_eq(R"test(
588 auto S = R"pb(item_1: 1,
589 item_2: 2,
590 item_3: 3)pb" +
591 longlongrest;)test",
592 format(R"test(
593 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
594 getRawStringPbStyleWithColumns(40)));
595
596 // Formats the raw string second operand of a binary operator expression.
597 expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test",
598 format(R"test(auto S = first + R"pb(item_1:1)pb";)test",
599 getRawStringPbStyleWithColumns(40)));
600
601 expect_eq(R"test(
602 auto S = first + R"pb(item_1: 1,
603 item_2: 2)pb";)test",
604 format(R"test(
605 auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
606 getRawStringPbStyleWithColumns(40)));
607
608 expect_eq(R"test(
609 auto S = first + R"pb(item_1: 1
610 item_2: 2)pb";)test",
611 format(R"test(
612 auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
613 getRawStringPbStyleWithColumns(40)));
614
615 expect_eq(R"test(
616 auto S = R"pb(item_1: 1,
617 item_2: 2,
618 item_3: 3)pb" + rest;)test",
619 format(R"test(
620 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
621 getRawStringPbStyleWithColumns(40)));
622
623 expect_eq(R"test(
624 auto S = R"pb(item_1: 1,
625 item_2: 2,
626 item_3: 3)pb" +
627 longlongrest;)test",
628 format(R"test(
629 auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
630 getRawStringPbStyleWithColumns(40)));
631
632 // Formats the raw string operands in expressions.
633 expect_eq(R"test(
634 auto S = R"pb(item_1: 1)pb" +
635 R"pb(item_2: 2)pb";
636 )test",
637 format(R"test(
638 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
639 )test",
640 getRawStringPbStyleWithColumns(40)));
641
642 expect_eq(R"test(
643 auto S = R"pb(item_1: 1)pb" +
644 R"pb(item_2: 2)pb" +
645 R"pb(item_3: 3)pb";
646 )test",
647 format(R"test(
648 auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
649 )test",
650 getRawStringPbStyleWithColumns(40)));
651
652 expect_eq(R"test(
653 auto S = (count < 3)
654 ? R"pb(item_1: 1)pb"
655 : R"pb(item_2: 2)pb";
656 )test",
657 format(R"test(
658 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb";
659 )test",
660 getRawStringPbStyleWithColumns(40)));
661
662 expect_eq(R"test(
663 auto S =
664 (count < 3)
665 ? R"pb(item_1: 1, item_2: 2)pb"
666 : R"pb(item_3: 3)pb";
667 )test",
668 format(R"test(
669 auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
670 )test",
671 getRawStringPbStyleWithColumns(40)));
672
673 expect_eq(R"test(
674 auto S =
675 (count < 3)
676 ? R"pb(item_1: 1)pb"
677 : R"pb(item_2: 2, item_3: 3)pb";
678 )test",
679 format(R"test(
680 auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
681 )test",
682 getRawStringPbStyleWithColumns(40)));
683
684 }
685
TEST_F(FormatTestRawStrings,PrefixAndSuffixAlignment)686 TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
687 // Keep the suffix at the end of line if not on newline.
688 expect_eq(R"test(
689 int s() {
690 auto S = PTP(
691 R"pb(
692 item_1: 1,
693 item_2: 2)pb");
694 })test",
695 format(R"test(
696 int s() {
697 auto S = PTP(
698 R"pb(
699 item_1: 1,
700 item_2: 2)pb");
701 })test",
702 getRawStringPbStyleWithColumns(20)));
703
704 // Align the suffix with the surrounding indent if the prefix is not on
705 // a line of its own.
706 expect_eq(R"test(
707 int s() {
708 auto S = PTP(R"pb(
709 item_1: 1,
710 item_2: 2
711 )pb");
712 })test",
713 format(R"test(
714 int s() {
715 auto S = PTP(R"pb(
716 item_1: 1,
717 item_2: 2
718 )pb");
719 })test",
720 getRawStringPbStyleWithColumns(20)));
721
722 // Align the prefix with the suffix if both the prefix and suffix are on a
723 // line of their own.
724 expect_eq(R"test(
725 int s() {
726 auto S = PTP(
727 R"pb(
728 item_1: 1,
729 item_2: 2,
730 )pb");
731 })test",
732 format(R"test(
733 int s() {
734 auto S = PTP(
735 R"pb(
736 item_1: 1,
737 item_2: 2,
738 )pb");
739 })test",
740 getRawStringPbStyleWithColumns(20)));
741 }
742
TEST_F(FormatTestRawStrings,EstimatesPenalty)743 TEST_F(FormatTestRawStrings, EstimatesPenalty) {
744 // The penalty for characters exceeding the column limit in the raw string
745 // forces 'hh' to be put on a newline.
746 expect_eq(R"test(
747 ff(gggggg,
748 hh(R"pb(key {
749 i1: k1
750 i2: k2
751 })pb"));
752 )test",
753 format(R"test(
754 ff(gggggg, hh(R"pb(key {
755 i1: k1
756 i2: k2
757 })pb"));
758 )test",
759 getRawStringPbStyleWithColumns(20)));
760 }
761
TEST_F(FormatTestRawStrings,DontFormatNonRawStrings)762 TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) {
763 expect_eq(R"test(a = R"pb(key:value)";)test",
764 format(R"test(a = R"pb(key:value)";)test",
765 getRawStringPbStyleWithColumns(20)));
766 }
767
TEST_F(FormatTestRawStrings,FormatsRawStringsWithEnclosingFunctionName)768 TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
769 FormatStyle Style = getRawStringPbStyleWithColumns(40);
770 Style.RawStringFormats[0].EnclosingFunctions.push_back(
771 "PARSE_TEXT_PROTO");
772 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
773 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
774 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
775
776 expect_eq(R"test(
777 a = PARSE_TEXT_PROTO /**/ (
778 /**/ R"(key: value)");)test",
779 format(R"test(
780 a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
781 Style));
782
783 expect_eq(R"test(
784 a = ParseTextProto<ProtoType>(
785 R"(key: value)");)test",
786 format(R"test(
787 a = ParseTextProto<ProtoType>(R"(key:value)");)test",
788 Style));
789 }
790
TEST_F(FormatTestRawStrings,UpdatesToCanonicalDelimiters)791 TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
792 FormatStyle Style = getRawStringPbStyleWithColumns(25);
793 Style.RawStringFormats[0].CanonicalDelimiter = "proto";
794 expect_eq(R"test(a = R"proto(key: value)proto";)test",
795 format(R"test(a = R"pb(key:value)pb";)test", Style));
796
797 // Don't update to canonical delimiter if it occurs as a raw string suffix in
798 // the raw string content.
799 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
800 format(R"test(a = R"pb(key:")proto")pb";)test", Style));
801 }
802
TEST_F(FormatTestRawStrings,PenalizesPrefixExcessChars)803 TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) {
804 FormatStyle Style = getRawStringPbStyleWithColumns(60);
805
806 // The '(' in R"pb is at column 60, no break.
807 expect_eq(R"test(
808 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
809 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
810 )pb"));
811 )test",
812 format(R"test(
813 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
814 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
815 )pb"));
816 )test", Style));
817 // The '(' in R"pb is at column 61, break.
818 expect_eq(R"test(
819 xxxxxxxaaaaax wwwwwww =
820 _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
821 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
822 )pb"));
823 )test",
824 format(R"test(
825 xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
826 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
827 )pb"));
828 )test", Style));
829 }
830
TEST_F(FormatTestRawStrings,KeepsRBraceFolloedByMoreLBracesOnSameLine)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
TEST_F(FormatTestRawStrings,DoNotFormatUnrecognizedDelimitersInRecognizedFunctions)865 TEST_F(FormatTestRawStrings,
866 DoNotFormatUnrecognizedDelimitersInRecognizedFunctions) {
867 FormatStyle Style = getRawStringPbStyleWithColumns(60);
868 Style.RawStringFormats[0].EnclosingFunctions.push_back(
869 "EqualsProto");
870 // EqualsProto is a recognized function, but the Raw delimiter is
871 // unrecognized. Do not touch the string in this case, since it might be
872 // special.
873 expect_eq(R"test(
874 void f() {
875 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
876 item {
877 key: value
878 }
879 )Raw"));
880 })test",
881 format(R"test(
882 void f() {
883 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
884 item {
885 key: value
886 }
887 )Raw"));
888 })test",
889 Style));
890 }
891
892 } // end namespace
893 } // end namespace format
894 } // end namespace clang
895