blob: 307394341d74f905a463ace3d6fedfa4a339b8a2 [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) {
Nicola Zaghen3538b392018-05-15 13:30:56 +000036 LLVM_DEBUG(llvm::errs() << "---\n");
37 LLVM_DEBUG(llvm::errs() << Code << "\n\n");
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000038 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));
Nicola Zaghen3538b392018-05-15 13:30:56 +000050 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000051 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)));
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000202 // Merge two short lines into one.
203 expect_eq(R"test(
204std::string s = R"pb(
205 item_1: 1 item_2: 2
206)pb";
207)test",
208 format(R"test(
209std::string s = R"pb(
210 item_1:1
211 item_2:2
212)pb";
213)test",
214 getRawStringPbStyleWithColumns(40)));
215}
216
Krasimir Georgiev8b98f552018-06-11 12:53:25 +0000217TEST_F(FormatTestRawStrings, BreaksShortRawStringsWhenNeeded) {
218 // The raw string contains multiple submessage entries, so break for
219 // readability.
220 expect_eq(R"test(
221P p = TP(R"pb(item_1 < 1 >
222 item_2: { 2 })pb");)test",
223 format(
224 R"test(
225P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
226 getRawStringPbStyleWithColumns(40)));
227}
228
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000229TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
230 expect_eq(R"test(
231P p = TPPPPPPPPPPPPPPP(
232 R"pb(item_1: 1, item_2: 2)pb");)test",
233 format(R"test(
234P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
235 getRawStringPbStyleWithColumns(40)));
236
237 expect_eq(R"test(
238P p =
239 TPPPPPPPPPPPPPPP(
240 R"pb(item_1: 1,
241 item_2: 2,
242 item_3: 3)pb");)test",
243 format(R"test(
244P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
245 getRawStringPbStyleWithColumns(40)));
246
247 expect_eq(R"test(
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000248P p = TP(R"pb(item_1 < 1 >
249 item_2: < 2 >
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000250 item_3 {})pb");)test",
251 format(R"test(
252P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
253 getRawStringPbStyleWithColumns(40)));
254
255 expect_eq(
256 R"test(
257P 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(
263P 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(
267P p = TPPPPPPPPPPPPPPP(
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000268 R"pb(item_1 < 1 >,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000269 item_2: { 2 },
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000270 item_3: < 3 >,
Krasimir Georgievc2091802018-01-31 10:14:10 +0000271 item_4: { 4 })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000272 format(R"test(
273P 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(
278FFFFFFFFFFFFFFFFFFFFFFFFFFF(
279 R"pb(key: 1)pb");
280P p = TPPPPPPPPPPPPPPPPPPPP(
281 R"pb(key: 2)pb");
282auto TPPPPPPPPPPPPPPPPPPPP =
283 R"pb(key: 3)pb";
284P p = TPPPPPPPPPPPPPPPPPPPP(
285 R"pb(i: 1, j: 2)pb");
286
287int 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(
300FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
301P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
302auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
303P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
304
305int 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
316TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
317 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000318P p = TP(R"pb(key { 1 })pb", param_2);)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000319 format(R"test(
320P p = TP(R"pb(key{1})pb",param_2);)test",
321 getRawStringPbStyleWithColumns(40)));
322
323 expect_eq(R"test(
324PPPPPPPPPPPPP(R"pb(keykeyk)pb",
325 param_2);)test",
326 format(R"test(
327PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
328 getRawStringPbStyleWithColumns(40)));
329
330 expect_eq(R"test(
Krasimir Georgievc2091802018-01-31 10:14:10 +0000331P p = TP(
332 R"pb(item: { i: 1, s: 's' }
333 item: { i: 2, s: 't' })pb");)test",
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000334 format(R"test(
335P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
336 getRawStringPbStyleWithColumns(40)));
337 expect_eq(R"test(
338FFFFFFFFFFFFFFFFFFF(
339 R"pb(key: "value")pb",
340 R"pb(key2: "value")pb");)test",
341 format(R"test(
342FFFFFFFFFFFFFFFFFFF(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(
347FFFFFFFF(R"pb(key: 1)pb", argument2);
348struct 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(
359FFFFFFFF(R"pb(key:1)pb", argument2);
360struct S {
361const s = f(R"pb(key:1)pb", argument2);
362void 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(
372FFFFFFFF(argument1, R"pb(key: 2)pb");
373struct 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(
384FFFFFFFF(argument1, R"pb(key:2)pb");
385struct S {
386const s = f(argument1, R"pb(key:2)pb");
387void 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(
397FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
398 format(R"test(
399FFFFF(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(
408FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
409 format(R"test(
410FFFFFFF(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(
415FFFFFFFF(R"pb(key: 1)pb",
416 R"pb(key: 2)pb");)test",
417 format(R"test(
418FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
419 getRawStringPbStyleWithColumns(40)));
420
421 // Formats both arguments.
422 expect_eq(R"test(
423FFFFFFFF(R"pb(key: 1)pb",
424 R"pb(key: 2)pb");
425struct 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(
436FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
437struct S {
438const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
439void 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
448TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
449 expect_eq(R"test(
450std::string s = R"pb(
451 item_1: 1
452)pb";
453)test",
454 format(R"test(
455std::string s = R"pb(
456 item_1:1
457)pb";
458)test",
459 getRawStringPbStyleWithColumns(40)));
460
461 expect_eq(R"test(
462std::string s = R"pb(
463
464 item_1: 1
465)pb";
466)test",
467 format(R"test(
468std::string s = R"pb(
469
470 item_1:1
471)pb";
472)test",
473 getRawStringPbStyleWithColumns(40)));
474
475 expect_eq(R"test(
476std::string s = R"pb(
477 item_1: 1
478)pb";
479)test",
480 format(R"test(
481std::string s = R"pb(
482 item_1:1
483
484)pb";
485)test",
486 getRawStringPbStyleWithColumns(40)));
487
488 expect_eq(R"test(
489std::string s = R"pb(
490 item_1: 1,
491 item_2: 2
492)pb";
493)test",
494 format(R"test(
495std::string s = R"pb(
496 item_1:1, item_2:2
497)pb";
498)test",
499 getRawStringPbStyleWithColumns(40)));
500
501 expect_eq(R"test(
502std::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(
514std::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
522TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
523 expect_eq(R"test(
524ASSERT_TRUE(
525 ParseFromString(R"pb(item_1: 1)pb"),
526 ptr);)test",
527 format(R"test(
528ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
529 getRawStringPbStyleWithColumns(40)));
530
531 expect_eq(R"test(
532ASSERT_TRUE(toolong::ParseFromString(
533 R"pb(item_1: 1)pb"),
534 ptr);)test",
535 format(R"test(
536ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
537 getRawStringPbStyleWithColumns(40)));
538
539 expect_eq(R"test(
540ASSERT_TRUE(ParseFromString(
541 R"pb(item_1: 1,
542 item_2: 2)pb"),
543 ptr);)test",
544 format(R"test(
545ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
546 getRawStringPbStyleWithColumns(40)));
547
548 expect_eq(R"test(
549ASSERT_TRUE(
550 ParseFromString(
551 R"pb(item_1: 1 item_2: 2)pb"),
552 ptr);)test",
553 format(R"test(
554ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
555 getRawStringPbStyleWithColumns(40)));
556
557}
558
559TEST_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(
566auto S = R"pb(item_1: 1, item_2: 2)pb" +
567 rest;)test",
568 format(R"test(
569auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
570 getRawStringPbStyleWithColumns(40)));
571
572 expect_eq(R"test(
573auto S =
574 R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
575 format(R"test(
576auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test",
577 getRawStringPbStyleWithColumns(40)));
578
579 expect_eq(R"test(
580auto S = R"pb(item_1: 1,
581 item_2: 2,
582 item_3: 3)pb" + rest;)test",
583 format(R"test(
584auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
585 getRawStringPbStyleWithColumns(40)));
586
587 expect_eq(R"test(
588auto S = R"pb(item_1: 1,
589 item_2: 2,
590 item_3: 3)pb" +
591 longlongrest;)test",
592 format(R"test(
593auto 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(
602auto S = first + R"pb(item_1: 1,
603 item_2: 2)pb";)test",
604 format(R"test(
605auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
606 getRawStringPbStyleWithColumns(40)));
607
608 expect_eq(R"test(
609auto S = first + R"pb(item_1: 1
610 item_2: 2)pb";)test",
611 format(R"test(
612auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
613 getRawStringPbStyleWithColumns(40)));
614
615 expect_eq(R"test(
616auto S = R"pb(item_1: 1,
617 item_2: 2,
618 item_3: 3)pb" + rest;)test",
619 format(R"test(
620auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
621 getRawStringPbStyleWithColumns(40)));
622
623 expect_eq(R"test(
624auto S = R"pb(item_1: 1,
625 item_2: 2,
626 item_3: 3)pb" +
627 longlongrest;)test",
628 format(R"test(
629auto 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(
634auto S = R"pb(item_1: 1)pb" +
635 R"pb(item_2: 2)pb";
636)test",
637 format(R"test(
638auto S=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 = 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(
648auto 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(
653auto S = (count < 3)
654 ? R"pb(item_1: 1)pb"
655 : R"pb(item_2: 2)pb";
656)test",
657 format(R"test(
658auto 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(
663auto 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(
669auto 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(
674auto 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(
680auto 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
686TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
687 // Keep the suffix at the end of line if not on newline.
688 expect_eq(R"test(
689int s() {
690 auto S = PTP(
691 R"pb(
692 item_1: 1,
693 item_2: 2)pb");
694})test",
695 format(R"test(
696int s() {
697 auto S = PTP(
698 R"pb(
699 item_1: 1,
700 item_2: 2)pb");
701})test",
702 getRawStringPbStyleWithColumns(20)));
703
Krasimir Georgieva71f6262018-03-08 11:29:27 +0000704 // Align the suffix with the surrounding indent if the prefix is not on
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000705 // a line of its own.
706 expect_eq(R"test(
707int s() {
Krasimir Georgieva71f6262018-03-08 11:29:27 +0000708 auto S = PTP(R"pb(
709 item_1: 1,
710 item_2: 2
711 )pb");
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000712})test",
713 format(R"test(
714int 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(
725int s() {
726 auto S = PTP(
727 R"pb(
728 item_1: 1,
729 item_2: 2,
730 )pb");
731})test",
732 format(R"test(
733int 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
743TEST_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(
747ff(gggggg,
748 hh(R"pb(key {
749 i1: k1
750 i2: k2
751 })pb"));
752)test",
753 format(R"test(
754ff(gggggg, hh(R"pb(key {
755 i1: k1
756 i2: k2
757 })pb"));
758)test",
759 getRawStringPbStyleWithColumns(20)));
760}
761
762TEST_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
Krasimir Georgiev2537e222018-01-17 16:17:26 +0000768TEST_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(
777a = PARSE_TEXT_PROTO /**/ (
778 /**/ R"(key: value)");)test",
779 format(R"test(
780a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
781 Style));
782
783 expect_eq(R"test(
784a = ParseTextProto<ProtoType>(
785 R"(key: value)");)test",
786 format(R"test(
787a = ParseTextProto<ProtoType>(R"(key:value)");)test",
788 Style));
789}
790
Krasimir Georgiev412ed092018-01-19 16:18:47 +0000791TEST_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
Krasimir Georgievce009782018-03-16 14:01:25 +0000803TEST_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(
808xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
809 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
810)pb"));
811)test",
812 format(R"test(
813xxxxxxxaaaaax 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(
819xxxxxxxaaaaax wwwwwww =
820 _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
821 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
822 )pb"));
823)test",
824 format(R"test(
825xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
826 Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
827)pb"));
828)test", Style));
829}
830
Krasimir Georgiev8b4bc76a232018-05-09 09:02:11 +0000831TEST_F(FormatTestRawStrings, KeepsRBraceFolloedByMoreLBracesOnSameLine) {
832 FormatStyle Style = getRawStringPbStyleWithColumns(80);
833
834 expect_eq(
835 R"test(
836int 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(
851int 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
Krasimir Georgiev2d69f5d2018-06-28 16:58:24 +0000865TEST_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(
874void f() {
875 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
876item {
877 key: value
878}
879)Raw"));
880})test",
881 format(R"test(
882void f() {
883 aaaaaaaaa(bbbbbbbbb, EqualsProto(R"Raw(
884item {
885 key: value
886}
887)Raw"));
888})test",
889 Style));
890}
Krasimir Georgiev8b4bc76a232018-05-09 09:02:11 +0000891
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000892} // end namespace
893} // end namespace format
894} // end namespace clang