1 //===- unittest/Format/FormatTestProto.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 "FormatTestUtils.h"
10 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
14 #define DEBUG_TYPE "format-test"
19 class FormatTestProto
: public ::testing::Test
{
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");
33 static std::string
format(llvm::StringRef Code
) {
34 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Proto
);
35 Style
.ColumnLimit
= 60; // To make writing tests easier.
36 return format(Code
, 0, Code
.size(), Style
);
39 static void verifyFormat(llvm::StringRef Code
) {
40 EXPECT_EQ(Code
.str(), format(Code
)) << "Expected code is not stable";
41 EXPECT_EQ(Code
.str(), format(test::messUp(Code
)));
45 TEST_F(FormatTestProto
, FormatsMessages
) {
46 verifyFormat("message SomeMessage {\n"
47 " required int32 field1 = 1;\n"
49 verifyFormat("message SomeMessage {\n"
50 " required .absolute.Reference field1 = 1;\n"
52 verifyFormat("message SomeMessage {\n"
53 " required int32 field1 = 1;\n"
54 " optional string field2 = 2 [default = \"2\"]\n"
57 verifyFormat("message SomeMessage {\n"
58 " optional really.really.long.qualified.type.aaa.aaaaaaa\n"
59 " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
61 " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
62 " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
64 verifyFormat("message SomeMessage {\n"
65 " map<string, Project> projects = 1;\n"
66 " optional map<string, int32> size_projects = 2;\n"
67 " map<int, really.really.really.long.qualified.type.nameeee>\n"
69 " map<int, really.really.really.really.long.qualified.type\n"
70 " .nameeee> projects = 4;\n"
72 " reallyreallyreallyreallyreallyreallyreallylongname>\n"
74 " map<int, Project>\n"
75 " longlonglonglonglonglonglonglonglonglongonglon = 6;\n"
76 " map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
77 " int> projects = 7;\n"
78 " map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
79 " releleallyreallyreallyreallyreallyreallyreallylongname>\n"
80 " releleallyreallyreallyreallyreallyreallyreallylongnam =\n"
82 " map<relele.llyreal.yreallyr.allyreally.eallyreal\n"
83 " .sauenirylongname,\n"
84 " really.really.really.really.long.qualified.type\n"
85 " .nameeee> projects = 9;\n"
89 TEST_F(FormatTestProto
, KeywordsInOtherLanguages
) {
90 verifyFormat("optional string operator = 1;");
93 TEST_F(FormatTestProto
, FormatsEnums
) {
94 verifyFormat("enum Type {\n"
99 verifyFormat("enum Type {\n"
100 " UNKNOWN = 0 [(some_options) = { a: aa, b: bb }];\n"
102 verifyFormat("enum Type {\n"
103 " UNKNOWN = 0 [(some_options) = {\n"
110 TEST_F(FormatTestProto
, EnumAsFieldName
) {
111 verifyFormat("message SomeMessage {\n"
112 " required int32 enum = 1;\n"
116 TEST_F(FormatTestProto
, CaseAsFieldName
) {
117 verifyFormat("message SomeMessage {\n"
118 " required string case = 1;\n"
119 " repeated int32 fizz = 2;\n"
123 TEST_F(FormatTestProto
, UnderstandsReturns
) {
124 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
127 TEST_F(FormatTestProto
, MessageFieldAttributes
) {
128 verifyFormat("optional string test = 1 [default = \"test\"];");
129 verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
130 verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
131 " default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
132 " deprecated = true\n"
134 verifyFormat("optional LongMessageType long_proto_field = 1\n"
135 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
136 verifyFormat("repeated double value = 1\n"
137 " [(aaaaaaa.aaaaaaaaa) = { aaaaaaaaaaaaaaaaa: AAAAAAAA }];");
138 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
139 " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
140 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
142 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
143 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
144 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
146 verifyFormat("repeated double value = 1 [\n"
147 " (aaaaaaa.aaaaaaaaa) = {\n"
148 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
149 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
151 " (bbbbbbb.bbbbbbbbb) = {\n"
152 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
153 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
156 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
157 " type: \"AAAAAAAAAA\"\n"
158 " is: \"AAAAAAAAAA\"\n"
159 " or: \"BBBBBBBBBB\"\n"
161 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
162 " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
166 verifyFormat("optional AAA aaa = 1 [\n"
174 verifyFormat("optional string test = 1 [default = \"test\"\n"
176 verifyFormat("optional Aaaaaaaa aaaaaaaa = 12 [\n"
178 " (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
179 " aaaaaaaaaaaaaaaaa: true,\n"
180 " aaaaaaaaaaaaaaaa: true\n"
183 verifyFormat("extensions 20 [(proto2.type) = 'Aaaa.bbbb'];");
184 verifyFormat("extensions 20\n"
185 " [(proto3.type) = 'Aaaa.bbbb', (aaa.Aaa) = 'aaa.bbb'];");
186 verifyFormat("extensions 123 [\n"
188 " (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
189 " aaaaaaaaaaaaaaaaa: true,\n"
190 " aaaaaaaaaaaaaaaa: true\n"
195 TEST_F(FormatTestProto
, DoesntWrapFileOptions
) {
197 "option java_package = "
198 "\"some.really.long.package.that.exceeds.the.column.limit\";",
199 format("option java_package = "
200 "\"some.really.long.package.that.exceeds.the.column.limit\";"));
203 TEST_F(FormatTestProto
, TrailingCommentAfterFileOption
) {
204 verifyFormat("option java_package = \"foo.pkg\"; // comment");
207 TEST_F(FormatTestProto
, FormatsOptions
) {
208 verifyFormat("option (MyProto.options) = {\n"
212 " msg_field: { field_d: 123 }\n"
214 verifyFormat("option (MyProto.options) = {\n"
218 " msg_field: { field_d: 123 field_e: OK }\n"
220 verifyFormat("option (MyProto.options) = {\n"
221 " field_a: OK // Comment\n"
224 " msg_field: { field_d: 123 }\n"
226 verifyFormat("option (MyProto.options) = {\n"
228 " msg_field { field_d: 123 }\n"
230 verifyFormat("option (MyProto.options) = {\n"
232 " field_b { field_c: OK }\n"
237 // Support syntax with <> instead of {}.
238 verifyFormat("option (MyProto.options) = {\n"
239 " field_c: \"OK\",\n"
240 " msg_field: < field_d: 123 >\n"
245 verifyFormat("option (MyProto.options) = {\n"
247 " field_b < field_c: OK >\n"
252 verifyFormat("option (MyProto.options) = {\n"
254 " field_c: \"OK\",\n"
255 " msg_field: < field_d: 123 >\n"
257 " msg_field: < field_d: 12 >\n"
260 verifyFormat("option (MyProto.options) = <\n"
268 verifyFormat("option (MyProto.options) = <\n"
270 " field_b: \"OK\",\n"
276 verifyFormat("option (MyProto.options) = <\n"
278 " msg_field: { field_b: OK }\n"
284 verifyFormat("option (MyProto.options) = <\n"
296 verifyFormat("option (MyProto.options) = <\n"
308 verifyFormat("option (MyProto.options) = <\n"
320 verifyFormat("option (MyProto.options) = <\n"
332 verifyFormat("option (MyProto.options) = <\n"
344 verifyFormat("option (MyProto.options) = {\n"
356 verifyFormat("option (MyProto.options) = {\n"
368 verifyFormat("option (MyProto.options) = <\n"
381 " msg_field < field_A: 1 field_B: 2 field_C: 3 f_D: 4 >\n"
388 verifyFormat("option (MyProto.options) = <\n"
389 " data1 < key1: value1 >\n"
390 " data2 { key2: value2 }\n"
393 verifyFormat("option (MyProto.options) = <\n"
394 " app_id: 'com.javax.swing.salsa.latino'\n"
396 " data < key: value >\n"
399 verifyFormat("option (MyProto.options) = {\n"
400 " app_id: 'com.javax.swing.salsa.latino'\n"
402 " headheadheadheadheadhead_id: 1\n"
403 " product_data { product { 1 } }\n"
407 TEST_F(FormatTestProto
, DoesntWrapPackageStatements
) {
410 " some.really.long.package.that.exceeds.the.column.limit00000000;");
413 TEST_F(FormatTestProto
, TrailingCommentAfterPackage
) {
414 verifyFormat("package foo.pkg; // comment");
417 TEST_F(FormatTestProto
, FormatsService
) {
418 verifyFormat("service SearchService {\n"
419 " rpc Search(SearchRequest) returns (SearchResponse) {\n"
420 " option foo = true;\n"
425 TEST_F(FormatTestProto
, ExtendingMessage
) {
426 verifyFormat("extend .foo.Bar {}");
429 TEST_F(FormatTestProto
, FormatsImports
) {
430 verifyFormat("import \"a.proto\";\n"
431 "import \"b.proto\";\n"
435 verifyFormat("import public \"a.proto\";\n"
436 "import \"b.proto\";\n"
440 // Missing semicolons should not confuse clang-format.
441 verifyFormat("import \"a.proto\"\n"
442 "import \"b.proto\"\n"
447 TEST_F(FormatTestProto
, KeepsLongStringLiteralsOnSameLine
) {
449 "option (MyProto.options) = {\n"
451 " text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaa\"\n"
456 TEST_F(FormatTestProto
, FormatsOptionsExtensions
) {
457 verifyFormat("option (MyProto.options) = {\n"
458 " msg_field: { field_d: 123 }\n"
459 " [ext.t/u] { key: value }\n"
462 " [ext] { key: value }\n"
467 TEST_F(FormatTestProto
, SpacesAroundPercents
) {
468 verifyFormat("option (MyProto.options) = {\n"
475 TEST_F(FormatTestProto
, FormatsRepeatedListInitializersInOptions
) {
476 verifyFormat("option (MyProto.options) = {\n"
505 TEST_F(FormatTestProto
, AcceptsOperatorAsKeyInOptions
) {
506 verifyFormat("option (MyProto.options) = {\n"
508 " ccccccccccccccccccccccc: <\n"
512 " operator { key: value }\n"
518 TEST_F(FormatTestProto
, BreaksEntriesOfSubmessagesContainingSubmessages
) {
519 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_TextProto
);
520 Style
.ColumnLimit
= 60;
521 // The column limit allows for the keys submessage to be put on 1 line, but we
522 // break it since it contains a submessage an another entry.
523 verifyFormat("option (MyProto.options) = {\n"
524 " key: valueeeeeeee\n"
526 " item: 'aaaaaaaaaaaaaaaa'\n"
530 verifyFormat("option (MyProto.options) = {\n"
531 " key: valueeeeeeee\n"
533 " item: 'aaaaaaaaaaaaaaaa'\n"
537 verifyFormat("option (MyProto.options) = {\n"
538 " key: valueeeeeeee\n"
545 verifyFormat("option (MyProto.options) = {\n"
546 " key: valueeeeeeee\n"
548 " item: 'aaaaaaaaaaa'\n"
552 verifyFormat("option (MyProto.options) = {\n"
553 " key: valueeeeeeee\n"
555 " item: 'aaaaaaaaaaa'\n"
559 verifyFormat("option (MyProto.options) = {\n"
560 " key: valueeeeeeee\n"
562 " item: 'aaaaaaaaaaa'\n"
566 verifyFormat("option (MyProto.options) = {\n"
567 " key: valueeeeeeee\n"
569 " item: 'aaaaaaaaaaa'\n"
573 verifyFormat("option (MyProto.options) = {\n"
574 " key: valueeeeeeee\n"
576 " item: 'aaaaaaaaaaa'\n"
580 verifyFormat("option (MyProto.options) = {\n"
581 " key: valueeeeeeee\n"
584 " item: 'aaaaaaaaaaaaaaaa'\n"
587 verifyFormat("option (MyProto.options) = {\n"
588 " key: valueeeeeeee\n"
591 " item: 'aaaaaaaaaaaaaaaa'\n"
594 verifyFormat("option (MyProto.options) = {\n"
595 " key: valueeeeeeee\n"
598 " item: 'aaaaaaaaaaaaaaaa'\n"
601 verifyFormat("option (MyProto.options) = {\n"
602 " key: valueeeeeeee\n"
604 " sub { key: value }\n"
605 " item: 'aaaaaaaaaaaaaaaa'\n"
608 verifyFormat("option (MyProto.options) = {\n"
609 " key: valueeeeeeee\n"
612 " item: 'aaaaaaaaaaaaaaaa'\n"
615 verifyFormat("option (MyProto.options) = {\n"
616 " key: valueeeeeeee\n"
618 " sub < sub_2: {} >\n"
619 " item: 'aaaaaaaaaaaaaaaa'\n"
622 verifyFormat("option (MyProto.options) = {\n"
623 " key: valueeeeeeee\n"
627 " item: 'aaaaaaaaaaaaaaaa'\n"
630 verifyFormat("option (MyProto.options) = {\n"
631 " key: valueeeeeeee\n"
634 " sub < sub_2: {} >\n"
635 " item: 'aaaaaaaaaaaaaaaa'\n"
638 verifyFormat("option (MyProto.options) = {\n"
640 " key: valueeeeeeee\n"
643 " item: 'aaaaaaaaaaaaaaaa'\n"
649 TEST_F(FormatTestProto
, PreventBreaksBetweenKeyAndSubmessages
) {
650 verifyFormat("option (MyProto.options) = {\n"
652 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
655 verifyFormat("option (MyProto.options) = {\n"
657 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
660 verifyFormat("option (MyProto.options) = {\n"
662 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
665 verifyFormat("option (MyProto.options) = {\n"
667 " key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
670 verifyFormat("option (MyProto.options) = {\n"
672 " 'eyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
677 } // namespace format
678 } // end namespace clang