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 verifyFormat("optional Aaaaaaaa aaaaaaaa = 12 [\n" 142 " (aaa) = aaaa,\n" 143 " (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n" 144 " aaaaaaaaaaaaaaaaa: true,\n" 145 " aaaaaaaaaaaaaaaa: true\n" 146 " }\n" 147 "];"); 148 } 149 150 TEST_F(FormatTestProto, DoesntWrapFileOptions) { 151 EXPECT_EQ( 152 "option java_package = " 153 "\"some.really.long.package.that.exceeds.the.column.limit\";", 154 format("option java_package = " 155 "\"some.really.long.package.that.exceeds.the.column.limit\";")); 156 } 157 158 TEST_F(FormatTestProto, FormatsOptions) { 159 verifyFormat("option (MyProto.options) = {\n" 160 " field_a: OK\n" 161 " field_b: \"OK\"\n" 162 " field_c: \"OK\"\n" 163 " msg_field: {field_d: 123}\n" 164 "};"); 165 verifyFormat("option (MyProto.options) = {\n" 166 " field_a: OK\n" 167 " field_b: \"OK\"\n" 168 " field_c: \"OK\"\n" 169 " msg_field: {field_d: 123 field_e: OK}\n" 170 "};"); 171 verifyFormat("option (MyProto.options) = {\n" 172 " field_a: OK // Comment\n" 173 " field_b: \"OK\"\n" 174 " field_c: \"OK\"\n" 175 " msg_field: {field_d: 123}\n" 176 "};"); 177 verifyFormat("option (MyProto.options) = {\n" 178 " field_c: \"OK\"\n" 179 " msg_field{field_d: 123}\n" 180 "};"); 181 182 // Support syntax with <> instead of {}. 183 verifyFormat("option (MyProto.options) = {\n" 184 " field_c: \"OK\",\n" 185 " msg_field: <field_d: 123>\n" 186 "};"); 187 } 188 189 TEST_F(FormatTestProto, FormatsService) { 190 verifyFormat("service SearchService {\n" 191 " rpc Search(SearchRequest) returns (SearchResponse) {\n" 192 " option foo = true;\n" 193 " }\n" 194 "};"); 195 } 196 197 TEST_F(FormatTestProto, ExtendingMessage) { 198 verifyFormat("extend .foo.Bar {\n" 199 "}"); 200 } 201 202 TEST_F(FormatTestProto, FormatsImports) { 203 verifyFormat("import \"a.proto\";\n" 204 "import \"b.proto\";\n" 205 "// comment\n" 206 "message A {\n" 207 "}"); 208 209 verifyFormat("import public \"a.proto\";\n" 210 "import \"b.proto\";\n" 211 "// comment\n" 212 "message A {\n" 213 "}"); 214 215 // Missing semicolons should not confuse clang-format. 216 verifyFormat("import \"a.proto\"\n" 217 "import \"b.proto\"\n" 218 "// comment\n" 219 "message A {\n" 220 "}"); 221 } 222 223 } // end namespace tooling 224 } // end namespace clang 225