blob: 6881af4361cf788f583cadc32fe36ae4376e8ead [file] [log] [blame]
Daniel Jasper7052ce62014-01-19 09:04:08 +00001//===- 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
Daniel Jasper7052ce62014-01-19 09:04:08 +000010#include "FormatTestUtils.h"
11#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Chandler Carruth10346662014-04-22 03:17:02 +000015#define DEBUG_TYPE "format-test"
16
Daniel Jasper7052ce62014-01-19 09:04:08 +000017namespace clang {
18namespace format {
19
20class FormatTestProto : public ::testing::Test {
21protected:
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);
Eric Liu4f8d9942016-07-11 13:53:12 +000028 auto Result = applyAllReplacements(Code, Replaces);
29 EXPECT_TRUE(static_cast<bool>(Result));
30 DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
31 return *Result;
Daniel Jasper7052ce62014-01-19 09:04:08 +000032 }
33
34 static std::string format(llvm::StringRef Code) {
Nico Weber514ecc82014-02-02 20:50:45 +000035 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
Daniel Jasper215d6c82014-01-22 08:04:52 +000036 Style.ColumnLimit = 60; // To make writing tests easier.
37 return format(Code, 0, Code.size(), Style);
Daniel Jasper7052ce62014-01-19 09:04:08 +000038 }
39
40 static void verifyFormat(llvm::StringRef Code) {
41 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
42 }
43};
44
45TEST_F(FormatTestProto, FormatsMessages) {
46 verifyFormat("message SomeMessage {\n"
47 " required int32 field1 = 1;\n"
48 "}");
49 verifyFormat("message SomeMessage {\n"
Daniel Jasper9c2820c2014-06-23 07:36:25 +000050 " required .absolute.Reference field1 = 1;\n"
51 "}");
52 verifyFormat("message SomeMessage {\n"
Daniel Jasper7052ce62014-01-19 09:04:08 +000053 " required int32 field1 = 1;\n"
54 " optional string field2 = 2 [default = \"2\"]\n"
55 "}");
Daniel Jasper215d6c82014-01-22 08:04:52 +000056
57 verifyFormat("message SomeMessage {\n"
Nikola Smiljanice08a91e2014-05-08 00:05:13 +000058 " optional really.really.long.qualified.type.aaa.aaaaaaa\n"
Daniel Jasper215d6c82014-01-22 08:04:52 +000059 " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
60 " optional\n"
Nikola Smiljanice08a91e2014-05-08 00:05:13 +000061 " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
Daniel Jasper215d6c82014-01-22 08:04:52 +000062 " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
63 "}");
Daniel Jasper7052ce62014-01-19 09:04:08 +000064}
65
Daniel Jasper23d3bcf2015-06-25 08:38:46 +000066TEST_F(FormatTestProto, KeywordsInOtherLanguages) {
67 verifyFormat("optional string operator = 1;");
68}
69
Daniel Jasper7052ce62014-01-19 09:04:08 +000070TEST_F(FormatTestProto, FormatsEnums) {
71 verifyFormat("enum Type {\n"
72 " UNKNOWN = 0;\n"
73 " TYPE_A = 1;\n"
74 " TYPE_B = 2;\n"
75 "};");
Daniel Jasperc6dd2732015-07-16 14:25:43 +000076 verifyFormat("enum Type {\n"
Daniel Jasper7f3b99f2016-06-23 09:40:19 +000077 " UNKNOWN = 0 [(some_options) = {a: aa, b: bb}];\n"
78 "};");
79 verifyFormat("enum Type {\n"
Daniel Jasperc6dd2732015-07-16 14:25:43 +000080 " UNKNOWN = 0 [(some_options) = {\n"
Daniel Jasper7f3b99f2016-06-23 09:40:19 +000081 " a: aa, // wrap\n"
Daniel Jasperc6dd2732015-07-16 14:25:43 +000082 " b: bb\n"
83 " }];\n"
84 "};");
Daniel Jasper7052ce62014-01-19 09:04:08 +000085}
86
87TEST_F(FormatTestProto, UnderstandsReturns) {
88 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
89}
90
91TEST_F(FormatTestProto, MessageFieldAttributes) {
92 verifyFormat("optional string test = 1 [default = \"test\"];");
Daniel Jaspera0e9be22014-01-28 18:51:11 +000093 verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
Daniel Jasperccff4d12016-01-04 07:27:33 +000094 verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
95 " default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
96 " deprecated = true\n"
97 "];");
Daniel Jasper7052ce62014-01-19 09:04:08 +000098 verifyFormat("optional LongMessageType long_proto_field = 1\n"
99 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
Daniel Jasper6e58fee2014-01-29 18:43:40 +0000100 verifyFormat("repeated double value = 1\n"
Daniel Jasper783bac62014-04-15 09:54:30 +0000101 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
Daniel Jasperce2fdb02014-10-27 13:25:59 +0000102 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 "}];");
Daniel Jasperccff4d12016-01-04 07:27:33 +0000110 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 "];");
Daniel Jasperce2fdb02014-10-27 13:25:59 +0000120 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
Daniel Jasper1662bfe2015-04-03 21:15:46 +0000121 " type: \"AAAAAAAAAA\"\n"
122 " is: \"AAAAAAAAAA\"\n"
123 " or: \"BBBBBBBBBB\"\n"
124 "}];");
125 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
Daniel Jasperce2fdb02014-10-27 13:25:59 +0000126 " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
127 " bbbbbbb: BBBB,\n"
128 " bbbb: BBB\n"
129 "}];");
Daniel Jasperccff4d12016-01-04 07:27:33 +0000130 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 "];");
Daniel Jasper7052ce62014-01-19 09:04:08 +0000138}
139
Daniel Jasper1b998812015-04-23 09:54:10 +0000140TEST_F(FormatTestProto, DoesntWrapFileOptions) {
141 EXPECT_EQ(
142 "option java_package = "
143 "\"some.really.long.package.that.exceeds.the.column.limit\";",
144 format("option java_package = "
145 "\"some.really.long.package.that.exceeds.the.column.limit\";"));
146}
147
Daniel Jasper929b1db2014-01-20 16:47:22 +0000148TEST_F(FormatTestProto, FormatsOptions) {
Daniel Jaspera382cbe2014-07-28 14:08:09 +0000149 verifyFormat("option (MyProto.options) = {\n"
150 " field_a: OK\n"
151 " field_b: \"OK\"\n"
152 " field_c: \"OK\"\n"
153 " msg_field: {field_d: 123}\n"
154 "};");
Daniel Jaspera382cbe2014-07-28 14:08:09 +0000155 verifyFormat("option (MyProto.options) = {\n"
156 " field_a: OK\n"
157 " field_b: \"OK\"\n"
158 " field_c: \"OK\"\n"
Daniel Jasper7f3b99f2016-06-23 09:40:19 +0000159 " msg_field: {field_d: 123 field_e: OK}\n"
Daniel Jaspera382cbe2014-07-28 14:08:09 +0000160 "};");
Daniel Jaspera382cbe2014-07-28 14:08:09 +0000161 verifyFormat("option (MyProto.options) = {\n"
162 " field_a: OK // Comment\n"
163 " field_b: \"OK\"\n"
164 " field_c: \"OK\"\n"
165 " msg_field: {field_d: 123}\n"
166 "};");
Daniel Jasper497d9fd2014-08-15 05:00:35 +0000167 verifyFormat("option (MyProto.options) = {\n"
168 " field_c: \"OK\"\n"
169 " msg_field{field_d: 123}\n"
170 "};");
Daniel Jasperffbad0e2016-01-04 07:28:12 +0000171
172 // Support syntax with <> instead of {}.
173 verifyFormat("option (MyProto.options) = {\n"
174 " field_c: \"OK\",\n"
175 " msg_field: <field_d: 123>\n"
176 "};");
Daniel Jasper929b1db2014-01-20 16:47:22 +0000177}
178
Daniel Jasper220c0d12014-04-10 07:27:12 +0000179TEST_F(FormatTestProto, FormatsService) {
180 verifyFormat("service SearchService {\n"
181 " rpc Search(SearchRequest) returns (SearchResponse) {\n"
182 " option foo = true;\n"
183 " }\n"
184 "};");
185}
186
Daniel Jasperd8d98392015-11-20 14:32:54 +0000187TEST_F(FormatTestProto, ExtendingMessage) {
188 verifyFormat("extend .foo.Bar {\n"
189 "}");
190}
191
Daniel Jasper3d5a7d62016-06-20 18:20:38 +0000192TEST_F(FormatTestProto, FormatsImports) {
193 verifyFormat("import \"a.proto\";\n"
194 "import \"b.proto\";\n"
195 "// comment\n"
196 "message A {\n"
197 "}");
198
Daniel Jasper8b61d142016-06-20 20:39:53 +0000199 verifyFormat("import public \"a.proto\";\n"
200 "import \"b.proto\";\n"
201 "// comment\n"
202 "message A {\n"
203 "}");
204
Daniel Jasper3d5a7d62016-06-20 18:20:38 +0000205 // Missing semicolons should not confuse clang-format.
206 verifyFormat("import \"a.proto\"\n"
207 "import \"b.proto\"\n"
208 "// comment\n"
209 "message A {\n"
210 "}");
211}
212
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000213} // end namespace tooling
Daniel Jasper7052ce62014-01-19 09:04:08 +0000214} // end namespace clang