blob: b88ff07ee6377f1b13ac162872a843da0668e58e [file] [log] [blame]
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00001//===- 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
22using clang::tooling::ReplacementTest;
23using clang::tooling::toReplacements;
24
25namespace clang {
26namespace format {
27namespace {
28
29class FormatTestRawStrings : public ::testing::Test {
30protected:
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 DEBUG(llvm::errs() << "---\n");
37 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 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;
Krasimir Georgiev2537e222018-01-17 16:17:26 +000068 Style.RawStringFormats = {
Krasimir Georgiev412ed092018-01-19 16:18:47 +000069 {
70 /*Language=*/FormatStyle::LK_TextProto,
71 /*Delimiters=*/{"pb"},
72 /*EnclosingFunctions=*/{},
73 /*CanonicalDelimiter=*/"",
74 /*BasedOnStyle=*/"google",
75 },
Krasimir Georgiev2537e222018-01-17 16:17:26 +000076 };
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000077 return Style;
78 }
79
80 FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
81 FormatStyle Style = getLLVMStyle();
Krasimir Georgiev2537e222018-01-17 16:17:26 +000082 Style.RawStringFormats = {
Krasimir Georgiev412ed092018-01-19 16:18:47 +000083 {
84 /*Language=*/FormatStyle::LK_Cpp,
85 /*Delimiters=*/{"cpp"},
86 /*EnclosingFunctions=*/{},
87 /*CanonicalDelimiter=*/"",
88 BasedOnStyle,
89 },
Krasimir Georgiev2537e222018-01-17 16:17:26 +000090 };
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000091 return Style;
92 }
93
94 FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
95 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
Krasimir Georgiev2537e222018-01-17 16:17:26 +000096 Style.RawStringFormats = {
Krasimir Georgiev412ed092018-01-19 16:18:47 +000097 {
98 /*Language=*/FormatStyle::LK_Cpp,
99 /*Delimiters=*/{"cpp"},
100 /*EnclosingFunctions=*/{},
101 /*CanonicalDelimiter=*/"",
102 BasedOnStyle,
103 },
Krasimir Georgiev2537e222018-01-17 16:17:26 +0000104 };
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000105 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
115TEST_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
Krasimir Georgiev4527f132018-01-17 12:24:59 +0000135TEST_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());
Krasimir Georgiev412ed092018-01-19 16:18:47 +0000145 Style.RawStringFormats = {{
146 FormatStyle::LK_Cpp,
147 {"cpp"},
148 {},
149 /*CanonicalDelimiter=*/"",
150 /*BasedOnStyle=*/"llvm",
151 }};
Krasimir Georgiev4527f132018-01-17 12:24:59 +0000152 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
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000156TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
157 // Don't touch the 'PB' raw string, format the 'pb' raw string.
158 expect_eq(R"test(
159s = R"PB(item:1)PB";
160t = R"pb(item: 1)pb";)test",
161 format(R"test(
162s = R"PB(item:1)PB";
163t = R"pb(item:1)pb";)test",
164 getRawStringPbStyleWithColumns(40)));
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000165}
166
Daniel Jasperdd0c4e12018-03-12 10:11:30 +0000167TEST_F(FormatTestRawStrings, RespectsClangFormatOff) {
168 expect_eq(R"test(
169// clang-format off
170s = R"pb(item: 1)pb";
171// clang-format on
172t = R"pb(item: 1)pb";)test",
173 format(R"test(
174// clang-format off
175s = R"pb(item: 1)pb";
176// clang-format on
177t = R"pb(item: 1)pb";)test",
178 getRawStringPbStyleWithColumns(40)));
179}
180
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000181TEST_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 expect_eq(
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000203 R"test(P p = TP(R"pb(item_1 < 1 > item_2: { 2 })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000204 format(
205 R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
206 getRawStringPbStyleWithColumns(40)));
207
208 // Merge two short lines into one.
209 expect_eq(R"test(
210std::string s = R"pb(
211 item_1: 1 item_2: 2
212)pb";
213)test",
214 format(R"test(
215std::string s = R"pb(
216 item_1:1
217 item_2:2
218)pb";
219)test",
220 getRawStringPbStyleWithColumns(40)));
221}
222
223TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
224 expect_eq(R"test(
225P p = TPPPPPPPPPPPPPPP(
226 R"pb(item_1: 1, item_2: 2)pb");)test",
227 format(R"test(
228P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
229 getRawStringPbStyleWithColumns(40)));
230
231 expect_eq(R"test(
232P p =
233 TPPPPPPPPPPPPPPP(
234 R"pb(item_1: 1,
235 item_2: 2,
236 item_3: 3)pb");)test",
237 format(R"test(
238P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
239 getRawStringPbStyleWithColumns(40)));
240
241 expect_eq(R"test(
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000242P p = TP(R"pb(item_1 < 1 >
243 item_2: < 2 >
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000244 item_3 {})pb");)test",
245 format(R"test(
246P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
247 getRawStringPbStyleWithColumns(40)));
248
249 expect_eq(
250 R"test(
251P p = TP(R"pb(item_1: 1,
252 item_2: 2,
253 item_3: 3,
254 item_4: 4)pb");)test",
255 format(
256 R"test(
257P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test",
258 getRawStringPbStyleWithColumns(40)));
259
260 expect_eq(R"test(
261P p = TPPPPPPPPPPPPPPP(
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000262 R"pb(item_1 < 1 >,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000263 item_2: { 2 },
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000264 item_3: < 3 >,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000265 item_4: { 4 })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000266 format(R"test(
267P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test",
268 getRawStringPbStyleWithColumns(40)));
269
270 // Breaks before a short raw string exceeding the column limit.
271 expect_eq(R"test(
272FFFFFFFFFFFFFFFFFFFFFFFFFFF(
273 R"pb(key: 1)pb");
274P p = TPPPPPPPPPPPPPPPPPPPP(
275 R"pb(key: 2)pb");
276auto TPPPPPPPPPPPPPPPPPPPP =
277 R"pb(key: 3)pb";
278P p = TPPPPPPPPPPPPPPPPPPPP(
279 R"pb(i: 1, j: 2)pb");
280
281int f(string s) {
282 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(
283 R"pb(key: 1)pb");
284 P p = TPPPPPPPPPPPPPPPPPPPP(
285 R"pb(key: 2)pb");
286 auto TPPPPPPPPPPPPPPPPPPPP =
287 R"pb(key: 3)pb";
288 if (s.empty())
289 P p = TPPPPPPPPPPPPPPPPPPPP(
290 R"pb(i: 1, j: 2)pb");
291}
292)test",
293 format(R"test(
294FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
295P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
296auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
297P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
298
299int f(string s) {
300 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
301 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
302 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
303 if (s.empty())
304 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
305}
306)test",
307 getRawStringPbStyleWithColumns(40)));
308}
309
310TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
311 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000312P p = TP(R"pb(key { 1 })pb", param_2);)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000313 format(R"test(
314P p = TP(R"pb(key{1})pb",param_2);)test",
315 getRawStringPbStyleWithColumns(40)));
316
317 expect_eq(R"test(
318PPPPPPPPPPPPP(R"pb(keykeyk)pb",
319 param_2);)test",
320 format(R"test(
321PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
322 getRawStringPbStyleWithColumns(40)));
323
324 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000325P p = TP(
326 R"pb(item: { i: 1, s: 's' }
327 item: { i: 2, s: 't' })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000328 format(R"test(
329P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
330 getRawStringPbStyleWithColumns(40)));
331 expect_eq(R"test(
332FFFFFFFFFFFFFFFFFFF(
333 R"pb(key: "value")pb",
334 R"pb(key2: "value")pb");)test",
335 format(R"test(
336FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test",
337 getRawStringPbStyleWithColumns(40)));
338
339 // Formats the first out of two arguments.
340 expect_eq(R"test(
341FFFFFFFF(R"pb(key: 1)pb", argument2);
342struct S {
343 const s =
344 f(R"pb(key: 1)pb", argument2);
345 void f() {
346 if (gol)
347 return g(R"pb(key: 1)pb",
348 132789237);
349 return g(R"pb(key: 1)pb", "172893");
350 }
351};)test",
352 format(R"test(
353FFFFFFFF(R"pb(key:1)pb", argument2);
354struct S {
355const s = f(R"pb(key:1)pb", argument2);
356void f() {
357 if (gol)
358 return g(R"pb(key:1)pb", 132789237);
359 return g(R"pb(key:1)pb", "172893");
360}
361};)test",
362 getRawStringPbStyleWithColumns(40)));
363
364 // Formats the second out of two arguments.
365 expect_eq(R"test(
366FFFFFFFF(argument1, R"pb(key: 2)pb");
367struct S {
368 const s =
369 f(argument1, R"pb(key: 2)pb");
370 void f() {
371 if (gol)
372 return g(12784137,
373 R"pb(key: 2)pb");
374 return g(17283122, R"pb(key: 2)pb");
375 }
376};)test",
377 format(R"test(
378FFFFFFFF(argument1, R"pb(key:2)pb");
379struct S {
380const s = f(argument1, R"pb(key:2)pb");
381void f() {
382 if (gol)
383 return g(12784137, R"pb(key:2)pb");
384 return g(17283122, R"pb(key:2)pb");
385}
386};)test",
387 getRawStringPbStyleWithColumns(40)));
388
389 // Formats two short raw string arguments.
390 expect_eq(R"test(
391FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
392 format(R"test(
393FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
394 getRawStringPbStyleWithColumns(40)));
395 // TODO(krasimir): The original source code fits on one line, so the
396 // non-optimizing formatter is chosen. But after the formatting in protos is
397 // made, the code doesn't fit on one line anymore and further formatting
398 // splits it.
399 //
400 // Should we disable raw string formatting for the non-optimizing formatter?
401 expect_eq(R"test(
402FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
403 format(R"test(
404FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
405 getRawStringPbStyleWithColumns(40)));
406
407 // Formats two short raw string arguments, puts second on newline.
408 expect_eq(R"test(
409FFFFFFFF(R"pb(key: 1)pb",
410 R"pb(key: 2)pb");)test",
411 format(R"test(
412FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
413 getRawStringPbStyleWithColumns(40)));
414
415 // Formats both arguments.
416 expect_eq(R"test(
417FFFFFFFF(R"pb(key: 1)pb",
418 R"pb(key: 2)pb");
419struct S {
420 const s = f(R"pb(key: 1)pb",
421 R"pb(key: 2)pb");
422 void f() {
423 if (gol)
424 return g(R"pb(key: 1)pb",
425 R"pb(key: 2)pb");
426 return g(R"pb(k1)pb", R"pb(k2)pb");
427 }
428};)test",
429 format(R"test(
430FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
431struct S {
432const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
433void f() {
434 if (gol)
435 return g(R"pb(key:1)pb", R"pb(key:2)pb");
436 return g(R"pb( k1 )pb", R"pb( k2 )pb");
437}
438};)test",
439 getRawStringPbStyleWithColumns(40)));
440}
441
442TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
443 expect_eq(R"test(
444std::string s = R"pb(
445 item_1: 1
446)pb";
447)test",
448 format(R"test(
449std::string s = R"pb(
450 item_1:1
451)pb";
452)test",
453 getRawStringPbStyleWithColumns(40)));
454
455 expect_eq(R"test(
456std::string s = R"pb(
457
458 item_1: 1
459)pb";
460)test",
461 format(R"test(
462std::string s = R"pb(
463
464 item_1:1
465)pb";
466)test",
467 getRawStringPbStyleWithColumns(40)));
468
469 expect_eq(R"test(
470std::string s = R"pb(
471 item_1: 1
472)pb";
473)test",
474 format(R"test(
475std::string s = R"pb(
476 item_1:1
477
478)pb";
479)test",
480 getRawStringPbStyleWithColumns(40)));
481
482 expect_eq(R"test(
483std::string s = R"pb(
484 item_1: 1,
485 item_2: 2
486)pb";
487)test",
488 format(R"test(
489std::string s = R"pb(
490 item_1:1, item_2:2
491)pb";
492)test",
493 getRawStringPbStyleWithColumns(40)));
494
495 expect_eq(R"test(
496std::string s = R"pb(
497 book {
498 title: "Alice's Adventures"
499 author: "Lewis Caroll"
500 }
501 book {
502 title: "Peter Pan"
503 author: "J. M. Barrie"
504 }
505)pb";
506)test",
507 format(R"test(
508std::string s = R"pb(
509 book { title: "Alice's Adventures" author: "Lewis Caroll" }
510 book { title: "Peter Pan" author: "J. M. Barrie" }
511)pb";
512)test",
513 getRawStringPbStyleWithColumns(40)));
514}
515
516TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
517 expect_eq(R"test(
518ASSERT_TRUE(
519 ParseFromString(R"pb(item_1: 1)pb"),
520 ptr);)test",
521 format(R"test(
522ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
523 getRawStringPbStyleWithColumns(40)));
524
525 expect_eq(R"test(
526ASSERT_TRUE(toolong::ParseFromString(
527 R"pb(item_1: 1)pb"),
528 ptr);)test",
529 format(R"test(
530ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
531 getRawStringPbStyleWithColumns(40)));
532
533 expect_eq(R"test(
534ASSERT_TRUE(ParseFromString(
535 R"pb(item_1: 1,
536 item_2: 2)pb"),
537 ptr);)test",
538 format(R"test(
539ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
540 getRawStringPbStyleWithColumns(40)));
541
542 expect_eq(R"test(
543ASSERT_TRUE(
544 ParseFromString(
545 R"pb(item_1: 1 item_2: 2)pb"),
546 ptr);)test",
547 format(R"test(
548ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
549 getRawStringPbStyleWithColumns(40)));
550
551}
552
553TEST_F(FormatTestRawStrings, RawStringsInOperands) {
554 // Formats the raw string first operand of a binary operator expression.
555 expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test",
556 format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test",
557 getRawStringPbStyleWithColumns(40)));
558
559 expect_eq(R"test(
560auto S = R"pb(item_1: 1, item_2: 2)pb" +
561 rest;)test",
562 format(R"test(
563auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
564 getRawStringPbStyleWithColumns(40)));
565
566 expect_eq(R"test(
567auto S =
568 R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
569 format(R"test(
570auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test",
571 getRawStringPbStyleWithColumns(40)));
572
573 expect_eq(R"test(
574auto S = R"pb(item_1: 1,
575 item_2: 2,
576 item_3: 3)pb" + rest;)test",
577 format(R"test(
578auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
579 getRawStringPbStyleWithColumns(40)));
580
581 expect_eq(R"test(
582auto S = R"pb(item_1: 1,
583 item_2: 2,
584 item_3: 3)pb" +
585 longlongrest;)test",
586 format(R"test(
587auto 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(
596auto S = first + R"pb(item_1: 1,
597 item_2: 2)pb";)test",
598 format(R"test(
599auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
600 getRawStringPbStyleWithColumns(40)));
601
602 expect_eq(R"test(
603auto S = first + R"pb(item_1: 1
604 item_2: 2)pb";)test",
605 format(R"test(
606auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
607 getRawStringPbStyleWithColumns(40)));
608
609 expect_eq(R"test(
610auto S = R"pb(item_1: 1,
611 item_2: 2,
612 item_3: 3)pb" + rest;)test",
613 format(R"test(
614auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)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 longlongrest;)test",
622 format(R"test(
623auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
624 getRawStringPbStyleWithColumns(40)));
625
626 // Formats the raw string operands in expressions.
627 expect_eq(R"test(
628auto S = R"pb(item_1: 1)pb" +
629 R"pb(item_2: 2)pb";
630)test",
631 format(R"test(
632auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
633)test",
634 getRawStringPbStyleWithColumns(40)));
635
636 expect_eq(R"test(
637auto S = R"pb(item_1: 1)pb" +
638 R"pb(item_2: 2)pb" +
639 R"pb(item_3: 3)pb";
640)test",
641 format(R"test(
642auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
643)test",
644 getRawStringPbStyleWithColumns(40)));
645
646 expect_eq(R"test(
647auto S = (count < 3)
648 ? R"pb(item_1: 1)pb"
649 : R"pb(item_2: 2)pb";
650)test",
651 format(R"test(
652auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb";
653)test",
654 getRawStringPbStyleWithColumns(40)));
655
656 expect_eq(R"test(
657auto S =
658 (count < 3)
659 ? R"pb(item_1: 1, item_2: 2)pb"
660 : R"pb(item_3: 3)pb";
661)test",
662 format(R"test(
663auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
664)test",
665 getRawStringPbStyleWithColumns(40)));
666
667 expect_eq(R"test(
668auto S =
669 (count < 3)
670 ? R"pb(item_1: 1)pb"
671 : R"pb(item_2: 2, item_3: 3)pb";
672)test",
673 format(R"test(
674auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
675)test",
676 getRawStringPbStyleWithColumns(40)));
677
678}
679
680TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
681 // Keep the suffix at the end of line if not on newline.
682 expect_eq(R"test(
683int s() {
684 auto S = PTP(
685 R"pb(
686 item_1: 1,
687 item_2: 2)pb");
688})test",
689 format(R"test(
690int s() {
691 auto S = PTP(
692 R"pb(
693 item_1: 1,
694 item_2: 2)pb");
695})test",
696 getRawStringPbStyleWithColumns(20)));
697
Krasimir Georgieva71f6262018-03-08 11:29:27 +0000698 // Align the suffix with the surrounding indent if the prefix is not on
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000699 // a line of its own.
700 expect_eq(R"test(
701int s() {
Krasimir Georgieva71f6262018-03-08 11:29:27 +0000702 auto S = PTP(R"pb(
703 item_1: 1,
704 item_2: 2
705 )pb");
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000706})test",
707 format(R"test(
708int 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(
719int s() {
720 auto S = PTP(
721 R"pb(
722 item_1: 1,
723 item_2: 2,
724 )pb");
725})test",
726 format(R"test(
727int 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
737TEST_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(
741ff(gggggg,
742 hh(R"pb(key {
743 i1: k1
744 i2: k2
745 })pb"));
746)test",
747 format(R"test(
748ff(gggggg, hh(R"pb(key {
749 i1: k1
750 i2: k2
751 })pb"));
752)test",
753 getRawStringPbStyleWithColumns(20)));
754}
755
756TEST_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
Krasimir Georgiev2537e222018-01-17 16:17:26 +0000762TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
763 FormatStyle Style = getRawStringPbStyleWithColumns(40);
764 Style.RawStringFormats[0].EnclosingFunctions.push_back(
765 "PARSE_TEXT_PROTO");
766 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
767 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
768 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
769
770 expect_eq(R"test(
771a = PARSE_TEXT_PROTO /**/ (
772 /**/ R"(key: value)");)test",
773 format(R"test(
774a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
775 Style));
776
777 expect_eq(R"test(
778a = ParseTextProto<ProtoType>(
779 R"(key: value)");)test",
780 format(R"test(
781a = ParseTextProto<ProtoType>(R"(key:value)");)test",
782 Style));
783}
784
Krasimir Georgiev412ed092018-01-19 16:18:47 +0000785TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
786 FormatStyle Style = getRawStringPbStyleWithColumns(25);
787 Style.RawStringFormats[0].CanonicalDelimiter = "proto";
788 expect_eq(R"test(a = R"proto(key: value)proto";)test",
789 format(R"test(a = R"pb(key:value)pb";)test", Style));
790
791 // Don't update to canonical delimiter if it occurs as a raw string suffix in
792 // the raw string content.
793 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
794 format(R"test(a = R"pb(key:")proto")pb";)test", Style));
795}
796
Krasimir Georgievce009782018-03-16 14:01:25 +0000797TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) {
798 FormatStyle Style = getRawStringPbStyleWithColumns(60);
799
800 // The '(' in R"pb is at column 60, no break.
801 expect_eq(R"test(
802xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
803 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
804)pb"));
805)test",
806 format(R"test(
807xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
808 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
809)pb"));
810)test", Style));
811 // The '(' in R"pb is at column 61, break.
812 expect_eq(R"test(
813xxxxxxxaaaaax wwwwwww =
814 _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
815 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
816 )pb"));
817)test",
818 format(R"test(
819xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
820 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
821)pb"));
822)test", Style));
823}
824
Krasimir Georgiev8b4bc76a232018-05-09 09:02:11 +0000825TEST_F(FormatTestRawStrings, KeepsRBraceFolloedByMoreLBracesOnSameLine) {
826 FormatStyle Style = getRawStringPbStyleWithColumns(80);
827
828 expect_eq(
829 R"test(
830int f() {
831 if (1) {
832 TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb(
833 ttttttttt {
834 ppppppppppppp {
835 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" }
836 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" }
837 }
838 }
839 )pb");
840 }
841}
842)test",
843 format(
844 R"test(
845int f() {
846 if (1) {
847 TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb(
848 ttttttttt {
849 ppppppppppppp {
850 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" }
851 [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" }}}
852 )pb");
853 }
854}
855)test",
856 Style));
857}
858
859
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000860} // end namespace
861} // end namespace format
862} // end namespace clang