• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittest/Format/FormatTestTextProto.cpp ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FormatTestUtils.h"
10 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
13 
14 #define DEBUG_TYPE "format-test"
15 
16 namespace clang {
17 namespace format {
18 
19 class FormatTestTextProto : public ::testing::Test {
20 protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)21   static std::string format(llvm::StringRef Code, unsigned Offset,
22                             unsigned Length, const FormatStyle &Style) {
23     LLVM_DEBUG(llvm::errs() << "---\n");
24     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
25     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
26     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
27     auto Result = applyAllReplacements(Code, Replaces);
28     EXPECT_TRUE(static_cast<bool>(Result));
29     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
30     return *Result;
31   }
32 
format(llvm::StringRef Code,const FormatStyle & Style)33   static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
34     return format(Code, 0, Code.size(), Style);
35   }
36 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style)37   static void verifyFormat(llvm::StringRef Code, const FormatStyle &Style) {
38     EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
39     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
40   }
41 
verifyFormat(llvm::StringRef Code)42   static void verifyFormat(llvm::StringRef Code) {
43     FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
44     Style.ColumnLimit = 60; // To make writing tests easier.
45     verifyFormat(Code, Style);
46   }
47 };
48 
TEST_F(FormatTestTextProto,KeepsTopLevelEntriesFittingALine)49 TEST_F(FormatTestTextProto, KeepsTopLevelEntriesFittingALine) {
50   verifyFormat("field_a: OK field_b: OK field_c: OK field_d: OK field_e: OK");
51 }
52 
TEST_F(FormatTestTextProto,SupportsMessageFields)53 TEST_F(FormatTestTextProto, SupportsMessageFields) {
54   verifyFormat("msg_field: {}");
55 
56   verifyFormat("msg_field: { field_a: A }");
57 
58   verifyFormat("msg_field: { field_a: \"OK\" field_b: 123 }");
59 
60   verifyFormat("msg_field: {\n"
61                "  field_a: 1\n"
62                "  field_b: OK\n"
63                "  field_c: \"OK\"\n"
64                "  field_d: 123\n"
65                "  field_e: 23\n"
66                "}");
67 
68   verifyFormat("msg_field {}");
69 
70   verifyFormat("msg_field { field_a: A }");
71 
72   verifyFormat("msg_field { field_a: \"OK\" field_b: 123 }");
73 
74   verifyFormat("msg_field {\n"
75                "  field_a: 1\n"
76                "  field_b: OK\n"
77                "  field_c: \"OK\"\n"
78                "  field_d: 123\n"
79                "  field_e: 23.0\n"
80                "  field_f: false\n"
81                "  field_g: 'lala'\n"
82                "  field_h: 1234.567e-89\n"
83                "}");
84 
85   verifyFormat("msg_field: { msg_field { field_a: 1 } }");
86 
87   verifyFormat("id: \"ala.bala\"\n"
88                "item { type: ITEM_A rank: 1 score: 90.0 }\n"
89                "item { type: ITEM_B rank: 2 score: 70.5 }\n"
90                "item {\n"
91                "  type: ITEM_A\n"
92                "  rank: 3\n"
93                "  score: 20.0\n"
94                "  description: \"the third item has a description\"\n"
95                "}");
96 }
97 
TEST_F(FormatTestTextProto,AvoidsTopLevelBinPacking)98 TEST_F(FormatTestTextProto, AvoidsTopLevelBinPacking) {
99   verifyFormat("field_a: OK\n"
100                "field_b: OK\n"
101                "field_c: OK\n"
102                "field_d: OK\n"
103                "field_e: OK\n"
104                "field_f: OK");
105 
106   verifyFormat("field_a: OK\n"
107                "field_b: \"OK\"\n"
108                "field_c: \"OK\"\n"
109                "msg_field: { field_d: 123 }\n"
110                "field_e: OK\n"
111                "field_f: OK");
112 
113   verifyFormat("field_a: OK\n"
114                "field_b: \"OK\"\n"
115                "field_c: \"OK\"\n"
116                "msg_field: { field_d: 123 field_e: OK }");
117 
118   verifyFormat("a: {\n"
119                "  field_a: OK\n"
120                "  field_b { field_c: OK }\n"
121                "  field_d: OKOKOK\n"
122                "  field_e: OK\n"
123                "}");
124 
125   verifyFormat("field_a: OK,\n"
126                "field_b { field_c: OK },\n"
127                "field_d: OKOKOK,\n"
128                "field_e: OK");
129 }
130 
TEST_F(FormatTestTextProto,AddsNewlinesAfterTrailingComments)131 TEST_F(FormatTestTextProto, AddsNewlinesAfterTrailingComments) {
132   verifyFormat("field_a: OK  // Comment\n"
133                "field_b: 1");
134 
135   verifyFormat("field_a: OK\n"
136                "msg_field: {\n"
137                "  field_b: OK  // Comment\n"
138                "}");
139 
140   verifyFormat("field_a: OK\n"
141                "msg_field {\n"
142                "  field_b: OK  // Comment\n"
143                "}");
144 }
145 
TEST_F(FormatTestTextProto,ImplicitStringLiteralConcatenation)146 TEST_F(FormatTestTextProto, ImplicitStringLiteralConcatenation) {
147   verifyFormat("field_a: 'aaaaa'\n"
148                "         'bbbbb'");
149   verifyFormat("field_a: \"aaaaa\"\n"
150                "         \"bbbbb\"");
151   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
152   Style.AlwaysBreakBeforeMultilineStrings = true;
153   verifyFormat("field_a:\n"
154                "    'aaaaa'\n"
155                "    'bbbbb'",
156                Style);
157   verifyFormat("field_a:\n"
158                "    \"aaaaa\"\n"
159                "    \"bbbbb\"",
160                Style);
161 }
162 
TEST_F(FormatTestTextProto,SupportsAngleBracketMessageFields)163 TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) {
164   // Single-line tests
165   verifyFormat("msg_field <>");
166   verifyFormat("msg_field: <>");
167   verifyFormat("msg_field < field_a: OK >");
168   verifyFormat("msg_field: < field_a: 123 >");
169   verifyFormat("msg_field < field_a <> >");
170   verifyFormat("msg_field < field_a < field_b <> > >");
171   verifyFormat("msg_field: < field_a < field_b: <> > >");
172   verifyFormat("msg_field < field_a: OK, field_b: \"OK\" >");
173   verifyFormat("msg_field: < field_a: OK, field_b: \"OK\" >");
174   // Multiple lines tests
175   verifyFormat("msg_field <\n"
176                "  field_a: OK\n"
177                "  field_b: <>,\n"
178                "  field_c: OK\n"
179                ">");
180 
181   verifyFormat("msg_field <\n"
182                "  field_a { field_b: 1 },\n"
183                "  field_c: < f_d: 2 >\n"
184                ">");
185 
186   verifyFormat("msg_field: <\n"
187                "  field_a: OK\n"
188                "  field_b: <>,\n"
189                "  field_c: OK\n"
190                ">");
191 
192   verifyFormat("msg_field: <\n"
193                "  field_a { field_b: 1 },\n"
194                "  field_c: < fd_d: 2 >\n"
195                ">");
196 
197   verifyFormat("field_a: \"OK\",\n"
198                "msg_field: < field_b: 123 >,\n"
199                "field_c: {}");
200 
201   verifyFormat("field_a < field_b: 1 >,\n"
202                "msg_fid: < fiel_b: 123 >,\n"
203                "field_c <>");
204 
205   verifyFormat("field_a < field_b: 1 >\n"
206                "msg_fied: < field_b: 123 >\n"
207                "field_c <>");
208 
209   verifyFormat("field <\n"
210                "  field < field: <> >,\n"
211                "  field <>\n"
212                ">\n"
213                "field: < field: 1 >");
214 
215   verifyFormat("msg_field <\n"
216                "  field_a: OK\n"
217                "  field_b: \"OK\"\n"
218                "  field_c: 1\n"
219                "  field_d: 12.5\n"
220                "  field_e: OK\n"
221                ">");
222 
223   verifyFormat("msg_field: <>\n"
224                "field_c: \"OK\",\n"
225                "msg_field: < field_d: 123 >\n"
226                "field_e: OK\n"
227                "msg_field: < field_d: 12 >");
228 
229   verifyFormat("field_a: OK,\n"
230                "field_b < field_c: OK >,\n"
231                "field_d: < 12.5 >,\n"
232                "field_e: OK");
233 
234   verifyFormat("field_a: OK\n"
235                "field_b < field_c: OK >\n"
236                "field_d: < 12.5 >\n"
237                "field_e: OKOKOK");
238 
239   verifyFormat("msg_field <\n"
240                "  field_a: OK,\n"
241                "  field_b < field_c: OK >,\n"
242                "  field_d: < 12.5 >,\n"
243                "  field_e: OK\n"
244                ">");
245 
246   verifyFormat("msg_field <\n"
247                "  field_a: < field: OK >,\n"
248                "  field_b < field_c: OK >,\n"
249                "  field_d: < 12.5 >,\n"
250                "  field_e: OK,\n"
251                ">");
252 
253   verifyFormat("msg_field: <\n"
254                "  field_a: \"OK\"\n"
255                "  msg_field: { field_b: OK }\n"
256                "  field_g: OK\n"
257                "  field_g: OK\n"
258                "  field_g: OK\n"
259                ">");
260 
261   verifyFormat("field_a {\n"
262                "  field_d: ok\n"
263                "  field_b: < field_c: 1 >\n"
264                "  field_d: ok\n"
265                "  field_d: ok\n"
266                "}");
267 
268   verifyFormat("field_a: {\n"
269                "  field_d: ok\n"
270                "  field_b: < field_c: 1 >\n"
271                "  field_d: ok\n"
272                "  field_d: ok\n"
273                "}");
274 
275   verifyFormat("field_a: <\n"
276                "  f1: 1,\n"
277                "  f2: <>\n"
278                ">\n"
279                "field_b <\n"
280                "  field_b1: <>\n"
281                "  field_b2: ok,\n"
282                "  field_b3: <\n"
283                "    field_x {}  // Comment\n"
284                "    field_y: { field_z: 1 }\n"
285                "    field_w: ok\n"
286                "  >\n"
287                "  field {\n"
288                "    field_x <>  // Comment\n"
289                "    field_y: < field_z: 1 >\n"
290                "    field_w: ok\n"
291                "    msg_field: <\n"
292                "      field: <>\n"
293                "      field: < field: 1 >\n"
294                "      field: < field: 2 >\n"
295                "      field: < field: 3 >\n"
296                "      field: < field: 4 >\n"
297                "      field: ok\n"
298                "    >\n"
299                "  }\n"
300                ">\n"
301                "field: OK,\n"
302                "field_c < field < field <> > >");
303 
304   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
305                "head_id: 1\n"
306                "data < key: value >");
307 
308   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
309                "head_id: 1\n"
310                "data < key: value >\n"
311                "tail_id: 2");
312 
313   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
314                "head_id: 1\n"
315                "data < key: value >\n"
316                "data { key: value }");
317 
318   verifyFormat("app {\n"
319                "  app_id: 'com.javax.swing.salsa.latino'\n"
320                "  head_id: 1\n"
321                "  data < key: value >\n"
322                "}");
323 
324   verifyFormat("app: {\n"
325                "  app_id: 'com.javax.swing.salsa.latino'\n"
326                "  head_id: 1\n"
327                "  data < key: value >\n"
328                "}");
329 
330   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
331                "headheadheadheadheadhead_id: 1\n"
332                "product_data { product { 1 } }");
333 
334   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
335                "headheadheadheadheadhead_id: 1\n"
336                "product_data < product { 1 } >");
337 
338   verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
339                "headheadheadheadheadhead_id: 1\n"
340                "product_data < product < 1 > >");
341 
342   verifyFormat("app <\n"
343                "  app_id: 'com.javax.swing.salsa.latino'\n"
344                "  headheadheadheadheadhead_id: 1\n"
345                "  product_data < product { 1 } >\n"
346                ">");
347 
348   verifyFormat("dcccwrnfioeruvginerurneitinfo {\n"
349                "  exte3nsionrnfvui { key: value }\n"
350                "}");
351 }
352 
TEST_F(FormatTestTextProto,DiscardsUnbreakableTailIfCanBreakAfter)353 TEST_F(FormatTestTextProto, DiscardsUnbreakableTailIfCanBreakAfter) {
354   // The two closing braces count towards the string UnbreakableTailLength, but
355   // since we have broken after the corresponding opening braces, we don't
356   // consider that length for string breaking.
357   verifyFormat(
358       "foo: {\n"
359       "  bar: {\n"
360       "    text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
361       "  }\n"
362       "}");
363 }
364 
TEST_F(FormatTestTextProto,KeepsLongStringLiteralsOnSameLine)365 TEST_F(FormatTestTextProto, KeepsLongStringLiteralsOnSameLine) {
366   verifyFormat(
367       "foo: {\n"
368       "  text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaaaa\"\n"
369       "}");
370 }
371 
TEST_F(FormatTestTextProto,KeepsCommentsIndentedInList)372 TEST_F(FormatTestTextProto, KeepsCommentsIndentedInList) {
373   verifyFormat("aaaaaaaaaa: 100\n"
374                "bbbbbbbbbbbbbbbbbbbbbbbbbbb: 200\n"
375                "# Single line comment for stuff here.\n"
376                "cccccccccccccccccccccccc: 3849\n"
377                "# Multiline comment for stuff here.\n"
378                "# Multiline comment for stuff here.\n"
379                "# Multiline comment for stuff here.\n"
380                "cccccccccccccccccccccccc: 3849");
381 }
382 
TEST_F(FormatTestTextProto,UnderstandsHashHashComments)383 TEST_F(FormatTestTextProto, UnderstandsHashHashComments) {
384   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
385   Style.ColumnLimit = 60; // To make writing tests easier.
386   EXPECT_EQ("aaa: 100\n"
387             "##this is a double-hash comment.\n"
388             "bb: 100\n"
389             "## another double-hash comment.\n"
390             "### a triple-hash comment\n"
391             "cc: 200\n"
392             "#### a quadriple-hash comment\n"
393             "dd: 100\n",
394             format("aaa: 100\n"
395                    "##this is a double-hash comment.\n"
396                    "bb: 100\n"
397                    "## another double-hash comment.\n"
398                    "### a triple-hash comment\n"
399                    "cc: 200\n"
400                    "#### a quadriple-hash comment\n"
401                    "dd: 100\n",
402                    Style));
403 }
404 
TEST_F(FormatTestTextProto,FormatsExtensions)405 TEST_F(FormatTestTextProto, FormatsExtensions) {
406   verifyFormat("[type] { key: value }");
407   verifyFormat("[type] {\n"
408                "  keyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: value\n"
409                "}");
410   verifyFormat("[type.type] { key: value }");
411   verifyFormat("[type.type] < key: value >");
412   verifyFormat("[type.type/type.type] { key: value }");
413   verifyFormat("msg {\n"
414                "  [type.type] { key: value }\n"
415                "}");
416   verifyFormat("msg {\n"
417                "  [type.type] {\n"
418                "    keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
419                "  }\n"
420                "}");
421   verifyFormat("key: value\n"
422                "[a.b] { key: value }");
423   verifyFormat("msg: <\n"
424                "  key: value\n"
425                "  [a.b.c/d.e]: < key: value >\n"
426                "  [f.g]: <\n"
427                "    key: valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n"
428                "    key: {}\n"
429                "  >\n"
430                "  key {}\n"
431                "  [h.i.j] < key: value >\n"
432                "  [a]: {\n"
433                "    [b.c]: {}\n"
434                "    [d] <>\n"
435                "    [e/f]: 1\n"
436                "  }\n"
437                ">");
438   verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
439                "     .longg.longlong] { key: value }");
440   verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
441                "     .longg.longlong] {\n"
442                "  key: value\n"
443                "  key: value\n"
444                "  key: value\n"
445                "  key: value\n"
446                "}");
447   verifyFormat("[longg.long.long.long.long.long.long.long.long.long\n"
448                "     .long/longg.longlong] { key: value }");
449   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/\n"
450                " bbbbbbbbbbbbbb] { key: value }");
451   // These go over the column limit intentionally, since the alternative
452   // [aa..a\n] is worse.
453   verifyFormat(
454       "[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
455       "  key: value\n"
456       "}");
457   verifyFormat(
458       "[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
459       "  [type.type] {\n"
460       "    keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
461       "  }\n"
462       "}");
463   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/\n"
464                " bbbbbbb] {\n"
465                "  [type.type] {\n"
466                "    keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
467                "  }\n"
468                "}");
469   verifyFormat(
470       "aaaaaaaaaaaaaaa {\n"
471       "  bbbbbb {\n"
472       "    [a.b/cy] {\n"
473       "      eeeeeeeeeeeee: \"The lazy coo cat jumps over the lazy hot dog\"\n"
474       "    }\n"
475       "  }\n"
476       "}");
477 }
478 
TEST_F(FormatTestTextProto,SpacesAroundPercents)479 TEST_F(FormatTestTextProto, SpacesAroundPercents) {
480   verifyFormat("key: %d");
481   verifyFormat("key: 0x%04x");
482   verifyFormat("key: \"%d %d\"");
483 }
484 
TEST_F(FormatTestTextProto,FormatsRepeatedListInitializers)485 TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
486   verifyFormat("keys: []");
487   verifyFormat("keys: [ 1 ]");
488   verifyFormat("keys: [ 'ala', 'bala' ]");
489   verifyFormat("keys: [\n"
490                "  'ala',\n"
491                "  'bala',\n"
492                "  'porto',\n"
493                "  'kala',\n"
494                "  'too',\n"
495                "  'long',\n"
496                "  'ng'\n"
497                "]");
498   verifyFormat("key: item\n"
499                "keys: [\n"
500                "  'ala',\n"
501                "  'bala',\n"
502                "  'porto',\n"
503                "  'kala',\n"
504                "  'too',\n"
505                "  'long',\n"
506                "  'long',\n"
507                "  'long'\n"
508                "]\n"
509                "key: item\n"
510                "msg {\n"
511                "  key: item\n"
512                "  keys: [\n"
513                "    'ala',\n"
514                "    'bala',\n"
515                "    'porto',\n"
516                "    'kala',\n"
517                "    'too',\n"
518                "    'long',\n"
519                "    'long'\n"
520                "  ]\n"
521                "}\n"
522                "key: value");
523   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
524   Style.ColumnLimit = 60; // To make writing tests easier.
525   Style.Cpp11BracedListStyle = true;
526   verifyFormat("keys: [1]", Style);
527 }
528 
TEST_F(FormatTestTextProto,AcceptsOperatorAsKey)529 TEST_F(FormatTestTextProto, AcceptsOperatorAsKey) {
530   verifyFormat("aaaaaaaaaaa: <\n"
531                "  bbbbbbbbb: <\n"
532                "    ccccccccccccccccccccccc: <\n"
533                "      operator: 1\n"
534                "      operator: 2\n"
535                "      operator: 3\n"
536                "      operator { key: value }\n"
537                "    >\n"
538                "  >\n"
539                ">");
540 }
541 
TEST_F(FormatTestTextProto,BreaksConsecutiveStringLiterals)542 TEST_F(FormatTestTextProto, BreaksConsecutiveStringLiterals) {
543   verifyFormat("ala: \"str1\"\n"
544                "     \"str2\"\n");
545 }
546 
TEST_F(FormatTestTextProto,PutsMultipleEntriesInExtensionsOnNewlines)547 TEST_F(FormatTestTextProto, PutsMultipleEntriesInExtensionsOnNewlines) {
548   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
549   verifyFormat("pppppppppp: {\n"
550                "  ssssss: \"http://example.com/blahblahblah\"\n"
551                "  ppppppp: \"sssss/MMMMMMMMMMMM\"\n"
552                "  [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] { begin: 24 end: 252 }\n"
553                "  [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] {\n"
554                "    begin: 24\n"
555                "    end: 252\n"
556                "    key: value\n"
557                "    key: value\n"
558                "  }\n"
559                "}",
560                Style);
561 }
562 
TEST_F(FormatTestTextProto,BreaksAfterBraceFollowedByClosingBraceOnNextLine)563 TEST_F(FormatTestTextProto, BreaksAfterBraceFollowedByClosingBraceOnNextLine) {
564   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
565   Style.ColumnLimit = 60;
566   verifyFormat("keys: [\n"
567                "  data: { item: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }\n"
568                "]");
569   verifyFormat("keys: <\n"
570                "  data: { item: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }\n"
571                ">");
572 }
573 
TEST_F(FormatTestTextProto,BreaksEntriesOfSubmessagesContainingSubmessages)574 TEST_F(FormatTestTextProto, BreaksEntriesOfSubmessagesContainingSubmessages) {
575   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
576   Style.ColumnLimit = 60;
577   // The column limit allows for the keys submessage to be put on 1 line, but we
578   // break it since it contains a submessage an another entry.
579   verifyFormat("key: valueeeeeeee\n"
580                "keys: {\n"
581                "  item: 'aaaaaaaaaaaaaaaa'\n"
582                "  sub <>\n"
583                "}");
584   verifyFormat("key: valueeeeeeee\n"
585                "keys: {\n"
586                "  item: 'aaaaaaaaaaaaaaaa'\n"
587                "  sub {}\n"
588                "}");
589   verifyFormat("key: valueeeeeeee\n"
590                "keys: {\n"
591                "  sub {}\n"
592                "  sub: <>\n"
593                "  sub: []\n"
594                "}");
595   verifyFormat("key: valueeeeeeee\n"
596                "keys: {\n"
597                "  item: 'aaaaaaaaaaa'\n"
598                "  sub { msg: 1 }\n"
599                "}");
600   verifyFormat("key: valueeeeeeee\n"
601                "keys: {\n"
602                "  item: 'aaaaaaaaaaa'\n"
603                "  sub: { msg: 1 }\n"
604                "}");
605   verifyFormat("key: valueeeeeeee\n"
606                "keys: {\n"
607                "  item: 'aaaaaaaaaaa'\n"
608                "  sub < msg: 1 >\n"
609                "}");
610   verifyFormat("key: valueeeeeeee\n"
611                "keys: {\n"
612                "  item: 'aaaaaaaaaaa'\n"
613                "  sub: [ msg: 1 ]\n"
614                "}");
615   verifyFormat("key: valueeeeeeee\n"
616                "keys: <\n"
617                "  item: 'aaaaaaaaaaa'\n"
618                "  sub: [ 1, 2 ]\n"
619                ">");
620   verifyFormat("key: valueeeeeeee\n"
621                "keys: {\n"
622                "  sub {}\n"
623                "  item: 'aaaaaaaaaaaaaaaa'\n"
624                "}");
625   verifyFormat("key: valueeeeeeee\n"
626                "keys: {\n"
627                "  sub: []\n"
628                "  item: 'aaaaaaaaaaaaaaaa'\n"
629                "}");
630   verifyFormat("key: valueeeeeeee\n"
631                "keys: {\n"
632                "  sub <>\n"
633                "  item: 'aaaaaaaaaaaaaaaa'\n"
634                "}");
635   verifyFormat("key: valueeeeeeee\n"
636                "keys: {\n"
637                "  sub { key: value }\n"
638                "  item: 'aaaaaaaaaaaaaaaa'\n"
639                "}");
640   verifyFormat("key: valueeeeeeee\n"
641                "keys: {\n"
642                "  sub: [ 1, 2 ]\n"
643                "  item: 'aaaaaaaaaaaaaaaa'\n"
644                "}");
645   verifyFormat("key: valueeeeeeee\n"
646                "keys: {\n"
647                "  sub < sub_2: {} >\n"
648                "  item: 'aaaaaaaaaaaaaaaa'\n"
649                "}");
650   verifyFormat("key: valueeeeeeee\n"
651                "keys: {\n"
652                "  item: data\n"
653                "  sub: [ 1, 2 ]\n"
654                "  item: 'aaaaaaaaaaaaaaaa'\n"
655                "}");
656   verifyFormat("key: valueeeeeeee\n"
657                "keys: {\n"
658                "  item: data\n"
659                "  sub < sub_2: {} >\n"
660                "  item: 'aaaaaaaaaaaaaaaa'\n"
661                "}");
662   verifyFormat("sub: {\n"
663                "  key: valueeeeeeee\n"
664                "  keys: {\n"
665                "    sub: [ 1, 2 ]\n"
666                "    item: 'aaaaaaaaaaaaaaaa'\n"
667                "  }\n"
668                "}");
669   verifyFormat("sub: {\n"
670                "  key: 1\n"
671                "  sub: {}\n"
672                "}\n"
673                "# comment\n");
674   verifyFormat("sub: {\n"
675                "  key: 1\n"
676                "  # comment\n"
677                "  sub: {}\n"
678                "}");
679 }
680 
TEST_F(FormatTestTextProto,PreventBreaksBetweenKeyAndSubmessages)681 TEST_F(FormatTestTextProto, PreventBreaksBetweenKeyAndSubmessages) {
682   verifyFormat("submessage: {\n"
683                "  key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
684                "}");
685   verifyFormat("submessage {\n"
686                "  key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
687                "}");
688   verifyFormat("submessage: <\n"
689                "  key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
690                ">");
691   verifyFormat("submessage <\n"
692                "  key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
693                ">");
694   verifyFormat("repeatedd: [\n"
695                "  'eyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
696                "]");
697   // "{" is going over the column limit.
698   verifyFormat(
699       "submessageeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee: {\n"
700       "  key: 'aaaaa'\n"
701       "}");
702 }
703 
TEST_F(FormatTestTextProto,FormatsCommentsAtEndOfFile)704 TEST_F(FormatTestTextProto, FormatsCommentsAtEndOfFile) {
705   verifyFormat("key: value\n"
706                "# endfile comment");
707   verifyFormat("key: value\n"
708                "// endfile comment");
709   verifyFormat("key: value\n"
710                "// endfile comment 1\n"
711                "// endfile comment 2");
712   verifyFormat("submessage { key: value }\n"
713                "# endfile comment");
714   verifyFormat("submessage <\n"
715                "  key: value\n"
716                "  item {}\n"
717                ">\n"
718                "# endfile comment");
719 }
720 
TEST_F(FormatTestTextProto,KeepsAmpersandsNextToKeys)721 TEST_F(FormatTestTextProto, KeepsAmpersandsNextToKeys) {
722   verifyFormat("@tmpl { field: 1 }");
723   verifyFormat("@placeholder: 1");
724   verifyFormat("@name <>");
725   verifyFormat("submessage: @base { key: value }");
726   verifyFormat("submessage: @base {\n"
727                "  key: value\n"
728                "  item: {}\n"
729                "}");
730   verifyFormat("submessage: {\n"
731                "  msg: @base {\n"
732                "    yolo: {}\n"
733                "    key: value\n"
734                "  }\n"
735                "  key: value\n"
736                "}");
737 }
738 
739 } // namespace format
740 } // end namespace clang
741