1 //===- unittest/Format/FormatTestProto.cpp --------------------------------===//
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 "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTestProto : public ::testing::Test {
21 protected:
22   static std::string format(llvm::StringRef Code, unsigned Offset,
23                             unsigned Length, const FormatStyle &Style) {
24     DEBUG(llvm::errs() << "---\n");
25     DEBUG(llvm::errs() << Code << "\n\n");
26     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28     auto Result = applyAllReplacements(Code, Replaces);
29     EXPECT_TRUE(static_cast<bool>(Result));
30     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
31     return *Result;
32   }
33 
34   static std::string format(llvm::StringRef Code) {
35     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
36     Style.ColumnLimit = 60; // To make writing tests easier.
37     return format(Code, 0, Code.size(), Style);
38   }
39 
40   static void verifyFormat(llvm::StringRef Code) {
41     EXPECT_EQ(Code.str(), format(test::messUp(Code)));
42   }
43 };
44 
45 TEST_F(FormatTestProto, FormatsMessages) {
46   verifyFormat("message SomeMessage {\n"
47                "  required int32 field1 = 1;\n"
48                "}");
49   verifyFormat("message SomeMessage {\n"
50                "  required .absolute.Reference field1 = 1;\n"
51                "}");
52   verifyFormat("message SomeMessage {\n"
53                "  required int32 field1 = 1;\n"
54                "  optional string field2 = 2 [default = \"2\"]\n"
55                "}");
56 
57   verifyFormat("message SomeMessage {\n"
58                "  optional really.really.long.qualified.type.aaa.aaaaaaa\n"
59                "      fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
60                "  optional\n"
61                "      really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
62                "          another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
63                "}");
64 }
65 
66 TEST_F(FormatTestProto, KeywordsInOtherLanguages) {
67   verifyFormat("optional string operator = 1;");
68 }
69 
70 TEST_F(FormatTestProto, FormatsEnums) {
71   verifyFormat("enum Type {\n"
72                "  UNKNOWN = 0;\n"
73                "  TYPE_A = 1;\n"
74                "  TYPE_B = 2;\n"
75                "};");
76   verifyFormat("enum Type {\n"
77                "  UNKNOWN = 0 [(some_options) = {a: aa, b: bb}];\n"
78                "};");
79   verifyFormat("enum Type {\n"
80                "  UNKNOWN = 0 [(some_options) = {\n"
81                "    a: aa,  // wrap\n"
82                "    b: bb\n"
83                "  }];\n"
84                "};");
85 }
86 
87 TEST_F(FormatTestProto, UnderstandsReturns) {
88   verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
89 }
90 
91 TEST_F(FormatTestProto, MessageFieldAttributes) {
92   verifyFormat("optional string test = 1 [default = \"test\"];");
93   verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
94   verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
95                "  default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
96                "  deprecated = true\n"
97                "];");
98   verifyFormat("optional LongMessageType long_proto_field = 1\n"
99                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
100   verifyFormat("repeated double value = 1\n"
101                "    [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
102   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
103                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
104                "  bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
105                "}];");
106   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
107                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
108                "  bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
109                "}];");
110   verifyFormat("repeated double value = 1 [\n"
111                "  (aaaaaaa.aaaaaaaaa) = {\n"
112                "    aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
113                "    bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
114                "  },\n"
115                "  (bbbbbbb.bbbbbbbbb) = {\n"
116                "    aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
117                "    bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
118                "  }\n"
119                "];");
120   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
121                "  type: \"AAAAAAAAAA\"\n"
122                "  is: \"AAAAAAAAAA\"\n"
123                "  or: \"BBBBBBBBBB\"\n"
124                "}];");
125   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
126                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
127                "  bbbbbbb: BBBB,\n"
128                "  bbbb: BBB\n"
129                "}];");
130   verifyFormat("optional AAA aaa = 1 [\n"
131                "  foo = {\n"
132                "    key: 'a'  //\n"
133                "  },\n"
134                "  bar = {\n"
135                "    key: 'a'  //\n"
136                "  }\n"
137                "];");
138   verifyFormat("optional string test = 1 [default =\n"
139                "                              \"test\"\n"
140                "                              \"test\"];");
141 }
142 
143 TEST_F(FormatTestProto, DoesntWrapFileOptions) {
144   EXPECT_EQ(
145       "option java_package = "
146       "\"some.really.long.package.that.exceeds.the.column.limit\";",
147       format("option    java_package   =    "
148              "\"some.really.long.package.that.exceeds.the.column.limit\";"));
149 }
150 
151 TEST_F(FormatTestProto, FormatsOptions) {
152   verifyFormat("option (MyProto.options) = {\n"
153                "  field_a: OK\n"
154                "  field_b: \"OK\"\n"
155                "  field_c: \"OK\"\n"
156                "  msg_field: {field_d: 123}\n"
157                "};");
158   verifyFormat("option (MyProto.options) = {\n"
159                "  field_a: OK\n"
160                "  field_b: \"OK\"\n"
161                "  field_c: \"OK\"\n"
162                "  msg_field: {field_d: 123 field_e: OK}\n"
163                "};");
164   verifyFormat("option (MyProto.options) = {\n"
165                "  field_a: OK  // Comment\n"
166                "  field_b: \"OK\"\n"
167                "  field_c: \"OK\"\n"
168                "  msg_field: {field_d: 123}\n"
169                "};");
170   verifyFormat("option (MyProto.options) = {\n"
171                "  field_c: \"OK\"\n"
172                "  msg_field{field_d: 123}\n"
173                "};");
174 
175   // Support syntax with <> instead of {}.
176   verifyFormat("option (MyProto.options) = {\n"
177                "  field_c: \"OK\",\n"
178                "  msg_field: <field_d: 123>\n"
179                "};");
180 }
181 
182 TEST_F(FormatTestProto, FormatsService) {
183   verifyFormat("service SearchService {\n"
184                "  rpc Search(SearchRequest) returns (SearchResponse) {\n"
185                "    option foo = true;\n"
186                "  }\n"
187                "};");
188 }
189 
190 TEST_F(FormatTestProto, ExtendingMessage) {
191   verifyFormat("extend .foo.Bar {\n"
192                "}");
193 }
194 
195 TEST_F(FormatTestProto, FormatsImports) {
196   verifyFormat("import \"a.proto\";\n"
197                "import \"b.proto\";\n"
198                "// comment\n"
199                "message A {\n"
200                "}");
201 
202   verifyFormat("import public \"a.proto\";\n"
203                "import \"b.proto\";\n"
204                "// comment\n"
205                "message A {\n"
206                "}");
207 
208   // Missing semicolons should not confuse clang-format.
209   verifyFormat("import \"a.proto\"\n"
210                "import \"b.proto\"\n"
211                "// comment\n"
212                "message A {\n"
213                "}");
214 }
215 
216 } // end namespace tooling
217 } // end namespace clang
218