blob: 4d1da9775951670cd5585425d7115370eb0b7568 [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
167TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) {
168 expect_eq(
169 R"test(P p = TP(R"pb()pb");)test",
170 format(
171 R"test(P p = TP(R"pb( )pb");)test",
172 getRawStringPbStyleWithColumns(40)));
173 expect_eq(
174 R"test(P p = TP(R"pb(item_1: 1)pb");)test",
175 format(
176 R"test(P p = TP(R"pb(item_1:1)pb");)test",
177 getRawStringPbStyleWithColumns(40)));
178 expect_eq(
179 R"test(P p = TP(R"pb(item_1: 1)pb");)test",
180 format(
181 R"test(P p = TP(R"pb( item_1 : 1 )pb");)test",
182 getRawStringPbStyleWithColumns(40)));
183 expect_eq(
184 R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test",
185 format(
186 R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test",
187 getRawStringPbStyleWithColumns(40)));
188 expect_eq(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000189 R"test(P p = TP(R"pb(item_1 <1> item_2: { 2 })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000190 format(
191 R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
192 getRawStringPbStyleWithColumns(40)));
193
194 // Merge two short lines into one.
195 expect_eq(R"test(
196std::string s = R"pb(
197 item_1: 1 item_2: 2
198)pb";
199)test",
200 format(R"test(
201std::string s = R"pb(
202 item_1:1
203 item_2:2
204)pb";
205)test",
206 getRawStringPbStyleWithColumns(40)));
207}
208
209TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
210 expect_eq(R"test(
211P p = TPPPPPPPPPPPPPPP(
212 R"pb(item_1: 1, item_2: 2)pb");)test",
213 format(R"test(
214P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
215 getRawStringPbStyleWithColumns(40)));
216
217 expect_eq(R"test(
218P p =
219 TPPPPPPPPPPPPPPP(
220 R"pb(item_1: 1,
221 item_2: 2,
222 item_3: 3)pb");)test",
223 format(R"test(
224P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
225 getRawStringPbStyleWithColumns(40)));
226
227 expect_eq(R"test(
228P p = TP(R"pb(item_1 <1>
229 item_2: <2>
230 item_3 {})pb");)test",
231 format(R"test(
232P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
233 getRawStringPbStyleWithColumns(40)));
234
235 expect_eq(
236 R"test(
237P p = TP(R"pb(item_1: 1,
238 item_2: 2,
239 item_3: 3,
240 item_4: 4)pb");)test",
241 format(
242 R"test(
243P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test",
244 getRawStringPbStyleWithColumns(40)));
245
246 expect_eq(R"test(
247P p = TPPPPPPPPPPPPPPP(
248 R"pb(item_1 <1>,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000249 item_2: { 2 },
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000250 item_3: <3>,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000251 item_4: { 4 })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000252 format(R"test(
253P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test",
254 getRawStringPbStyleWithColumns(40)));
255
256 // Breaks before a short raw string exceeding the column limit.
257 expect_eq(R"test(
258FFFFFFFFFFFFFFFFFFFFFFFFFFF(
259 R"pb(key: 1)pb");
260P p = TPPPPPPPPPPPPPPPPPPPP(
261 R"pb(key: 2)pb");
262auto TPPPPPPPPPPPPPPPPPPPP =
263 R"pb(key: 3)pb";
264P p = TPPPPPPPPPPPPPPPPPPPP(
265 R"pb(i: 1, j: 2)pb");
266
267int f(string s) {
268 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(
269 R"pb(key: 1)pb");
270 P p = TPPPPPPPPPPPPPPPPPPPP(
271 R"pb(key: 2)pb");
272 auto TPPPPPPPPPPPPPPPPPPPP =
273 R"pb(key: 3)pb";
274 if (s.empty())
275 P p = TPPPPPPPPPPPPPPPPPPPP(
276 R"pb(i: 1, j: 2)pb");
277}
278)test",
279 format(R"test(
280FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
281P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
282auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
283P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
284
285int f(string s) {
286 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
287 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
288 auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
289 if (s.empty())
290 P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
291}
292)test",
293 getRawStringPbStyleWithColumns(40)));
294}
295
296TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
297 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000298P p = TP(R"pb(key { 1 })pb", param_2);)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000299 format(R"test(
300P p = TP(R"pb(key{1})pb",param_2);)test",
301 getRawStringPbStyleWithColumns(40)));
302
303 expect_eq(R"test(
304PPPPPPPPPPPPP(R"pb(keykeyk)pb",
305 param_2);)test",
306 format(R"test(
307PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
308 getRawStringPbStyleWithColumns(40)));
309
310 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000311P p = TP(
312 R"pb(item: { i: 1, s: 's' }
313 item: { i: 2, s: 't' })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000314 format(R"test(
315P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
316 getRawStringPbStyleWithColumns(40)));
317 expect_eq(R"test(
318FFFFFFFFFFFFFFFFFFF(
319 R"pb(key: "value")pb",
320 R"pb(key2: "value")pb");)test",
321 format(R"test(
322FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test",
323 getRawStringPbStyleWithColumns(40)));
324
325 // Formats the first out of two arguments.
326 expect_eq(R"test(
327FFFFFFFF(R"pb(key: 1)pb", argument2);
328struct S {
329 const s =
330 f(R"pb(key: 1)pb", argument2);
331 void f() {
332 if (gol)
333 return g(R"pb(key: 1)pb",
334 132789237);
335 return g(R"pb(key: 1)pb", "172893");
336 }
337};)test",
338 format(R"test(
339FFFFFFFF(R"pb(key:1)pb", argument2);
340struct S {
341const s = f(R"pb(key:1)pb", argument2);
342void f() {
343 if (gol)
344 return g(R"pb(key:1)pb", 132789237);
345 return g(R"pb(key:1)pb", "172893");
346}
347};)test",
348 getRawStringPbStyleWithColumns(40)));
349
350 // Formats the second out of two arguments.
351 expect_eq(R"test(
352FFFFFFFF(argument1, R"pb(key: 2)pb");
353struct S {
354 const s =
355 f(argument1, R"pb(key: 2)pb");
356 void f() {
357 if (gol)
358 return g(12784137,
359 R"pb(key: 2)pb");
360 return g(17283122, R"pb(key: 2)pb");
361 }
362};)test",
363 format(R"test(
364FFFFFFFF(argument1, R"pb(key:2)pb");
365struct S {
366const s = f(argument1, R"pb(key:2)pb");
367void f() {
368 if (gol)
369 return g(12784137, R"pb(key:2)pb");
370 return g(17283122, R"pb(key:2)pb");
371}
372};)test",
373 getRawStringPbStyleWithColumns(40)));
374
375 // Formats two short raw string arguments.
376 expect_eq(R"test(
377FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
378 format(R"test(
379FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
380 getRawStringPbStyleWithColumns(40)));
381 // TODO(krasimir): The original source code fits on one line, so the
382 // non-optimizing formatter is chosen. But after the formatting in protos is
383 // made, the code doesn't fit on one line anymore and further formatting
384 // splits it.
385 //
386 // Should we disable raw string formatting for the non-optimizing formatter?
387 expect_eq(R"test(
388FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
389 format(R"test(
390FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
391 getRawStringPbStyleWithColumns(40)));
392
393 // Formats two short raw string arguments, puts second on newline.
394 expect_eq(R"test(
395FFFFFFFF(R"pb(key: 1)pb",
396 R"pb(key: 2)pb");)test",
397 format(R"test(
398FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
399 getRawStringPbStyleWithColumns(40)));
400
401 // Formats both arguments.
402 expect_eq(R"test(
403FFFFFFFF(R"pb(key: 1)pb",
404 R"pb(key: 2)pb");
405struct S {
406 const s = f(R"pb(key: 1)pb",
407 R"pb(key: 2)pb");
408 void f() {
409 if (gol)
410 return g(R"pb(key: 1)pb",
411 R"pb(key: 2)pb");
412 return g(R"pb(k1)pb", R"pb(k2)pb");
413 }
414};)test",
415 format(R"test(
416FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
417struct S {
418const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
419void f() {
420 if (gol)
421 return g(R"pb(key:1)pb", R"pb(key:2)pb");
422 return g(R"pb( k1 )pb", R"pb( k2 )pb");
423}
424};)test",
425 getRawStringPbStyleWithColumns(40)));
426}
427
428TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
429 expect_eq(R"test(
430std::string s = R"pb(
431 item_1: 1
432)pb";
433)test",
434 format(R"test(
435std::string s = R"pb(
436 item_1:1
437)pb";
438)test",
439 getRawStringPbStyleWithColumns(40)));
440
441 expect_eq(R"test(
442std::string s = R"pb(
443
444 item_1: 1
445)pb";
446)test",
447 format(R"test(
448std::string s = R"pb(
449
450 item_1:1
451)pb";
452)test",
453 getRawStringPbStyleWithColumns(40)));
454
455 expect_eq(R"test(
456std::string s = R"pb(
457 item_1: 1
458)pb";
459)test",
460 format(R"test(
461std::string s = R"pb(
462 item_1:1
463
464)pb";
465)test",
466 getRawStringPbStyleWithColumns(40)));
467
468 expect_eq(R"test(
469std::string s = R"pb(
470 item_1: 1,
471 item_2: 2
472)pb";
473)test",
474 format(R"test(
475std::string s = R"pb(
476 item_1:1, item_2:2
477)pb";
478)test",
479 getRawStringPbStyleWithColumns(40)));
480
481 expect_eq(R"test(
482std::string s = R"pb(
483 book {
484 title: "Alice's Adventures"
485 author: "Lewis Caroll"
486 }
487 book {
488 title: "Peter Pan"
489 author: "J. M. Barrie"
490 }
491)pb";
492)test",
493 format(R"test(
494std::string s = R"pb(
495 book { title: "Alice's Adventures" author: "Lewis Caroll" }
496 book { title: "Peter Pan" author: "J. M. Barrie" }
497)pb";
498)test",
499 getRawStringPbStyleWithColumns(40)));
500}
501
502TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
503 expect_eq(R"test(
504ASSERT_TRUE(
505 ParseFromString(R"pb(item_1: 1)pb"),
506 ptr);)test",
507 format(R"test(
508ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
509 getRawStringPbStyleWithColumns(40)));
510
511 expect_eq(R"test(
512ASSERT_TRUE(toolong::ParseFromString(
513 R"pb(item_1: 1)pb"),
514 ptr);)test",
515 format(R"test(
516ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
517 getRawStringPbStyleWithColumns(40)));
518
519 expect_eq(R"test(
520ASSERT_TRUE(ParseFromString(
521 R"pb(item_1: 1,
522 item_2: 2)pb"),
523 ptr);)test",
524 format(R"test(
525ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
526 getRawStringPbStyleWithColumns(40)));
527
528 expect_eq(R"test(
529ASSERT_TRUE(
530 ParseFromString(
531 R"pb(item_1: 1 item_2: 2)pb"),
532 ptr);)test",
533 format(R"test(
534ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
535 getRawStringPbStyleWithColumns(40)));
536
537}
538
539TEST_F(FormatTestRawStrings, RawStringsInOperands) {
540 // Formats the raw string first operand of a binary operator expression.
541 expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test",
542 format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test",
543 getRawStringPbStyleWithColumns(40)));
544
545 expect_eq(R"test(
546auto S = R"pb(item_1: 1, item_2: 2)pb" +
547 rest;)test",
548 format(R"test(
549auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
550 getRawStringPbStyleWithColumns(40)));
551
552 expect_eq(R"test(
553auto S =
554 R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
555 format(R"test(
556auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test",
557 getRawStringPbStyleWithColumns(40)));
558
559 expect_eq(R"test(
560auto S = R"pb(item_1: 1,
561 item_2: 2,
562 item_3: 3)pb" + rest;)test",
563 format(R"test(
564auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
565 getRawStringPbStyleWithColumns(40)));
566
567 expect_eq(R"test(
568auto S = R"pb(item_1: 1,
569 item_2: 2,
570 item_3: 3)pb" +
571 longlongrest;)test",
572 format(R"test(
573auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
574 getRawStringPbStyleWithColumns(40)));
575
576 // Formats the raw string second operand of a binary operator expression.
577 expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test",
578 format(R"test(auto S = first + R"pb(item_1:1)pb";)test",
579 getRawStringPbStyleWithColumns(40)));
580
581 expect_eq(R"test(
582auto S = first + R"pb(item_1: 1,
583 item_2: 2)pb";)test",
584 format(R"test(
585auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
586 getRawStringPbStyleWithColumns(40)));
587
588 expect_eq(R"test(
589auto S = first + R"pb(item_1: 1
590 item_2: 2)pb";)test",
591 format(R"test(
592auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
593 getRawStringPbStyleWithColumns(40)));
594
595 expect_eq(R"test(
596auto S = R"pb(item_1: 1,
597 item_2: 2,
598 item_3: 3)pb" + rest;)test",
599 format(R"test(
600auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
601 getRawStringPbStyleWithColumns(40)));
602
603 expect_eq(R"test(
604auto S = R"pb(item_1: 1,
605 item_2: 2,
606 item_3: 3)pb" +
607 longlongrest;)test",
608 format(R"test(
609auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
610 getRawStringPbStyleWithColumns(40)));
611
612 // Formats the raw string operands in expressions.
613 expect_eq(R"test(
614auto S = R"pb(item_1: 1)pb" +
615 R"pb(item_2: 2)pb";
616)test",
617 format(R"test(
618auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
619)test",
620 getRawStringPbStyleWithColumns(40)));
621
622 expect_eq(R"test(
623auto S = R"pb(item_1: 1)pb" +
624 R"pb(item_2: 2)pb" +
625 R"pb(item_3: 3)pb";
626)test",
627 format(R"test(
628auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
629)test",
630 getRawStringPbStyleWithColumns(40)));
631
632 expect_eq(R"test(
633auto S = (count < 3)
634 ? R"pb(item_1: 1)pb"
635 : R"pb(item_2: 2)pb";
636)test",
637 format(R"test(
638auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb";
639)test",
640 getRawStringPbStyleWithColumns(40)));
641
642 expect_eq(R"test(
643auto S =
644 (count < 3)
645 ? R"pb(item_1: 1, item_2: 2)pb"
646 : R"pb(item_3: 3)pb";
647)test",
648 format(R"test(
649auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
650)test",
651 getRawStringPbStyleWithColumns(40)));
652
653 expect_eq(R"test(
654auto S =
655 (count < 3)
656 ? R"pb(item_1: 1)pb"
657 : R"pb(item_2: 2, item_3: 3)pb";
658)test",
659 format(R"test(
660auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
661)test",
662 getRawStringPbStyleWithColumns(40)));
663
664}
665
666TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
667 // Keep the suffix at the end of line if not on newline.
668 expect_eq(R"test(
669int s() {
670 auto S = PTP(
671 R"pb(
672 item_1: 1,
673 item_2: 2)pb");
674})test",
675 format(R"test(
676int s() {
677 auto S = PTP(
678 R"pb(
679 item_1: 1,
680 item_2: 2)pb");
681})test",
682 getRawStringPbStyleWithColumns(20)));
683
684 // Align the suffix with the surrounding FirstIndent if the prefix is not on
685 // a line of its own.
686 expect_eq(R"test(
687int s() {
688 auto S = PTP(
689 R"pb(
690 item_1: 1,
691 item_2: 2
692 )pb");
693})test",
694 format(R"test(
695int s() {
696 auto S = PTP(R"pb(
697 item_1: 1,
698 item_2: 2
699 )pb");
700})test",
701 getRawStringPbStyleWithColumns(20)));
702
703 // Align the prefix with the suffix if both the prefix and suffix are on a
704 // line of their own.
705 expect_eq(R"test(
706int s() {
707 auto S = PTP(
708 R"pb(
709 item_1: 1,
710 item_2: 2,
711 )pb");
712})test",
713 format(R"test(
714int s() {
715 auto S = PTP(
716 R"pb(
717 item_1: 1,
718 item_2: 2,
719 )pb");
720})test",
721 getRawStringPbStyleWithColumns(20)));
722}
723
724TEST_F(FormatTestRawStrings, EstimatesPenalty) {
725 // The penalty for characters exceeding the column limit in the raw string
726 // forces 'hh' to be put on a newline.
727 expect_eq(R"test(
728ff(gggggg,
729 hh(R"pb(key {
730 i1: k1
731 i2: k2
732 })pb"));
733)test",
734 format(R"test(
735ff(gggggg, hh(R"pb(key {
736 i1: k1
737 i2: k2
738 })pb"));
739)test",
740 getRawStringPbStyleWithColumns(20)));
741}
742
743TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) {
744 expect_eq(R"test(a = R"pb(key:value)";)test",
745 format(R"test(a = R"pb(key:value)";)test",
746 getRawStringPbStyleWithColumns(20)));
747}
748
Krasimir Georgiev2537e222018-01-17 16:17:26 +0000749TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
750 FormatStyle Style = getRawStringPbStyleWithColumns(40);
751 Style.RawStringFormats[0].EnclosingFunctions.push_back(
752 "PARSE_TEXT_PROTO");
753 Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
754 expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
755 format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
756
757 expect_eq(R"test(
758a = PARSE_TEXT_PROTO /**/ (
759 /**/ R"(key: value)");)test",
760 format(R"test(
761a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
762 Style));
763
764 expect_eq(R"test(
765a = ParseTextProto<ProtoType>(
766 R"(key: value)");)test",
767 format(R"test(
768a = ParseTextProto<ProtoType>(R"(key:value)");)test",
769 Style));
770}
771
Krasimir Georgiev412ed092018-01-19 16:18:47 +0000772TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
773 FormatStyle Style = getRawStringPbStyleWithColumns(25);
774 Style.RawStringFormats[0].CanonicalDelimiter = "proto";
775 expect_eq(R"test(a = R"proto(key: value)proto";)test",
776 format(R"test(a = R"pb(key:value)pb";)test", Style));
777
778 // Don't update to canonical delimiter if it occurs as a raw string suffix in
779 // the raw string content.
780 expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
781 format(R"test(a = R"pb(key:")proto")pb";)test", Style));
782}
783
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000784} // end namespace
785} // end namespace format
786} // end namespace clang