1 //===- unittest/Format/FormatTestTextProto.cpp ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test-text-proto"
18 class FormatTestTextProto
: public FormatTestBase
{
20 virtual FormatStyle
getDefaultStyle() const override
{
21 return getTextProtoStyleWithColumns(60);
25 TEST_F(FormatTestTextProto
, KeepsTopLevelEntriesFittingALine
) {
26 verifyFormat("field_a: OK field_b: OK field_c: OK field_d: OK field_e: OK");
29 TEST_F(FormatTestTextProto
, SupportsMessageFields
) {
30 verifyFormat("msg_field: {}");
32 verifyFormat("msg_field: { field_a: A }");
34 verifyFormat("msg_field: { field_a: \"OK\" field_b: 123 }");
36 verifyFormat("msg_field: {\n"
44 verifyFormat("msg_field {}");
46 verifyFormat("msg_field { field_a: A }");
48 verifyFormat("msg_field { field_a: \"OK\" field_b: 123 }");
50 verifyFormat("msg_field {\n"
58 " field_h: 1234.567e-89\n"
61 verifyFormat("msg_field: { msg_field { field_a: 1 } }");
63 verifyFormat("id: \"ala.bala\"\n"
64 "item { type: ITEM_A rank: 1 score: 90.0 }\n"
65 "item { type: ITEM_B rank: 2 score: 70.5 }\n"
70 " description: \"the third item has a description\"\n"
74 TEST_F(FormatTestTextProto
, AvoidsTopLevelBinPacking
) {
75 verifyFormat("field_a: OK\n"
82 verifyFormat("field_a: OK\n"
85 "msg_field: { field_d: 123 }\n"
89 verifyFormat("field_a: OK\n"
92 "msg_field: { field_d: 123 field_e: OK }");
96 " field_b { field_c: OK }\n"
101 verifyFormat("field_a: OK,\n"
102 "field_b { field_c: OK },\n"
107 TEST_F(FormatTestTextProto
, AddsNewlinesAfterTrailingComments
) {
108 verifyFormat("field_a: OK // Comment\n"
111 verifyFormat("field_a: OK\n"
113 " field_b: OK // Comment\n"
116 verifyFormat("field_a: OK\n"
118 " field_b: OK // Comment\n"
122 TEST_F(FormatTestTextProto
, ImplicitStringLiteralConcatenation
) {
123 verifyFormat("field_a: 'aaaaa'\n"
125 verifyFormat("field_a: \"aaaaa\"\n"
128 auto Style
= getDefaultStyle();
129 Style
.AlwaysBreakBeforeMultilineStrings
= true;
130 verifyFormat("field_a:\n"
134 verifyFormat("field_a:\n"
140 TEST_F(FormatTestTextProto
, SupportsAngleBracketMessageFields
) {
142 verifyFormat("msg_field <>");
143 verifyFormat("msg_field: <>");
144 verifyFormat("msg_field < field_a: OK >");
145 verifyFormat("msg_field: < field_a: 123 >");
146 verifyFormat("msg_field < field_a <> >");
147 verifyFormat("msg_field < field_a < field_b <> > >");
148 verifyFormat("msg_field: < field_a < field_b: <> > >");
149 verifyFormat("msg_field < field_a: OK, field_b: \"OK\" >");
150 verifyFormat("msg_field: < field_a: OK, field_b: \"OK\" >");
151 // Multiple lines tests
152 verifyFormat("msg_field <\n"
158 verifyFormat("msg_field <\n"
159 " field_a { field_b: 1 },\n"
160 " field_c: < f_d: 2 >\n"
163 verifyFormat("msg_field: <\n"
169 verifyFormat("msg_field: <\n"
170 " field_a { field_b: 1 },\n"
171 " field_c: < fd_d: 2 >\n"
174 verifyFormat("field_a: \"OK\",\n"
175 "msg_field: < field_b: 123 >,\n"
178 verifyFormat("field_a < field_b: 1 >,\n"
179 "msg_fid: < fiel_b: 123 >,\n"
182 verifyFormat("field_a < field_b: 1 >\n"
183 "msg_fied: < field_b: 123 >\n"
186 verifyFormat("field <\n"
187 " field < field: <> >,\n"
190 "field: < field: 1 >");
192 verifyFormat("msg_field <\n"
200 verifyFormat("msg_field: <>\n"
202 "msg_field: < field_d: 123 >\n"
204 "msg_field: < field_d: 12 >");
206 verifyFormat("field_a: OK,\n"
207 "field_b < field_c: OK >,\n"
208 "field_d: < 12.5 >,\n"
211 verifyFormat("field_a: OK\n"
212 "field_b < field_c: OK >\n"
213 "field_d: < 12.5 >\n"
216 verifyFormat("msg_field <\n"
218 " field_b < field_c: OK >,\n"
219 " field_d: < 12.5 >,\n"
223 verifyFormat("msg_field <\n"
224 " field_a: < field: OK >,\n"
225 " field_b < field_c: OK >,\n"
226 " field_d: < 12.5 >,\n"
230 verifyFormat("msg_field: <\n"
232 " msg_field: { field_b: OK }\n"
238 verifyFormat("field_a {\n"
240 " field_b: < field_c: 1 >\n"
245 verifyFormat("field_a: {\n"
247 " field_b: < field_c: 1 >\n"
252 verifyFormat("field_a: <\n"
260 " field_x {} // Comment\n"
261 " field_y: { field_z: 1 }\n"
265 " field_x <> // Comment\n"
266 " field_y: < field_z: 1 >\n"
270 " field: < field: 1 >\n"
271 " field: < field: 2 >\n"
272 " field: < field: 3 >\n"
273 " field: < field: 4 >\n"
279 "field_c < field < field <> > >");
281 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
283 "data < key: value >");
285 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
287 "data < key: value >\n"
290 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
292 "data < key: value >\n"
293 "data { key: value }");
295 verifyFormat("app {\n"
296 " app_id: 'com.javax.swing.salsa.latino'\n"
298 " data < key: value >\n"
301 verifyFormat("app: {\n"
302 " app_id: 'com.javax.swing.salsa.latino'\n"
304 " data < key: value >\n"
307 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
308 "headheadheadheadheadhead_id: 1\n"
309 "product_data { product { 1 } }");
311 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
312 "headheadheadheadheadhead_id: 1\n"
313 "product_data < product { 1 } >");
315 verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
316 "headheadheadheadheadhead_id: 1\n"
317 "product_data < product < 1 > >");
319 verifyFormat("app <\n"
320 " app_id: 'com.javax.swing.salsa.latino'\n"
321 " headheadheadheadheadhead_id: 1\n"
322 " product_data < product { 1 } >\n"
325 verifyFormat("dcccwrnfioeruvginerurneitinfo {\n"
326 " exte3nsionrnfvui { key: value }\n"
330 TEST_F(FormatTestTextProto
, DiscardsUnbreakableTailIfCanBreakAfter
) {
331 // The two closing braces count towards the string UnbreakableTailLength, but
332 // since we have broken after the corresponding opening braces, we don't
333 // consider that length for string breaking.
337 " text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
342 TEST_F(FormatTestTextProto
, KeepsLongStringLiteralsOnSameLine
) {
345 " text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaaaa\"\n"
349 TEST_F(FormatTestTextProto
, KeepsCommentsIndentedInList
) {
350 verifyFormat("aaaaaaaaaa: 100\n"
351 "bbbbbbbbbbbbbbbbbbbbbbbbbbb: 200\n"
352 "# Single line comment for stuff here.\n"
353 "cccccccccccccccccccccccc: 3849\n"
354 "# Multiline comment for stuff here.\n"
355 "# Multiline comment for stuff here.\n"
356 "# Multiline comment for stuff here.\n"
357 "cccccccccccccccccccccccc: 3849");
360 TEST_F(FormatTestTextProto
, UnderstandsHashComments
) {
361 auto Style
= getDefaultStyle();
363 verifyFormat("aaa: 100\n"
364 "## this is a double-hash comment.\n"
366 "## another double-hash comment.\n"
367 "### a triple-hash comment\n"
369 "### another triple-hash comment\n"
370 "#### a quadriple-hash comment\n"
372 "#### another quadriple-hash comment",
374 "##this is a double-hash comment.\n"
376 "## another double-hash comment.\n"
377 "###a triple-hash comment\n"
379 "### another triple-hash comment\n"
380 "####a quadriple-hash comment\n"
382 "#### another quadriple-hash comment",
385 // Ensure we support a common pattern for naming sections.
386 verifyFormat("##############\n"
391 verifyFormat("///////////////\n"
397 TEST_F(FormatTestTextProto
, FormatsExtensions
) {
398 verifyFormat("[type] { key: value }");
399 verifyFormat("[type] {\n"
400 " keyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: value\n"
402 verifyFormat("[type.type] { key: value }");
403 verifyFormat("[type.type] < key: value >");
404 verifyFormat("[type.type/type.type] { key: value }");
405 verifyFormat("msg {\n"
406 " [type.type] { key: value }\n"
408 verifyFormat("msg {\n"
410 " keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
413 verifyFormat("key: value\n"
414 "[a.b] { key: value }");
415 verifyFormat("msg: <\n"
417 " [a.b.c/d.e]: < key: value >\n"
419 " key: valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n"
423 " [h.i.j] < key: value >\n"
430 verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
431 " .longg.longlong] { key: value }");
432 verifyFormat("[longg.long.long.long.long.long.long.long.long.long.long\n"
433 " .longg.longlong] {\n"
439 verifyFormat("[longg.long.long.long.long.long.long.long.long.long\n"
440 " .long/longg.longlong] { key: value }");
441 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/\n"
442 " bbbbbbbbbbbbbb] { key: value }");
443 // These go over the column limit intentionally, since the alternative
444 // [aa..a\n] is worse.
446 "[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
450 "[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
452 " keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
455 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/\n"
458 " keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
462 "aaaaaaaaaaaaaaa {\n"
465 " eeeeeeeeeeeee: \"The lazy coo cat jumps over the lazy hot dog\"\n"
471 TEST_F(FormatTestTextProto
, SpacesAroundPercents
) {
472 verifyFormat("key: %d");
473 verifyFormat("key: 0x%04x");
474 verifyFormat("key: \"%d %d\"");
477 TEST_F(FormatTestTextProto
, FormatsRepeatedListInitializers
) {
478 verifyFormat("keys: []");
479 verifyFormat("keys: [ 1 ]");
480 verifyFormat("keys: [ 'ala', 'bala' ]");
481 verifyFormat("keys: [\n"
490 verifyFormat("key: item\n"
516 auto Style
= getDefaultStyle();
517 Style
.Cpp11BracedListStyle
= true;
518 verifyFormat("keys: [1]", Style
);
521 TEST_F(FormatTestTextProto
, AcceptsOperatorAsKey
) {
522 verifyFormat("aaaaaaaaaaa: <\n"
524 " ccccccccccccccccccccccc: <\n"
528 " operator { key: value }\n"
534 TEST_F(FormatTestTextProto
, BreaksConsecutiveStringLiterals
) {
535 verifyFormat("ala: \"str1\"\n"
539 TEST_F(FormatTestTextProto
, PutsMultipleEntriesInExtensionsOnNewlines
) {
540 verifyFormat("pppppppppp: {\n"
541 " ssssss: \"http://example.com/blahblahblah\"\n"
542 " ppppppp: \"sssss/MMMMMMMMMMMM\"\n"
543 " [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] { begin: 24 end: 252 }\n"
544 " [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] {\n"
551 getGoogleStyle(FormatStyle::LK_TextProto
));
554 TEST_F(FormatTestTextProto
, BreaksAfterBraceFollowedByClosingBraceOnNextLine
) {
555 verifyFormat("keys: [\n"
556 " data: { item: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }\n"
558 verifyFormat("keys: <\n"
559 " data: { item: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }\n"
563 TEST_F(FormatTestTextProto
, BreaksEntriesOfSubmessagesContainingSubmessages
) {
564 verifyFormat("key: valueeeeeeee\n"
566 " item: 'aaaaaaaaaaaaaaaa'\n"
569 verifyFormat("key: valueeeeeeee\n"
571 " item: 'aaaaaaaaaaaaaaaa'\n"
574 verifyFormat("key: valueeeeeeee\n"
580 verifyFormat("key: valueeeeeeee\n"
582 " item: 'aaaaaaaaaaa'\n"
585 verifyFormat("key: valueeeeeeee\n"
587 " item: 'aaaaaaaaaaa'\n"
590 verifyFormat("key: valueeeeeeee\n"
592 " item: 'aaaaaaaaaaa'\n"
595 verifyFormat("key: valueeeeeeee\n"
597 " item: 'aaaaaaaaaaa'\n"
600 verifyFormat("key: valueeeeeeee\n"
602 " item: 'aaaaaaaaaaa'\n"
605 verifyFormat("key: valueeeeeeee\n"
608 " item: 'aaaaaaaaaaaaaaaa'\n"
610 verifyFormat("key: valueeeeeeee\n"
613 " item: 'aaaaaaaaaaaaaaaa'\n"
615 verifyFormat("key: valueeeeeeee\n"
618 " item: 'aaaaaaaaaaaaaaaa'\n"
620 verifyFormat("key: valueeeeeeee\n"
622 " sub { key: value }\n"
623 " item: 'aaaaaaaaaaaaaaaa'\n"
625 verifyFormat("key: valueeeeeeee\n"
628 " item: 'aaaaaaaaaaaaaaaa'\n"
630 verifyFormat("key: valueeeeeeee\n"
632 " sub < sub_2: {} >\n"
633 " item: 'aaaaaaaaaaaaaaaa'\n"
635 verifyFormat("key: valueeeeeeee\n"
639 " item: 'aaaaaaaaaaaaaaaa'\n"
641 verifyFormat("key: valueeeeeeee\n"
644 " sub < sub_2: {} >\n"
645 " item: 'aaaaaaaaaaaaaaaa'\n"
647 verifyFormat("sub: {\n"
648 " key: valueeeeeeee\n"
651 " item: 'aaaaaaaaaaaaaaaa'\n"
654 verifyFormat("sub: {\n"
659 verifyFormat("sub: {\n"
666 TEST_F(FormatTestTextProto
, PreventBreaksBetweenKeyAndSubmessages
) {
667 verifyFormat("submessage: {\n"
668 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
670 verifyFormat("submessage {\n"
671 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
673 verifyFormat("submessage: <\n"
674 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
676 verifyFormat("submessage <\n"
677 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
679 verifyFormat("repeatedd: [\n"
680 " 'eyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
682 // "{" is going over the column limit.
684 "submessageeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee: {\n"
689 TEST_F(FormatTestTextProto
, FormatsCommentsAtEndOfFile
) {
690 verifyFormat("key: value\n"
691 "# endfile comment");
692 verifyFormat("key: value\n"
693 "// endfile comment");
694 verifyFormat("key: value\n"
695 "// endfile comment 1\n"
696 "// endfile comment 2");
697 verifyFormat("submessage { key: value }\n"
698 "# endfile comment");
699 verifyFormat("submessage <\n"
703 "# endfile comment");
706 TEST_F(FormatTestTextProto
, KeepsAmpersandsNextToKeys
) {
707 verifyFormat("@tmpl { field: 1 }");
708 verifyFormat("@placeholder: 1");
709 verifyFormat("@name <>");
710 verifyFormat("submessage: @base { key: value }");
711 verifyFormat("submessage: @base {\n"
715 verifyFormat("submessage: {\n"
726 } // namespace format
727 } // end namespace clang